观察者模式
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
“观察者模式”:__被观察者对象__通过一个列表维护__观察者对象__的行为(方法),当__被观察者对象__状态改变时会主动呼叫(调用)__观察者对象__行为(方法)。
PS:观察者模式有时又被称为发布(publish)-订阅(Subscribe)模式、模型-视图(View)模式、源-收听者(Listener)模式或从属者模式。
1 | ''' |
中介者模式
The essence of the Mediator Pattern is to “define an object that encapsulates how a set of objects interact“. It promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to be varied independently. Client classes can use the mediator to send messages to other clients, and can receive messages from other clients via an event on the mediator class.
Mediator - defines the interface for communication between Colleague objects
ConcreteMediator - implements the Mediator interface and coordinates communication between Colleague objects. It is aware of all of the Colleagues and their purposes with regards to inter-communication.
Colleague - defines the interface for communication with other Colleagues
ConcreteColleague - implements the Colleague interface and communicates with other Colleagues through its Mediator
“中介者模式”:把对象(A)与对象(B)之间的__直接__通信关系,变成对象(A)与对象(B)通过__中介对象__来通信的__间接__关系,从而松散了对象(A)与对象(B)的耦合度;实现上必须“__你中有我,我中有你__”。(PS:如果参与通信的对象很多的话就会增加中介对象的复杂度。)
代理模式
A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy, extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.
Notes:
- A proxy may hide information about the real object to the client.
- A proxy may perform optimization like on demand loading.
- A proxy may do additional house-keeping job like audit tasks.
- Proxy design pattern is also known as surrogate design pattern.
“代理模式”:在__客户端__和__目标对象__之间建立一个__代理对象__,客户端通过代理对象来访问目标对象;这样可以起到__保护__或__欺骗__目标对象的作用。
1 | """ |
外观模式
A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can
- make a software library easier to use, understand and test, since the facade has convenient methods for common tasks,
- make the library more readable, for the same reason,
- reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system,
- wrap a poorly designed collection of APIs with a single well-designed API.
The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class that contains a set of members required by client. These members access the system on behalf of the facade client and hide the implementation details. The facade pattern is typically used when:
- a simple interface is required to access a complex system,
- a system is very complex or difficult to understand,
- an entry point is needed to each level of layered software, or
- the abstractions and implementations of a subsystem are tightly coupled.
“外观模式”:用函数或类进行封装实现,对外提供一个__简洁明了__的接口。
1 | """ |
策略模式
“策略模式”:__定义了一系列的算法并提供运行时动态更改算法的能力,保证了客户端代码的简洁性__。
实现方式:
- 创建__策略接口__;
- 创建多个__策略类__继承__策略接口__并通过不同算法实现接口中声明的方法;
- 创建一个__消费者类__,它必须存储一个__策略对象__的引用并对外提供一个可修改该引用的方法;
- 在执行环境的上下文通过动态的更改__消费者对象__中存储的__策略对象__引用来达到动态更改算法的目的;
装饰者模式
The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new Decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:
- Subclass the original Component class into a Decorator class (see UML diagram);
- In the Decorator class, add a Component pointer as a field;
- In the Decorator class, pass a Component to the Decorator constructor to initialize the Component pointer;
- In the Decorator class, forward all Component methods to the Component pointer;
- In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.