设计模式(3)-对象创建型模式-Abstract Factory模式

1.对象创建型模式

1.3           Abstract Factory模式

1.3.1 需求

在下面情况能够使用Abstract Factory模式:

•  一个系统要独立于它的产品的创建、组合和表示时(这个需求和FactoryMethod类似)。

•  一个系统要由多个产品系列中的一个来配置时(这个需求也和Factory Method类似)。

•  当你要强调一系列相关的产品对象的设计以便进行联合使用时(这个需求表明一个工厂要创建多个相关的产品对象,是比FactoryMethod多的需求)。

•  当你提供一个产品类库,而仅仅想显示它们的接口而不是实现时(这个也与FactoryMethod类似)。

依据这个适应场景能够看出,AbstractFactory模式是为创建一组(有多类)相关或依赖的对象提供创建接口,而Factory Method模式(常常直接成为工厂模式)是为一类对象提供创建接口或延迟对象的创建到子类中实现。

1.3.2功能

                提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们详细的类。

1.3.3 结构

 

•A b s t r a c t F a c t o r y ( Wi d g e t F a c t o r y )

                — 声明一个创建抽象产品对象的操作接口。

•C o n c r e t e F a c t o r y ( M o t i f Wi d g e t F a c t o r y ,P M Wid g e t F a c t o r y )

— 实现创建详细产品对象的操作。

•A b s t r a c t P r o d u c t ( Wi n d o w s,S c r o l l B a r )

— 为一类产品对象声明一个接口。

•C o n c r e t e P r o d u c t ( M o t i f Wi n d o w,M o t i f S c r o l l B a r )

— 定义一个将被对应的详细工厂创建的产品对象。

— 实现Ab s t r a c t P r o d u c t接口。

•C l i e n t

— 仅使用由Ab s t r a c t F a c t o r y和Ab s t r a c t P r o d u c t类声明的接口。

 

1.3.4 优缺点

长处:

1.      分类了详细的类:由于一个工厂封装创建产品对象的责任和过程,客户假设使用这样的模式开发一个库,使用这个库的程序就叫做客户)将仅仅须要通过抽象接口来操作实例。这意味着客户与产品类的实现分离,并且客户不须要知道产品类的详细名称,仅仅须要知道要生成这个系列产品的工厂的名称。

2.      易于交换产品系列:在改动代码时,客户程序中仅仅须要改动要生成产品的工厂,不须要改动其它不论什么代码。

3.      有利于产品的一致性:它保证了产品簇的相关性,当一个产品族中的多个对象被设计成一起工作时,它可以保证client始终仅仅使用同一个产品族中的对象。

 

缺点:

难以支持新种类的产品. 这是由于A b s t r a c t F a c t or y接口确定了能够被创建的产品集合。 支持新种类的产品就须要扩展该工厂接口,这将涉及A b s t r a c t  F a c t o r y类及其全部子类的改变。正所谓鱼与熊掌,不可兼得啊,你打包组合在一起,自然要承担这个缺点。

 

1.35 样例-C++

//Product.h

#ifndef _PRODUCT_H_

#define _PRODUCT_H_

 

class AbstractProductA

{

public:

       virtual ~AbstractProductA();

protected:

       AbstractProductA();

private:

};

 

class AbstractProductB

{

public:

       virtual ~AbstractProductB();

protected:

       AbstractProductB();

private:

};

 

class ProductA1 :publicAbstractProductA

{

public:

       ProductA1();

       ~ProductA1();

protected

private:

};

 

class ProductA2 :publicAbstractProductA

{

public:

       ProductA2();

       ~ProductA2();

protected

private:

};

 

 

class ProductB1 :publicAbstractProductB

{

public:

       ProductB1();

       ~ProductB1();

protected:

private:

};

 

class ProductB2 :publicAbstractProductB

{

public:

       ProductB2();

       ~ProductB2();

protected

private:

};

 

 

