码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂模式

时间:2015-11-09 01:30:02      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

---使用抽象工厂模式实现以下产品线并代码实现
技术分享
 
 
---UML图
技术分享
1.  IFood接口以及实现它的抽象类Burger、Chicken、Drink
 1 interface IFood
 2 {
 3     abstract void order();
 4 }
 5 abstract class Burger implements IFood
 6 {
 7 }
 8 abstract class Chicken implements IFood
 9 {
10 }
11 abstract class Drink implements IFood
12 {
13 }


2.  KFC 和 McDonalds 的 Burger

 1 class KFC_Burger extends Burger
 2 {
 3     public void order()
 4     {
 5         System.out.println("This is KFC‘s Burger!");
 6     }
 7 }
 8 class MD_Burger extends Burger
 9 {
10     public void order()
11     {
12         System.out.println("This is McDonalds‘s Burger!");
13     }
14 }

 

3.  KFC 和 McDonalds 的 Chicken

 1 class KFC_Chicken extends Chicken
 2 {
 3     public void order()
 4     {
 5         System.out.println("This is KFC‘s Chicken!");
 6     }
 7 }
 8 class MD_Chicken extends Chicken
 9 {
10     public void order()
11     {
12         System.out.println("This is McDonalds‘s Chicken!");
13     }
14 }

 

4.  KFC 和 McDonalds 的 Drink

 1 class KFC_Drink extends Drink
 2 {
 3     public void order()
 4     {
 5         System.out.println("This is KFC‘s Drink!");
 6     }
 7 }
 8 class MD_Drink extends Drink
 9 {
10     public void order()
11     {
12         System.out.println("This is McDonalds‘s Drink!");
13     }
14 }

 

5.  IFoodFactory 工厂接口

1 public interface IFoodFactory
2 {
3     Burger CreateBurger();
4     Chicken CreateChicken();
5     Drink CreateDrink();
6 }


6.   KFCFactory

 1 public class KFCFactory implements IFoodFactory
 2 {
 3     public Burger CreateBurger()
 4     {
 5         Burger x=new KFC_Burger();
 6         return x;
 7     }
 8     public Chicken CreateChicken()
 9     {
10         Chicken x=new KFC_Chicken();
11         return x;
12     }
13     public Drink CreateDrink()
14     {
15         Drink x=new KFC_Drink();
16         return x;
17     }
18 }


7.  McDonaldsFactory

 1 public class McDonaldsFactory implements IFoodFactory
 2 {
 3     public Burger CreateBurger()
 4     {
 5         Burger x=new MD_Burger();
 6         return x;
 7     }
 8     public Chicken CreateChicken()
 9     {
10         Chicken x=new MD_Chicken();
11         return x;
12     }
13     public Drink CreateDrink()
14     {
15         Drink x=new MD_Drink();
16         return x;
17     }
18 }


8.  测试 test

 1 public class test
 2 {
 3     public static void main(String[] args) 
 4     {
 5         IFoodFactory factory1=new McDonaldsFactory();
 6         IFoodFactory factory2=new KFCFactory();
 7         Burger burger1 = factory1.CreateBurger();
 8         Chicken chicken1 = factory1.CreateChicken();
 9         Drink drink1 = factory1.CreateDrink();
10         burger1.order();
11         chicken1.order();
12         drink1.order();
13         Burger burger2 = factory2.CreateBurger();
14         Chicken chicken2 = factory2.CreateChicken();
15         Drink drink2 = factory2.CreateDrink();
16         burger2.order();
17         chicken2.order();
18         drink2.order();
19     }
20 }


9.  输出结果

 

技术分享

 

 

 

 

 

 

 
 

抽象工厂模式

标签:

原文地址:http://www.cnblogs.com/mustforever/p/4948822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!