GoF设计模式意图与类图

 


 

 

Design Pattern Space

The simplest and most common patterns:

 

Abstract Factory

Adapter

Composite

Decorator

Factory Method

Observer

Strategy

Template Method

It's hard to find an object-oriented system that doesn't use at least a couple of these patterns, and large systems use nearly all of them. This subset will help you understand design patterns in particular and good object-oriented design in general


 

 

Intents and diagrams of 23 design patterns

*Abstract Factory (CCOR 1, 3, 4, 6)选择产品簇的实现

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

CCOR

1. Creating an object by specifying a class explicitly.

3. Dependence on hardware and software platform.

4. Dependence on object representations or implementations.

6. Tight coupling.

 


 

 

*Adapter (CCOR 8)转换匹配,复用功能

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

CCOR

8. Inability to alter classes conveniently.

 


 

 

Bridge (CCOR 3, 4, 6, 7)分离抽象和实现

Decouple an abstraction from its implementation so that the two can vary independently.

CCOR

3. Dependence on hardware and software platform.

4. Dependence on object representations or implementations.

6. Tight coupling.

7. Extending functionality by subclassing.

 


 

 

Builder (CCOR 5)分离整体构建算法和部件构造

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

CCOR

5. Algorithmic dependencies.

 


 

 

Chain of Responsibility (CCOR 2, 6, 7)分离职责,动态组合

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.

CCOR

2. Dependence on specific operations.

6. Tight coupling.

7. Extending functionality by subclassing.

 


 

 

Command (CCOR 2, 6)封装请求

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

CCOR

2. Dependence on specific operations.

6. Tight coupling.

 


 

 

*Composite (CCOR 7)统一叶子对象和组合对象

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clientstreat individual objects and compositions of objects uniformly.

CCOR

7. Extending functionality by subclassing.

 


 

 

*Decorator (CCOR 7, 8)动态组合

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

CCOR

7. Extending functionality by subclassing.

8. Inability to alter classes conveniently.

 


 

 

Facade (CCOR 6)封装交互,简化调用

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

CCOR

6. Tight coupling.

 


 

 

*Factory Method (CCOR 1)延迟到子类来选择实现

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Methodlets a class defer instantiation to subclasses.

CCOR

1. Creating an object by specifying a class explicitly.

 


 

 

Flyweight分离与共享

Use sharing to support large numbers of fine-grained objects efficiently.


 

 

Interpreter分离实现,解释执行

Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.


 

 

Iterator (CCOR 5)控制访问聚合对象中的元素

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

CCOR

5. Algorithmic dependencies.

 


 

 

Mediator (CCOR 6)封装交互

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

CCOR

6. Tight coupling.

 


 

 

Memento (CCOR 4)保存和恢复内部状态

Without violating encapsulation,capture and externalize an object's internal state so that the object can be restored to this state later.

CCOR

4. Dependence on object representations or implementations.

 


 

 

*Observer (CCOR 6, 7)触发联动

Define a one-to-many dependency between objects so that when one object changes state,all its dependents are notified and updated automatically.

CCOR

6. Tight coupling.

7. Extending functionality by subclassing.

 


 

 

Prototype (CCOR 1)克隆生成对象

Specify the kinds of objects to create using a prototypical instance, andcreate new objects by copying this prototype.

CCOR

1. Creating an object by specifying a class explicitly.

 


 

 

Proxy (CCOR 4)控制对象访问

Provide a surrogate or placeholder for another object to control access to it.

CCOR

4. Dependence on object representations or implementations.

 


 

 

Singleton控制实例数目

Ensure a class only has one instance, and provide a global point of access to it.


 

 

State根据状态来分离和选择行为

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.


 

 

*Strategy (CCOR 5, 7)分离算法,选择实现

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

CCOR

5. Algorithmic dependencies.

7. Extending functionality by subclassing.

 


 

 

*Template Method (CCOR 5)固定算法骨架

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

CCOR

5. Algorithmic dependencies.

 


 

 

Visitor (CCOR 5, 8)预留通路,回调实现

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

CCOR:      5. Algorithmic dependencies.

8. Inability to alter classes conveniently.

 

Design Pattern Relationships


 

 

Design Aspects that Design Patterns Let You Vary


 

 

 

Some common causes of redesign (CCOR)

Common causes of redesign

Design patterns

Abbr.

1. Creating an object by specifying a class explicitly.

Abstract Factory , Factory Method , Prototype

FAP

2. Dependence on specific operations.

Chain of Responsibility , Command

CC

3. Dependence on hardware and software platform.

Abstract Factory , Bridge

BA

4. Dependence on object representations or implementations.

Abstract Factory , Bridge , Memento , Proxy

BAPM

5. Algorithmic dependencies.

Builder , Iterator , Strategy , Template Method , Visitor

BITVS

6. Tight coupling.

Abstract Factory , Bridge , Chain of Responsibility , Command , Facade , Mediator ,Observer

BACCOFM

7. Extending functionality by subclassing.

