首页 > 编程语言 > 详细

java进阶之工厂模式(二)抽象工厂模式

时间:2015-06-15 13:00:12      阅读:209      评论:0      收藏:0      [点我收藏+]
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进阶之工厂模式(二)抽象工厂模式

原文:http://www.cnblogs.com/firegy/p/4576687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!