First, Simple Factory (Factory ) mode
The simple Factory pattern returns an instance of a class in several possible classes, based on the data supplied to it. Typically, the class it returns has a common parent class and a public method.
Simple Factory mode is not actually a member of the Gof 23 design patterns.
Second, Simple Factory (Factory ) mode role and Structure
Factory class role creator (Lightsimplefactory): The factory class creates a product object under direct control of the client (create method).
Abstract Product role Product (light): defines the parent class of objects created by simple factories or interfaces that they collectively own. Can be a class, abstract class, or interface.
Specific product role concreteproduct (Bulblight, tubelight): defines the object that the factory specifically processes.
Third, Simple Factory (Factory ) Pattern Evolution
1) Simple Factory model Evolution (i)
In addition to the above usage, in some cases simple factory can have abstract product role playing, an abstract product class is also a sub-class factory.
2) Simple Factory model Evolution (II.)
All three characters are merged: similar to singleton mode (Singleton), but different.
Four, a bit of a disadvantage
The factory class contains the necessary judgment logic to decide when to create an instance of the product class, and the client can dispense with the responsibility to create the product object directly, but only "consume" the product. The simple factory model achieves the separation of responsibilities through this approach.
When the product has complex multilayer hierarchy structure, the factory class only own, Status quo, is the disadvantage of the pattern. Because the factory class centralizes all product creation logic, the entire system is affected once it does not work properly.
At the same time, the system is difficult to expand, once adding new products will have to modify the factory logic, may cause the factory logic is too complex. In addition, the simple factory pattern typically uses a static factory approach, which makes it impossible to subclass inheritance, resulting in a factory role that cannot form an inheritance-based hierarchy
Source code:http://pan.baidu.com/s/1qWl8T2G Password: bbwx
Simple pattern Mode