1、AbstractFactory VS Builder
Builder生成器模式和Abstract Factory抽象工厂模式在抽象的角度来看非常相似。但是Builder生成器模式关注如何组装起一个对象,而抽象工厂关注实例哪个类。Builder生成器模式将构造对象的算法抽象出来放在对象的控制器Director中,Director负责按照构造算法一步一步地实例对象,对象的控制器Director不一定要实现固定的接口。客户端不负责对象的实例,由Builder模式中的对象的控制器Director来决定实例对象的各组成部分。
The Builder and Abstract Factory patterns are similar in that they both look at construction at an abstract level. However, the Builder pattern is concerned with how a single object is made up by the different factories, whereas the Abstract Factory pattern is concerned with what products are made. The Builder pattern abstracts the algorithm for construction by including the concept of a director. The director is responsible for itemizing the steps and calls on builders to fulfill them. Directors do not have to conform to an interface.
2、Chain of Responsibility VS Command
责任链模式和命令模式都是将发送方和接受方解耦,这样增强了系统的层次和重用性。责任链模式通过在可能的接受方传递请求来解耦,而命令模式是通过将请求封装成对象。
A similarity between the Chain of Responsibility and the Command patterns is that they decouple senders and receivers, thus improving the layering and reusability of a system. The Chain of Responsibility pattern supports decoupling by passing a request between potential receivers, whereas the Command pattern supports using a command object to encapsulate a request.
3、Strategy VS State VS TemplateMethod
策略对象封装算法,状态对象封装了某种状态下的行为或状态的转变。
两种模式都使用了多态,都定义了父接口或抽象类和实现了方法的一系列子类,两种模式都用上下文来维护或决定状态的转变。所以最大的不同是封装算法到策略模式,而封装状态在状态模式。模版方法模式和策略模式非常类似都是基于算法。不同于策略模式模版方法中在算法的各个步骤中的一部分是被推到子类中实现的。
There are considerable similarities between the Strategy and State patterns, but the main difference is one of intent:
• A Strategy object encapsulates an algorithm
• A State object encapsulates a state-dependent behavior (and possibly state transitions)
Both patterns are concerned with polymorphism. Both patterns define a parent interface or abstract class and a series of subclasses that implement the methods therein.And both patterns have a context that they maintain and use to decide on state transitions. So, the biggest difference between the patterns is that we encapsulate an algorithm into strategy classes in the Strategy pattern, but we encapsulate state into state classes in the State pattern.
The Template Method pattern is more like the Strategy pattern in that it is algorithm based. The steps of the algorithm are specified in the Template Method and some are deferred to domain classes.