Original article: http://www.cnblogs.com/zhenyulu/articles/36462.html
The factory mode is dedicated to instantiating a large number of classes with common interfaces. The factory mode dynamically determines which class to instantiate without having to know which class to instantiate each time in advance. The factory model has the following forms:
Simple factory Mode
Factory method
Abstract Factory Mode
1. Simple factory Mode
The simple factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the class it returns has a public parent class and a public method.
The simple factory mode is not actually one of the 23 gof design patterns.
Ii. Simple factory mode roles and structures:
Factory role creator (lightsimplefactory): The factory class creates product objects under the direct control of the client (Create method.
Abstract Product role light: defines the parent class or interfaces of objects created in a simple factory. It can be a class, abstract class, or interface.
Product role concreteproduct (bulblight, tubelight): defines the specific objects processed by the factory.
3. program example:
Using system; </P> <p> public abstract class light <br/> {<br/> public abstract void turnon (); <br/> public abstract void turnoff (); <br/>}</P> <p> public class bulblight: light <br/>{< br/> Public override void turnon () <br/>{< br/> console. writeline ("Bulb light is turned on"); <br/>}</P> <p> Public override void turnoff () <br/>{< br/> console. writeline ("Bulb light is turned off"); <br/>}</P> <p> public class tubelight: light <br/>{< br/> Public override void turnon () <br/>{< br/> console. writeline ("tube light is turned on"); <br/>}</P> <p> Public override void turnoff () <br/>{< br/> console. writeline ("tube light is turned off "); <br/>}</P> <p> public class lightsimplefactory <br/>{< br/> Public Light create (string lighttype) <br/>{< br/> If (lighttype = "bulb") <br/> return New bulblight (); <br/> else if (lighttype = "tube") <br/> return New tubelight (); <br/> else <br/> return NULL; <br/>}</P> <p> public class client <br/>{< br/> Public static void main () <br/>{< br/> lightsimplefactory LSF = new lightsimplefactory (); </P> <p> light L = LSF. create ("bulb"); <br/> L. turnon (); <br/> L. turnoff (); </P> <p> console. writeline ("-----------------"); </P> <p> L = LSF. create ("tube"); <br/> L. turnon (); <br/> L. turnoff (); <br/>}< br/>}
Iv. Evolution of the simple factory Mode
Evolution of simple factory mode (1)
In addition to the above usage, in some cases, simple factory can be assumed by the abstract Product role. An abstract product class is also a factory of sub-classes.
Program example:
Using system; </P> <p> public class light <br/>{< br/> Public Virtual void turnon () <br/>{< br/>}</P> <p> Public Virtual void turnoff () <br/>{< br/>}</P> <p> Public static light create (string lighttype) <br/>{< br/> If (lighttype = "bulb") <br/> return New bulblight (); <br/> else if (lighttype = "tube") <br/> return New tubelight (); <br/> else <br/> return NULL; <br/>}</P> <p> public class bulblight: Light <br/>{< br/> Public override void turnon () <br/> {<br/> console. writeline ("Bulb light is turned on"); <br/>}</P> <p> Public override void turnoff () <br/>{< br/> console. writeline ("Bulb light is turned off"); <br/>}</P> <p> public class tubelight: light <br/>{< br/> Public override void turnon () <br/>{< br/> console. writeline ("tube light is turned on"); <br/>}</P> <p> Public override void turnoff () <br/>{< br/> console. writeline ("tube light is turned off "); <br/>}</P> <p> public class client <br/>{< br/> Public static void main () <br/> {<br/> light L = light. create ("bulb"); <br/> L. turnon (); <br/> L. turnoff (); </P> <p> console. writeline ("-----------------"); </P> <p> L = light. create ("tube"); <br/> L. turnon (); <br/> L. turnoff (); <br/>}< br/>}
Evolution of simple factory mode (II)
Merge all three roles:
Similar to Singleton, but different.
V. Advantages and Disadvantages:
Advantages:
The factory class contains the necessary judgment logic to determine when to create a product class instance. The client can avoid the responsibility of directly creating product objects, instead of simply "consuming" products. The simple factory model achieves division of responsibility through this approach.
Disadvantages:
When the product has a complex multi-layer hierarchical structure, the factory class only has its own, and should not change, is the disadvantage of the model. Because the factory class integrates the creation logic of all products, the entire system will be affected once it fails to work normally.
At the same time, it is difficult to expand the system. Once a new product is added, the factory logic has to be modified, which may cause the factory logic to be too complex.
In addition, the simple factory mode usually uses the static factory method, which makes it impossible to inherit from sub-classes, and the factory role cannot form a hierarchy based on inheritance.
References:
Min Hong, Java and mode, e-Industry Press
[Us] James W. Cooper, C # design model, Electronic Industry Press
[Us] Alan shalloway James R. Trott, design patterns explained, China Power Press
[Us] Robert C. Martin, Agile Software Development-principles, models and practices, Tsinghua University Press
[Us] Don box, Chris sells, 1st. Net essence: Public Language Runtime Library, China Power Press