design pattern

1. design patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.
2. program to interface, not implementation.
3. in general, a pattern has four key elements: pattern name, problem, solution, consequences.
4. inheritance breaks encapsulation. the less the dependence on the implementation of a class is, the better the encapsulation of the class is.
5. favor object composition over class inheritance. get all the functionality through composing existed components as possible as we can.
6.delegation: a way of making composition as powerful for reuse as inheritance. a receiving object defers the requests to it delegate.
7. there is disparity between run-time and compile-time structures, so it is quite clear that code will not reveal everything about how system works
8. when you use a  framework, you reuse the main body and write the code it calls. Frameworks reuses the architecture in the specific domains.(p27)
factory:
1. client create instances through the interface provided by the abstract factory class and the detailed implementation of the interface is hidden in the concrete subclass of abstract factory class
2. i think abstract factory is basic same with factory method. factory method provides a interface to create instances in the subclass while abstract factory extract  common interfaces so that clients do not depend on the concrete subclasses but just on the interface.

factory method is to define some creational methods and change the implementation through inheritance.
abstract factory is to reply on object composition. usually we will define a abstract factory class and define some subclass of the factory class. factory class will contain some common interfaces  used to create object. client will have to just call the interface and do not have to know its implementation which is hidden in the subclass.

comment:
usually abstract factory is implemented with factory method.
factory method is defined directly in the class that need instantiate objects,while abstract factory is passed into class as a parameter.
singleton pattern:
1. There are two conditions we must meet in implementing singleton pattern
   A. We must make sure that only one instance of class can be created.    //constructor must be protected or private
   B. We must make sure that we can access to the only one instance.      //operation to access instance must be static. and data member must be static too. and data membe
2. Singleton class can be subclassed , but I still cannot find how to use it.
adapter:(interface is different between adapter and adpatee)
convert the interface of a class into another interface clients expect, otherwise they cannot work together because of incompatible interfaces.
there are two method, inheritance and object composition.
inheritance: public inherit from target and private inherit from adaptee.
object composition: compose a object that refers to adaptee or its subclass.

bridge
decouple abstraction and implementation. Implementation is separated from abstraction in different class hierarchy. that is we do not use inheritance but use object composition.
implementation is not permanently bound to its interface. if we use inheritance then the implementation is bound permanently.
comment: usually we define an abstract class and subclass it to define some concrete implementations. in this way implementations is bound to the interface permanently. if we can decouple interface and its implementation then interface and its implementations can vary easily with disturbing each other.
composite:
1. client can treat primitive object and composite object uniformly.
2. compose objects into tree structures to represent part-whole hierarchies. It means some subclass is a leaf and some subclass is a container that composed by other subclass.

decorator:
1. add responsibilities to individual objects, not to an entire class.
2.inheritance will add functionality to entire class, while to enclose the component in another object is much more flexible.the enclosing object is called decorator.
3. interface conformance.
comment:
implemented with composite
facade:
provide a unified interface to a set of interfaces in a subsystem.
clients communicate with subsystem by sending requests to facade which is responsible for sending requests to the corresponding subsystem object.
facade should know how to transfer requests from clients to subclass objects.
often facade is implemented as singleton
proxy:serve as a surrogate or placeholder for another object to control access to it.
proxyclass and real class are both inherited from same abstract class.(the same paradigm is used in adapter,bridge.composite.etc.) it provides a good profile in that client can treat real class and proxyclass uniformly(or adapter,bridge,composite,etc).
actually smarter pointer is a kind of proxy.//overload member access operator

Comment:
1. proxy must provide the identical interface with subject it works for.
2. proxy is quite similar with decorator in structure but they have different purposes. decorator is to add some functionality while proxy is to control the access.

1. Structural pattern majorly focus on relationship between classes and between objects. it usually combine class and object with inheritance and object composition to meet some requirement.
2. behavioral pattern focuses on algorithm and responsibility distribution
3. key point of design pattern is how to decouple the relevant classes

command:
1. encapsulate request in the object so that client can know how to issue the operation even though it does not know how to carry out the requests.
2. encapsulate request in the object and object provides a interface to execute requests. client does not have to know how to carry out requests but it just need know how to issue the request through the interface provided by the object.
3. command model can be implemented with callback function in a procedural language.
4 command decouples the object that invokes the operation from the one that know how to perform it. (invoker and receiver)

iterator:
1. provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
2. we can think iterator as a encapsulation of pointer in c++
3. separating the traversal mechanism from the list object let us define iterators for different traversal policies without enumerating them in the list interface.
4. good idea!!. aggregate class and iterator class is coupled because they are one for one. In order to decouple them we can use factory method in the concrete subclass of aggregate class to create a concrete subclass of iterator which is responsible for iterating the aggregate.

Mediator:
1. A mediator is responsible for controlling and coordinating the interactions of a groups of objects.
Observer:
1. define a one-to-many dependencies between objects so that when one object changes state all the dependencies are notified and updated automatically.
2. publish and subscribe
3. observers can change state of subject and retrieve state of subject.

State:
1. object's behavior diff from its state. separate object state and object itself and define the same interfaces to enable communication between them. different state object holds the same interface but behavior is different.
2. state pattern put state related behaviors into one object.

strategy:
1. A family of related algorithms and encapsulate each one and make them interchangeable. strategy lets the algorithm vary independently from clients that use it.

template method:
a template method defines an algorithm in terms of abstraction operations and subclass overrides them to provide concrete implementations.
abstract common part of algorithm into one class to avoid code duplication
Inverted control: Hollywood principle. "don't call us; we will call you".
template operation is defined non virtual usually.
發佈了81 篇原創文章 · 獲贊 22 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章