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

抽象工厂模式

时间:2015-04-09 23:19:52      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

设计模式的意义在于:面向业务内容、业务数据结构和系统架构,高内聚低耦合、优雅的将平面逻辑立体化。

 1 package designPattern;
 2 /**
 3  * 抽象工厂模式
 4  * @author Administrator
 5  */
 6 public class A3_AbstractFactoryTest {
 7 
 8     /**
 9      *  提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
10      *  1.一个系统要独立于它的产品的创建、组合和表示时。
11      *    2.一个系统要由多个产品系列中的一个来配置时。
12      *    3.当你要强调一系列相关的产品对象的设计以便进行联合使用时。
13      *    4.当你提供一个产品类库,而只想显示它们的接口而不是实现时。
14      */
15     public static void main(String[] args) {
16         IAnimalFactory whiteAnimalFactory=new WhiteAnimalFactory();
17         ICat whiteCat=whiteAnimalFactory.createCat();
18         whiteCat.eat();
19         
20         IAnimalFactory blackAnimalFactory=new BlackAnimalFactory();
21         ICat blackCat=blackAnimalFactory.createCat();
22         blackCat.eat();
23         
24         IDog whiteDog=whiteAnimalFactory.createDog();
25         whiteDog.eat();
26         
27         IDog blackDog=blackAnimalFactory.createDog();
28         blackDog.eat();
29     }
30 }
31 //AbstractFactory 声明一个创建抽象产品对象的操作接口
32 interface IAnimalFactory 
33 {
34     ICat createCat();
35     IDog createDog();
36 }
37 //ConcreteFactory 声明一个创建具体产品对象的接口
38 class BlackAnimalFactory implements IAnimalFactory
39 {
40     public ICat createCat() {
41         return new BlackCat();
42     }
43 
44     public IDog createDog() {
45         return new BlackDog();
46     }    
47 }
48 class WhiteAnimalFactory implements IAnimalFactory
49 {
50     public ICat createCat() {
51         return new WhiteCat();
52     }
53 
54     public IDog createDog() {
55         return new WhiteDog();
56     }    
57 }
58 //AbstractProduct 为一类产品对象声明一个接口
59 interface ICat
60 {
61     void eat();
62 }
63 interface IDog
64 {
65     void eat();
66 }
67 //ConcreteProduct 定义一个将被相应的具体工厂创建的产品对象
68 class BlackCat implements ICat
69 {
70     public void eat() {
71         System.out.println("the blackCat is ...");        
72     }    
73 }
74 //ConcreteProduct 
75 class WhiteCat implements ICat
76 {
77     public void eat() {
78         System.out.println("the whiteCat is ...");        
79     }    
80 }
81 //ConcreteProduct 
82 class BlackDog implements IDog
83 {
84     public void eat() {
85         System.out.println("the blackdog is ...");        
86     }    
87 }
88 //ConcreteProduct 
89 class WhiteDog implements IDog
90 {
91     public void eat() {
92         System.out.println("the whitedog is ...");        
93     }    
94 }

 

环境:JDK1.6,MAVEN,tomcat,eclipse

源码地址:http://files.cnblogs.com/files/xiluhua/designPattern.rar

欢迎亲们评论指教。

抽象工厂模式

标签:

原文地址:http://www.cnblogs.com/xiluhua/p/4412386.html

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