public interface Supplier {
public void order(Product p, int quantity);
}
public class SupplierA implements Supplier {
public void order(Product p, int quantity) {
System.out.println("Enviar " + quantity + " " + p.toString());
}
}
public class SupplierB implements Supplier {
public void order(Product p, int quantity) {
System.out.println("Encomenda recebida: " + p.toString());
}
}
public abstract class Product {
private String name;
public Product(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
public abstract Supplier createSupplier();
}
public class Screw extends Product {
public Screw() {
super("Parafuso");
}
public Supplier createSupplier() {
return new SupplierA();
}
}
public class Nail extends Product {
public Parafuso() {
super("Prego");
}
public Supplier createSupplier() {
return new SupplierB();
}
}
public class App {
public static void main(String[] args) {
Product[] p = { new Nail(), new Screw() };
Supplier f = p[0].createSupplier();
f.order(p[0], 4);
f = p[1].createSupplier();
f.order(p[1], 5);
}
}