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); } }
原文:http://www.cnblogs.com/firegy/p/4576687.html