Design Pattern (iii): Abstract Factory

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.