First, the definition
The factory method mentioned earlier. Each factory corresponds to a class of products. Abstract Factory is the factory method is no longer for a product, but a number of different types of products.
The difference between the factory method and the abstract method-manual drawing, the word is a bit ugly.
Second, examples
Service type interface: can also be likened to "series A", Specific products: WCF, BL
Public InterfaceIService {} Public classWcf:iservice { PublicWCF () {Console.WriteLine ("WCF services."); } } Public classBl:iservice { PublicBL () {Console.WriteLine ("BL service."); } }
Push Service interface: can also be likened to "Series B": There are QQ and email products
Public InterfaceIpush {} Public classQq:ipush { PublicQQ () {Console.WriteLine ("QQ push service."); } } Public classEmail:ipush { PublicEmail () {Console.WriteLine ("email push service."); } }
Factory Interface: Create two series of products: Series A and Series B
Public Abstract class Factorybuilder { publicabstract iservice creatservice (); Public Abstract Ipush Creatpush (); }
Factory A: Create a WCF service and QQ push
Public class Wcf_qqpushfactory:factorybuilder { publicoverride iservice creatservice () { returnNew WCF (); } Public Override Ipush Creatpush () { returnnew QQ (); } }
Factory B: Create BL service and email push
Public class Bl_emailfactory:factorybuilder { publicoverride iservice creatservice () { returnNew BL (); } Public Override Ipush Creatpush () { returnnew Email (); } }
Client calls:
// Abstract Factory New factory3.wcf_qqpushfactory (); fb3. Creatservice (); fb3. Creatpush ();
Iii. Advantages and Disadvantages
Excellent: Each factory can create different products in different series. The instantiation is still deferred to the subclass.
Missing: If a series to increase product or add a series, that ~ ~ Add, modify the place more.
Design patterns to be dialectical use, the right is the best.
Design Pattern (iii): Abstract Factory