[Design Mode] design pattern notes

Design Patterns

Simple factory pattern (Simple Factory)

Add a factory class, a method of adding the factory to create an instance of the class, in accordance with different parameters, initialization of different instances.

foundation

The sample code

Strategy Mode (Strategy)

Lack of simple plant

面向对象的编程,并不是类越多越好, 类的划分是为了封装, 但分类的基础是抽象, 具有相同属性和功能的对象的抽象集合才是类. So some Action looks just act different, but the abstract analysis algorithm is the same, so it should be abstracted as a class.

Simple factory pattern design abstract base class, the actual operation of the other classes inherit this base class is then used to initialize the factory class instance of the operation.

但是这种方式只能解决操作类的实例创建问题, 而且由于工厂本身包括了所有的实例创建方式. 所以每次维护和扩展都要改动这个工厂, 易导致代码需要重新部署, 面对算法的时常变动, 应该有个更合理的模式.

The introduction of the strategy pattern

定义了算法家族, 分别封装起来, 让他们之间可以互相替换, 此模式让算法的变化不影响使用算法的客户.

The advantages of the strategy pattern

The introduction of the strategy pattern allows client only needs to understand context, the context and the client is responsible for the communication of the Strategy category corresponding initialization. Then called by the context of the implementation of the corresponding class method of operation, the results can be returned to the client. This ways on decoupling is also a lot of sense.

Another advantage of the strategy pattern is to facilitate the actual operation of the unit test class. Because each algorithm has its own class, so it can be unit tested through their own interfaces.

Packaging strategy mode changes. Different algorithms into a different class in the actual operation, performed separately abstracted common method is its greatest strength.

Examples

GitLink

Decorative patterns (Decorator)

To an object dynamically add some additional responsibilities, increased functionality, the decorative pattern is more flexible than a subclass.

Principles of Design Patterns

Single Responsibility Principle

For a class, you should have one and only one reason for it to change.

If a class too responsibilities, functions equivalent to those coupled together, a change in the duty may weaken or suppress the ability to perform other duties, so the code of the reconstructed time to be separated under the responsibility of the class.

Open - Closed Principle

For software, it should be expanded and not be modified. In doing system is the same, can not expect the demand will initially determined, since demand is changing, so in the face of changes in demand, how to design ? stable software used here is thinking:

多扩展, 少修改. Is in the design category, try to make this class do good enough, and other new demand, and then add a class on the line.

Inversion of Control principles

Also known as the IOC, dependency inversion, may be used. The actual nature of low coupling, high cohesion.

  • High-level modules should not depend on low-level modules Both should depend on abstractions.
  • Abstract should not rely on the details, the details should depend on the abstract.

Guess you like

Origin www.cnblogs.com/it-dennis/p/10621216.html