#endif //~_PRODUCT_H_ECT_H_

 

//Product.cpp

#include"Product.h"

#include<iostream>

using namespace std;

AbstractProductA::AbstractProductA()

{}

 

AbstractProductA::~AbstractProductA()

{}

 

AbstractProductB::AbstractProductB()

{

}

 

AbstractProductB::~AbstractProductB()

{

}

 

ProductA1::ProductA1()

{

       cout << "ProductA1..."<< endl;

}

 

ProductA1::~ProductA1()

{

}

 

ProductA2::ProductA2()

{

       cout << "ProductA2..."<< endl;

}

 

ProductA2::~ProductA2()

{

}

 

ProductB1::ProductB1()

{

       cout << "ProductB1..."<< endl;

}

 

ProductB1::~ProductB1()

{

}

 

ProductB2::ProductB2()

{

       cout << "ProductB2..."<< endl;

}

 

ProductB2::~ProductB2()

{

}

 

//AbstractFactory.h

#ifndef _ABSTRACTFACTORY_H_

#define _ABSTRACTFACTORY_H_

class AbstractProductA;

class AbstractProductB;

 

class AbstractFactory

{

public:

       virtual ~AbstractFactory();

       virtual  AbstractProductA*

              CreateProductA() = 0;

       virtual  AbstractProductB*

              CreateProductB() = 0;

protected:

       AbstractFactory();

private:

};

 

class ConcreteFactory1 :publicAbstractFactory

{

public:

       ConcreteFactory1();

       ~ConcreteFactory1();

       AbstractProductA* CreateProductA();

       AbstractProductB* CreateProductB();

protected:

private:

};

 

class ConcreteFactory2 :publicAbstractFactory

{

public:

       ConcreteFactory2();

       ~ConcreteFactory2();

       AbstractProductA* CreateProductA();

       AbstractProductB* CreateProductB();

protected:

private:

};

 

#endif //~_ABSTRACTFACTORY_H_

 

//AbstractFactory.cpp

#include"AbstractFactory.h"

#include"Product.h"

#include<iostream>

using namespace std;

 

AbstractFactory::AbstractFactory()

{

}

 

AbstractFactory::~AbstractFactory()

{

}

 

ConcreteFactory1::ConcreteFactory1()

{

}

 

ConcreteFactory1::~ConcreteFactory1()

{

}

 

AbstractProductA*

ConcreteFactory1::CreateProductA()

{

       return newProductA1();

}

 

AbstractProductB*

ConcreteFactory1::CreateProductB()

{

       return newProductB1();

}

 

ConcreteFactory2::ConcreteFactory2()

{

}

 

ConcreteFactory2::~ConcreteFactory2()

{

}

 

AbstractProductA*

ConcreteFactory2::CreateProductA()

{

       return newProductA2();

}

 

AbstractProductB*

ConcreteFactory2::CreateProductB()

{

       return newProductB2();

}

 

//main.cpp

#include"AbstractFactory.h"

#include<iostream>

using namespace std;

int main(int argc, char*argv[])

{

       AbstractFactory* cf1 = new

              ConcreteFactory1();

       cf1->CreateProductA();

       cf1->CreateProductB();

       AbstractFactory* cf2 = new

              ConcreteFactory2();

       cf2->CreateProductA();

       cf2->CreateProductB();

       return 0;

}

1.3.5 样例-JAVA

1. //抽象产品  

2. UpperClothes.java  

3. public abstract class UpperClothes {  

4.     public abstract int getChestSize();  

5.     public abstract int getHeight();  

6.     public abstract String getName();  

7. }  

8. Trousers.java  

9. public abstract class Trousers {  

10.    public abstract int getWaistSize();  

11.    public abstract int getHeight();  

12.    public abstract String getName();  

13.}  

14.//详细产品  

15.WesternUpperClothes.java  

