好看的排版,尽在博客园 设计模式之三:抽象工厂模式
2014-01-14 22:21:30
Abstract Factory,继续GOF。
1、Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
2、Also Known As
Kit 工具箱/成套工具
3、Motivation
Consider a user interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager. Different look-and-feels define different appearances and behaviors for user interface "widgets" like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look and feel. Instantiating look-and-feelspecific classes of widgets throughout the application makes it hard to change the look and feel later.
例如有一套形式各异的用户界面包,内含主题及展示工具。不同的视感风格为诸如滚动条、窗口和按钮等用户界面“窗口组件”定义不同的外观和行为。为保证视感风格标准间的可移植性,一个应用不应该为一个特定的视感外观固定它的窗口组件。在整个应用中实例化特定视感风格的窗口组件类将使得以后很难改变视感风格。
We can solve this problem by defining an abstract WidgetFactory class that declares an interface for creating each basic kind of widget. There's also an abstract class for each kind of widget, and concrete subclasses implement widgets for specific look-and-feel standards. WidgetFactory's interface has an operation that returns a new widget object for each abstract widget class. Clients call these operations to obtain widget instances, but clients aren't aware of the concrete classes they're using. Thus clients stay independent of the prevailing look and feel.
为解决这一问题我们可以定义一个抽象的WidgetFactory 类,这个类声明了一个用来创建每一类基本窗口组件的接口。每一类窗口组件都有一个抽象类,而具体子类则实现了窗口组件的特定视感风格。对于每一个抽象窗口组件类, WidgetFactory 接口都有一个返回新窗口组件对象的操作。客户调用这些操作以获得窗口组件实例,但客户并不知道他们正在使用的是哪些具体类。这样客户就不依赖于一般的视感风格,如下页图所示。
4、Applicability
Use the Abstract Factory pattern when
● a system should be independent of how its products are created, composed, and represented.
● a system should be configured with one of multiple families of products.
● a family of related product objects is designed to be used together, and you need to enforce this constraint.
● you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
在以下情况可以使用 Abstract Factory模式
● 一个系统要独立于它的产品的创建、组合和表示时。
● 一个系统要由多个产品系列中的一个来配置时。
● 当你要强调一系列相关的产品对象的设计以便进行联合使用时。
● 当你提供一个产品类库,而只想显示它们的接口而不是实现时。
5、Structure
6、Demo
此博客描述的很不错,就借用了。设计模式学习之简单工厂(simple facotry)、工厂方法(actory method)、抽象工厂(abstract factory)
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
abstract
class
Products
{
public
abstract
void
showProduct();
}
class
ProductBed : Products
{
public
override
void
showProduct()
{
Console.WriteLine(
"exist a Bed"
);
}
}
class
ProductDesk : Products
{
public
override
void
showProduct()
{
Console.WriteLine(
"exist a Desk"
);
}
}
class
ProductDoor : Products
{
public
override
void
showProduct()
{
Console.WriteLine(
"exist a Door"
);
}
}
class
ProductSofa : Products
{
public
override
void
showProduct()
{
Console.WriteLine(
"exist a Sofa"
);
}
}
//abstractfactory,这里是一个抽象的“家”
abstract
class
Home
{
protected
Products myBed,myDesk,myDoor,mySofa;
public
abstract
void
showMyHouse();
}
//简单的家:只需要门和床
class
SimpleHome : Home
{
public
SimpleHome()
{
myBed =
new
ProductBed();
myDoor =
new
ProductDoor();
}
public
override
void
showMyHouse()
{
Console.WriteLine(
"this is a simple Home:"
);
myBed.showProduct();
myDoor.showProduct();
}
}
//高级一点的家:还有沙发,桌子等家具
class
AdvancedHome : Home
{
public
AdvancedHome()
{
myBed =
new
ProductBed();
myDoor =
new
ProductDoor();
mySofa =
new
ProductSofa();
myDesk =
new
ProductDesk();
}
public
override
void
showMyHouse()
{
Console.WriteLine(
"this is a advanced Home:"
);
myBed.showProduct();
myDoor.showProduct();
mySofa.showProduct();
myDesk.showProduct();
}
}
//调用抽象工厂的客户类
class
Client
{
Home myHome;
public
Client(Home newHome)
{
this
.myHome = newHome;
}
public
void
showHome()
{
myHome.showMyHouse();
}
}
//--------------------执行-------------------
class
Program
{
static
void
Main(string[] args)
{
//客户不需要选择具体家具了,只需要选择具体的房子类型就可以了,家具自动配好。
Client client1 =
new
Client(
new
SimpleHome());
client1.showHome();
Client client2 =
new
Client(
new
AdvancedHome());
client2.showHome();
Console.ReadLine();
}
}
|