Factory pattern Summary

A role

Factory mode of action: allow users to separate the creator, in order to achieve the purpose of decoupling. Popular terms is not new object, but use the factory to create.

Two simple factory

1. The most common, simple, powerful

2. does not comply with the principle of opening and closing

Source

Defined interfaces

Define the implementation class

 

 Factory class definitions, forgotten to put a static

 

 The definition of the caller

 

 These are simple plants, because the factory class to add static, also known as static factory methods.

 

Third, the factory method

To overcome the problem of simple extension of the factory: If you want to increase the implementation class, such as to add Mercedes to re-modify the code does not comply with the principle of opening and closing, so the design of the factory method pattern

1. difference

The new factory interface, so you can plant it can solve the above problems by a new class

New factory interface

 

 Realization of class factory

 

 The caller should be changed accordingly

 

 By the above changes, then you want to add something, you can achieve class and the actual implementation class can be completed by the new plant.

 

Guess you like

Origin www.cnblogs.com/funimei/p/11489306.html