一直说设计模式,零零散散的看过不少学过一些,今天(2011-04-27)下载了Gof 那本书,决定系统的回顾一下。
一、引言
- 了解到MVC是多种设计模式的集合:
The main relationships in MVC are given by the three design patterns:
Observer(Whenever the model's data changes, the model notifies views that depend on it.)
Composite(a control panel of buttons might be implemented as a complex view containing nested button views)
Strategy(change the way a view responds to user input without changing its visual presentation.)
MVC uses other design patterns, such as Factory Method (121) to specify the default controller class for a view and Decorator (196) to add scrolling to a view.
- 设计模式的分类,按purpose:
- Creational patterns concern the process of object creation.
- Structural patterns deal with the composition of classes or objects.
- Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility.
- 另一种分类,按scope:
- Class patterns deal with relationships between classes and their subclasses. These relationships are established through inheritance, so they are static—fixed at compile-time.
- Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.
- 一个图示,以前不常用的,如下,表示实例化一个类对象,指向被实例化的对象的类
二、案例分析
这章没有细看,主要通过一个例子说明用到了几种设计模式
三、Creational Patterns
- Abstract Factory
1、首先沿用书中的说法,先说下其意图:
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
提供这么一个接口:用来创建一系列相关或相互依赖的对象,而没有指定他们的具体类
2、下面类图说的是创建一个用户界面,这个界面可能有不同的风格(Motif或PM的),每种风格的窗口、滚动条样式都不相同
3、structure (等下结合bulider会有简短代码示例,说明2者的区别)
- Bulider
1、意图:
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
讲一个复制对象的构造与表现分离,使得同样的构造过程可以产生不同的表现
2、下面类图实现一个RTFReader,能够将RTF转换成多种格式
3、structure
4、与Abstract Factory区别
抽象工厂中,client里面其实还实现了builder中Director的功能,看如下代码,建立在一个迷宫游戏的创建上,主要列出了创建迷宫的过程
他们的区别可以看出来:
Abstract Factory (99) is similar to Builder in that it too may construct complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract Factory's emphasis is on families of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory pattern is concerned, the product gets returned immediately.
Builder 侧重的是一步一步的构造一个复杂的对象,Abstract Factory 强调的是一系列的产品对象。
Builder 在最后一步才返回产品,Abstract Factory 确实立即返回产品
- Factory Method
1、意图
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
定义一个接口来创建对象,让子类决定实例化那个类,他让一个类的实例化延迟到子类
2、下图说的是通过Application创建不同的文档
3、structure
4、补充。
通常会结合模版来进行使用。
文中提到了 Parameterized factory methods(参数化工厂方法),其实就是我们说的简单工厂。
实心圆点表示“多于一个”。
- Prototype
1、意图
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
用一个原型实例来指定创建对象的类型,通过拷贝这个原型来创建新的对象
2、下图说明的是一个图形编辑器的通用框架,当添加五线谱等对象时,是通过GraphicTool调用一个prototype的Clone来实现。
3、structure
4、补充
注意深拷贝浅拷贝问题
- Singleton
1、意图
Ensure a class only has one instance, and provide a global point of access to it.
确保一个类只有一个实例,提供一个全局访问点来访问它
2、structure
3、补充
文中对于为什么不使用全局静态对象,而使用singleton,说了3大原因:
a> We can't guarantee that only one instance of a static object will ever be declared.
就是说,静态对象,你可能不小心在多处声明。
b> We might not have enough information to instantiate every singleton at static initialization time. A singleton might require values that are computed later in the program's execution.
有时候我们可能在运行时传一些值给singleton(比如Instance可以有参数传入),来确定其该怎么构造,而静态对象做不到这一点。
c> C++ doesn't define the order in which constructors for global objects are called across translation units [ES90]. This means that no dependencies can exist between singletons; if any do, then errors are inevitable.
我们可能使用多个singletons,而这些singletons是有相互依赖关系的,呃...静态对象也做不到这一点。
还有个小原因:it forces all singletons to be created whether they are used or not.