Comparison between design patterns

First, the relationship between the design pattern and Comparative

Examples 1.1 Single mode and a factory mode

The actual business code, factory class will usually designed as a single embodiment.

1.2 Strategy mode and the factory pattern

1. factory model contains factory method pattern and abstract factory pattern is a creational pattern, strategy pattern belongs to the type of model.

2. The main purpose of the factory model is a good package to create a logical, policy-mode receiver factory to create good object, in order to achieve different behavior.

1.3 Strategy mode and delegated mode

1, the policy mode is a form of internal delegation model to achieve, whether the result of the policy of mutual alternative mode of attention.

2, delegation model is more concerned about the process of distribution and scheduling.

Template Method Pattern factory and factory method pattern template method is a special implementation method.

        Factory Method Pattern Template Method Pattern

For create factory method pattern in terms of () method, the equivalent of a template method pattern is only one step. This is a step to the sub-class to implement. The template approach it,

The needHomework () method and checkHomework () method to implement the subclass, needHomework () method and checkHomework () method and a step belonging to a certain class and the parent can not be changed.

1.4 Template Method and Strategy Mode

1, templates and strategies model has encapsulation algorithm.

2, the policy mode is a mode different algorithms can be interchanged without affecting the use of client application layer.

3, the template method is an algorithm for the definition of the process, there are some subtle differences in part to the sub-class implementation.

4, the template method pattern can not change the algorithm processes, policies can change the algorithm flow pattern and can be replaced. Strategy mode is often used instead of if ... else ... such as conditional branching statements.

 

 

 Strategy Template Method pattern

1, WechatPay, JDPay, AliPay to the user is selected and another alternative solution. JdbcTemplate of subclasses and can not be replaced with each other.

queryBalance 2 Strategy Mode () method, although there are calls in pay () method, but this is only for procedural logic robustness considerations.

Users can call queryBalance () method independent. The mapRow Template Method mode () method must be called only after obtaining ResultSet, otherwise it does not make sense.

1.5 decorative pattern and static proxy mode

1, the decorator pattern focus is dynamically added to the object method, and pay more attention to control access to the proxy object. 2, the proxy mode is generally creates an instance of a proxy object in the proxy class,

The decorator pattern is usually configured as a decorator parameter.

Proxy mode decorator pattern decoration and brokers who are holding each other although references, but logic processing center of gravity is not the same.

1.6 decorative pattern and adapter pattern

1, the decorative pattern and the adapter mode belong packing mode (Wrapper Pattern).

2, the decorator pattern can be implemented in the same decorators interface or inherit the decorator as its subclass of the adapter and the adapter can implement different interfaces.

 

 Adapter decorator pattern

Decorator and all of the adapters and extended SiginService packaging, is an implementation form of the decorator pattern. But the decorators need to meet is-a relationship of OOP,

Regardless of the package have a common parent class. The adapter is compatible mainly to solve the problem, not necessarily to unify the parent class, and the figure above LoginAdapter

RegistAdapter is compatible with two different functional classes, but the auto-login after RegistForQQAdapter need to register, and therefore not only inherited the RegistAdpter also inherited the LoginAdapter.

Design Patterns

Sentence induction

For example

Factory pattern (Factory)

Only responsible for the results, the package creation process.

BeanFactory、Calender

Singleton pattern (the Singleton)

Guarantee unique.

ApplicationContext、Calender

Prototype model (Prototype)

Pull a monkey hair, blowing ten million.

ArrayList、PrototypeBean

Agent mode (Proxy)

To find people to do things, enhanced responsibilities.

ProxyFactoryBean、

JdkDynamicAopProxy、CglibAopProxy

委派模式(Delegate)

干活算你的(普通员工),功劳算我的(项目经理)。

DispatcherServlet、

BeanDefinitionParserDelegate

策略模式(Strategy)

用户选择,结果统一。

InstantiationStrategy

模板模式(Template)

流程标准化,自己实现定制。

JdbcTemplate、HttpServlet

适配器模式(Adapter)

兼容转换头。

AdvisorAdapter、HandlerAdapter

装饰器模式(Decorator)

包装,同宗同源。

BufferedReader、InputStream、

 

 

OutputStream、

HttpHeadResponseDecorator

观察者模式(Observer)

任务完成时通知。

ContextLoaderListener

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/cxyyh/p/11456510.html