Bridge , Chain of Responsibility ,Composite , Decorator , Observer , Strategy

BCCDOS

8. Inability to alter classes conveniently.

Adapter , Decorator , Visitor

ADV

 

Here are some common causes of redesign along with the design pattern(s) that address them:

 

1. Creating an object by specifying a class explicitly. Specifying a class name when you create an object commits you to a particular implementation instead of a particular interface. This commitment can complicate future changes. To avoid it, create objects indirectly.

Design patterns: Abstract Factory , Factory Method , Prototype .

 

2. Dependence on specific operations. When you specify a particular operation, you commit to one way of satisfying a request. By avoiding hard-coded requests, you make it easier to change the way a request gets satisfied both at compile-time and at run-time.

Design patterns: Chain of Responsibility , Command .

 

3. Dependence on hardware and software platform. External operating system interfaces and application programming interfaces (APIs) are different on different hardware and software platforms. Software that depends on a particular platform will be harder to port to other platforms. It may even be difficult to keep it up to date on its native platform. It's important therefore to design your system to limit its platform dependencies.

Design patterns: Abstract Factory , Bridge .

 

4. Dependence on object representations or implementations. Clients that know how an object is represented, stored, located, or implemented might need to be changed when the object changes. Hiding this information from clients keeps changes from cascading.

Design patterns: Abstract Factory , Bridge , Memento , Proxy .

 

5. Algorithmic dependencies. Algorithms are often extended, optimized, and replaced during development and reuse. Objects that depend on an algorithm will have to change when the algorithm changes. Therefore algorithms that are likely to change should be isolated.

Design patterns: Builder , Iterator , Strategy , Template Method , Visitor .

 

6. Tight coupling. Classes that are tightly coupled are hard to reuse in isolation, since they depend on each other. Tight coupling leads to monolithic systems, where you can't change or remove a class without understanding and changing many other classes. The system becomes a dense mass that's hard to learn, port, and maintain.

Loose coupling increases the probability that a class can be reused by itself and that a system can be learned, ported, modified, and extended more easily. Design patterns use techniques such as abstract coupling and layering to promote loosely coupled systems.

 

Design patterns: Abstract Factory , Bridge , Chain of Responsibility , Command , Facade , Mediator , Observer .

 

7. Extending functionality by subclassing. Customizing an object by subclassing often isn't easy. Every new class has a fixed implementation overhead (initialization, finalization, etc.). Defining a subclass also requires an in-depth understanding of the parent class. For example, overriding one operation might require overriding another. An overridden operation might be required to call an inherited operation. And subclassing can lead to an explosion of classes, because you might have to introduce many new subclasses for even a simple extension.

Object composition in general and delegation in particular provide flexible alternatives to inheritance for combining behavior. New functionality can be added to an application by composing existing objects in new ways rather than by defining new subclasses of existing classes. On the other hand, heavy use of object composition can make designs harder to understand. Many design patterns produce designs in which you can introduce customized functionality just by defining one subclass and composing its instances with existing ones.

 

Design patterns: Bridge , Chain of Responsibility , Composite , Decorator , Observer , Strategy .

 

8. Inability to alter classes conveniently. Sometimes you have to modify a class that can't be modified conveniently. Perhaps you need the source code and don't have it (as may be the case with a commercial class library). Or maybe any change would require modifying lots of existing subclasses. Design patterns offer ways to modify classes in such circumstances.

Design patterns: Adapter , Decorator , Visitor .


 

 

 

Intents of 23 design patterns

Common causes of redesign

Design patterns

Abbr.

1. Creating an object by specifying a class explicitly.

Abstract Factory:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

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. 

Prototype:

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

FAP

2. Dependence on specific operations.

Chain of Responsibility:

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.  

Command:

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

CC

3. Dependence on hardware and software platform.

Abstract Factory:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.  

Bridge:

Decouple an abstraction from its implementation so that the two can vary independently.

BA

4. Dependence on object representations or implementations.

Abstract Factory:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.  

Bridge:

Decouple an abstraction from its implementation so that the two can vary independently.  

Memento:

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.  

Proxy:

Provide a surrogate or placeholder for another object to control access to it.

BAPM

5. Algorithmic dependencies.

Builder:

Separate the construction of a complex object from its representation so that the same construction process can create different representations.  

Iterator:

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.  

Strategy:

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. 

Template Method:

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.  

Visitor:

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

BITVS

6. Tight coupling.

Abstract Factory:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.  

Bridge:

Decouple an abstraction from its implementation so that the two can vary independently.  

Chain of Responsibility:

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.  

Command:

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.  

Façade:

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.  

Mediator:

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.  

Observer:

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

BACCOFM

7. Extending functionality by subclassing.

Bridge:

Decouple an abstraction from its implementation so that the two can vary independently.  

Chain of Responsibility:

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.  

Composite:

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.  

Decorator:

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. 

Observer:

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 

Strategy:

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

BCCDOS

8. Inability to alter classes conveniently.

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. 

Decorator:

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.  

Visitor:

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

ADV

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值