16.public class WesternUpperClothes extends UpperClothes {  

17.    private int chestSize;  

18.    private int height;  

19.    private String name;  

20.    WesternUpperClothes(String name,int chestSize,int height){  

21.        this.name=name;  

22.        this.chestSize=chestSize;  

23.        this.height=height;  

24.    }  

25.    public int getChestSize() {  

26.        return chestSize;  

27.    }  

28.    public int getHeight() {  

29.        return height;  

30.    }  

31.    public String getName() {  

32.        return name;  

33.    }  

34.}  

35.CowboyUpperClothes.java  

36.public class CowboyUpperClothes extends UpperClothes {  

37.    private int chestSize;  

38.    private int height;  

39.    private String name;  

40.    CowboyUpperClothes(String name,int chestSize,int height){  

41.        this.name=name;  

42.        this.chestSize=chestSize;  

43.        this.height=height;  

44.    }  

45.    public int getChestSize() {  

46.        return chestSize;  

47.    }  

48.    public int getHeight() {  

49.        return height;  

50.    }  

51.    public String getName () {  

52.        return name;  

53.    }  

54.}  

55.WesternTrousers.java  

56.public class WesternTrousers extends Trousers {  

57.    private int waistSize;  

58.    private int height;  

59.    private String name;  

60.    WesternTrousers(String name,int waistSize,int height){  

61.        this.name=name;  

62.        this.waistSize=waistSize;  

63.        this.height=height;  

64.    }  

65.    public int getHeight() {  

66.        return height;  

67.    }  

68.    public String getName() {  

69.        return name;  

70.    }  

71.    public int getWaistSize() {  

72.        return waistSize;  

73.    }  

74.}  

75.CowboyTrousers.java  

76.public class CowboyTrousers extends Trousers {  

77.    private int waistSize;  

78.    private int height;  

79.    private String name;  

80.    CowboyTrousers(String name,int waistSize,int height){  

81.        this.name=name;  

82.        this.waistSize=waistSize;  

83.        this.height=height;  

84.    }  

85.    public int getHeight() {  

86.        return height;  

87.    }  

88.    public String getName() {  

89.        return name;  

90.    }  

91.    public int getWaistSize() {  

92.        return waistSize;  

93.    }  

94.}  

95.//抽象工厂  

96.ClothesFactory.java  

97.public abstract class ClothesFactory {  

98.    public abstract UpperClothes createUpperClothes(int chestSize,int height);  

99.    public abstract Trousers createTrousers(int waistSize,int height);  

100. }  

101. //详细工厂  

102. BeijingClothesFactory.java  

103. public class BeijingClothesFactory extends ClothesFactory {  

104.     public Trousers createTrousers(int waistSize, int height) {  

105.         return new WesternTrousers("北京牌裤子",waistSize,height);  

106.     }  

107.     public UpperClothes createUpperClothes(int chestSize, int height) {  

108.         return new WesternUpperClothes("北京牌上衣",chestSize,height);  

109.     }  

110. }  

111. ShanghaiClothesFactory.java  

112. public class ShanghaiClothesFactory extends ClothesFactory {  

113.     public Trousers createTrousers(int waistSize, int height) {  

114.         return new WesternTrousers("上海牌裤子",waistSize,height);  

115.     }  

116.     public UpperClothes createUpperClothes(int chestSize, int height) {  

117.         return new WesternUpperClothes("上海牌上衣",chestSize,height);  

118.     }  

119. }  

 

package DesignPattern;

 

public class TestMain {

 

       /**

        * @param args

        */

       public static void main(String[] args){

              ClothesFactoryf=newShanghaiClothesFactory();

              //ClothesFactoryf=new BeijingClothesFactory();

 

              Trouserst = f.createTrousers(13, 14);

              System.out.println(t.getName());

             

              UpperClothesuc=f.createUpperClothes(13,14);

              System.out.println(uc.getName());

       }

}

 

posted @ 2014-07-11 17:37  hrhguanli  阅读(152)  评论(0编辑  收藏  举报