码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-06-15 13:03:03      阅读:125      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!