<<Design Pattern Concepts>> writen by taosinker
这是由我的一次讲义汇编而成
Basic Concepts
-
Design Pattern
重复出现的某情景下,针对某问题的解决方案
- 注意的事项
模式不是灵丹妙药
Simple is key
不要过度应用
GoF Patterns
提供创建一系列相关或相互依赖的对象接口,无须指定具体的实现类
将对象的构建与表示分离
定义一个用于创建对象的接口,让子类觉得那个类实例化
用原型实例指定创建对象的种类,并通过拷贝这个原型来创建新的对象
将一个类接口转换为客户希望的接口.
将抽象部分与实现部分分离,使他们独立地变化
将对象组合为树形结构以表示“整体和部分”的关系。使得客户对单个对象的使用和复合对象的使用具有一致性。
动态地为对象添加职责
为子系统的一组接口提供一致的界面
运用共享技术有效地支持大量细粒度的对象
为其它对象提供代理来控制对这个对象的访问。
Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
一种顺序访问聚合对象中各元素的方法,而又不暴露对象的内部表示
用中介对象封装一系列对象的交互
不破坏封装的前提下,捕获对象的内部状态,并在对象之外保持这个状态。
对象内部状态发生变化时,广播通知的机制
保证一个类只有一个实例
定义一系列算法,把他们封装起立,使它们可以互换
定义算法的框架,其中一些步骤延迟到子类
不改变各元素类的前提下定义作用于这些元素的新操作
将请求封装为对象,可以用不同的请求对客户进行参数化
将对象连成一条链,请求沿着链传递,直到有一个对象处理它.
容许对象状态改变时改变它的行为
如何学习,扪心自问的几个问题
- 应用了什么原则
- 解决了什么问题
- 有什么好处
- 自己如何处理类似情况,对照Pattern
常见模式
Observer
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Observer MSC
TBD
Observer structure & principles
设计原则
松耦合
接口明确, SRP
变化被独立出来(主题的状态,观察者的类型和数目)
动态组合(动态添加观察者)
Examples
Imaging ………
Java
subject::setListener(observer* pObs);
Adapter
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Structure and Principles
TBD
Example
Could you find examples?
Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Example
Imaging: How to design a ScrollBorderTextView
Decorator Structure & Principles
Open-Close
Composite vs. inheritance
Examples
Question
TextView GUI component and we want to add different kinds of borders and scrollbars to it.
Suppose we have three types of borders:
Plain, 3D, Fancy
l And two types of scrollbars:Horizontal, Vertical
Solution1
Solution2
solution2
Composite
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Composite Structure & design principle
违反了类单一责任原则。Tradeoff
Transparency (Client don’t know the difference b/w Leaf & Composite)
Example
Example-cont
Class Diagram
Proxy
Provide a surrogate or placeholder for another object to control access to it.
Proxy Structure
TBD
Example
Factory Method
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Factory Method Structure & design principle
DIP(依赖倒置)
消费者与生产者解耦
Examples
public interface ImageReader
{
public DecodedImage getDecodedImage();
}
public class GifReader implements ImageReader
{
public GifReader( InputStream in )
{
// check that it's a gif, throw exception if it's not, then if it is
// decode it.
}
public DecodedImage getDecodedImage()
{
return decodedImage;
}
}
public class JpegReader implements ImageReader
{
//....
}
public class ImageReaderFactory
{
public static ImageReader getImageReader( InputStream is )
{
int imageType = figureOutImageType( is );
switch( imageType )
{
case ImageReaderFactory.GIF:
return new GifReader( is );
case ImageReaderFactory.JPEG:
return new JpegReader( is );
// etc.
}
}
}
Example2
Singleton
Ensure a class only has one instance, and provide a global point of access to it.
Singleton Structure
Problem:
Multi thread
Singleton destruction
State
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Example
One:
Operation1()
{
if(state == STATE1){};
if(state == STATE2){};
……
}
Operation2()
{
}
Two:
Static stateOperationTable[][]=
{
operation1(); // STATE1
operation2(); // STATE1
…
operation1(); // STATE2
operation2(); // STATE2
…
}
Handler()
{
…
stateOperationTable[state].operationx()
}
……
违反了什么原则?
Open-Close
Information hiding
容易改变的是:
增加状态
增加动作
Structure & principles
Open-Close, 容易添加状态,而不用更改其他状态和Context,其他的已有状态封闭
Chain
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
Structure & principles
Example
References
Design Patterns Elements of Reusable Object-Oriented Software