標籤:
public interface KitchenFactory{//抽象工廠public Food getFood();抽象方法public TableWare getTableWare();}public interface Food{//抽象食物public String getFoodName();}public interface TableWare{public String getToolName();//抽象餐具}public class AKitchen implements KitchenFactory{//具體工廠public Food getFood(){return new Apple();}public TableWare getTableWare(){return new Knife();}}public class Apple implements Food{//具體食物public String getFoodName(){return "apple";}}public class Knife implements TableWare{//具體餐具public String getToolName(){return "Knife";}}public class Foodaholic{//吃貨要開吃了public void eat(KitchenFactory){System.out.println("A foodaholic is eating "+k.getFood().getFoodName()+"with "+k.getTableWare().getToolName());}public static void main(String[] args){Foodaholic fh=new Foodaholic();KitchenFactory kf=new AKitchen();fh.eat(kf);}}
java進階之原廠模式(二)抽象原廠模式