Thursday, November 08, 2007

Desing Pattern

 

  • Creational
    • Facotory Method
    • Abstract Factory
      • When we need to create a object which can be decided in the subclass , usually we will use Facotry Method.(Inhertiance will be used)
      • When we want to create mutiple objects which are related and the system only need one of them, usually we will use Abstract Factory.(Composition will be used)
      • Abstract Factory需要处理的是“一系列相互依赖”的产品的创建,即抽象工厂创建的是一组产品;而Factory Method需要处理的单个产品的创建工作。
        当一个类不知道他所必需创建对象的类或一个类希望有子类指定他所创建的类型时(也就是说一个类中的某个对象需要由子类来创建时),此时我们使用Factory Method
        当系统有多于一个的产品组合,而系统只需要使用其中某一产品组合,此时我们一般使用Abstract Factory
      • 分析了上面的优缺点,当我们在遇到如下情况时,应当考虑使用抽象工厂模式:
        1、一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的;
        2、这个系统有多于一个的产品组合,而系统只需要使用其中某一产品组合;
        3、同属于同一个产品组合的产品是在一起使用的,这一约束必须在系统的设计中体现出来;
        4、系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于具体实现;
  • Structure : to compose bigger structure
    • Decorator
  • Behaviour : Distribute the  algorithim and responsibility between different objects .
    • Stragtegy

 

 

Adapter   VS   Decorator VS Facade

 Adapter: Class adapter(using mutiple inheritance); Object adapter(Composition)

  • They are all wrappers using composition(Adapter ,object adapter)
  • Adapter wraps an object to change its interface
  • Decorator wraps an object to add new behaviours and responsibilites dynamically 
  • Facadie wraps a collection of objects to simplify and get a uniformed handling method.

Bridge    VS   Stragtegy VS State

 

Strategy:是一个锥把可以使用多个锥头;
Bridge:是不同锥把可以使用多个锥头;
当Bridge的左边只有一个时就退化为Strategy了!
____________________
这才说到点上了。不只是context 不一样,表现就是不一样的!

 

Composite VS Decorator VS Proxy

 

 Decorator VS Proction Proxy: The selection is depends on the intent.

for Proction Proxy , the structure is same as Decorator.

  • A Protection Proxy protects an object
  • Decorator adds behaviour to an object

 

Decorator VS Stragtegy : They all can add behavior to an object. The selection is depends on the intent.

  • Decorator focus on adding behaviors dynamically
    • Can be used for adding behaviors for an instance of object (not an object) which mens the object code or structure can not be changed
    • Can not be shared by other instance of object
    • The instantiation must be transparent which means
      • (right)object o= new Decorator(new object() ) ,
      • (wrong) Decortor o= new Decorator()
  • Stragtegy
    • The object will always have the stragtegy object
    • Can be shared by other object

 Template && Factory

  • Factory can be used without Template
  • Template usually call Factory Methods
  • Template just like "Pull Up Method To Super Class"

Decorator VS Proxy

  • They all have a pointer with other object
  • Decorator is using the object to add new behavior
  • Proxy is using the object to do the real work
    • when it is difficult to access the object , using proxy to access. 

Adapter VS Proxy

  • They all use composition to have another object  and forward request to it.
  • Adapter changes the interface of object
  • Proxy implement the same interface


Builder VS Composite VS Collectiing Parameter

  • In Composite , can use builder pattern to create the tree(Encapsulate Composite with builder)
  • In Composite, can aslo use collecting parameter to Move Accumulation to Collecting Parameter
  • For using builder in Compoiste, will build mutiple objects like Nodes (Nodesbuilder.build(parent);
  • For using collecting parameter in Composite, will build single object like a xml string(stringbuilder.add(starttag))

 

Vistor:

  • add new feature for a composition of objects but not want to add it to each one in the composition. For example , have three objects :Clerk, Boss, Directory(they have a common interface :Employee). Now we want to add addincome feature for these three classes. But we don't want to add addincome function in each of these three.
  • So we can use Vistor pattern.
    • Create a AddIncomeVistor class.
      • AddIncome(Employee)
    • In these three class , write a Accept(AddIncomeVistor) method: AddincomeVisotr.AddIncome(this);
  • Now we can call Element.accept(AddIncomeVistor) to add this new feature  without making changes in three objects.
  • Vistor pattern break "Encapsulation" and use "Double-Dispatch".
  • 访问者模式有如下的优点:

    1. 访问者模式使得增加新的操作变得很容易。如果一些操作依赖于一个复杂的结构对象的话,那么一般而言,增加新的操作会很复杂。而使用访问者模式,增加新的操作就意味着增加一个新的访问者类(Vistor),因此,变得很容易。
    2. 访问者模式将有关的行为集中到一个访问者对象中,而不是分散到一个个的节点类中。
    3. 访问者模式可以跨过几个类的等级结构访问属于不同的等级结构的成员类。迭代子只能访问属于同一个类型等级结构的成员对象,而不能访问属于不同等级结构的对象。访问者模式可以做到这一点。
    4. 积累状态。每一个单独的访问者对象都集中了相关的行为,从而也就可以在访问的过程中将执行操作的状态积累在自己内部,而不是分散到很多的节点对象中。这是有益于系统维护的优点。

    访问者模式有如下的缺点:

    1. 增加新的节点类变得很困难。每增加一个新的节点都意味着要在抽象访问者角色中增加一个新的抽象操作,并在每一个具体访问者类中增加相应的具体操作。
    2. 破坏封装。访问者模式要求访问者对象访问并调用每一个节点对象的操作,这隐含了一个对所有节点对象的要求:它们必须暴露一些自己的操作和内部状态。不然,访问者的访问就变得没有意义。由于访问者对象自己会积累访问操作所需的状态,从而使这些状态不再存储在节点对象中,这也是破坏封装的。

0 Comments:

Post a Comment

<< Home