3. Abstract Factory mode

Source: Internet
Author: User

1. Definition

Provide an interface for creating families of related or dependent objects without specifying their concrete clas Ses. (Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.) )

2. Mode description

Abstract Factory mode is an upgraded version of the factory method pattern, which is a good solution for generating the required objects through the abstract factory pattern when there are multiple business varieties and business classifications.

3. Common code

    /// <summary>    ///Abstract Product class/// </summary>     Public Abstract classAbstractproducta {//Abstract Methods         Public Abstract voidMethod1 (); }    /// <summary>    ///Specific Product Categories/// </summary>     Public classProducta1:abstractproducta { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("ProductA1"); }    }    /// <summary>    ///Specific Product Categories/// </summary>     Public classProducta2:abstractproducta { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("ProductA2"); }    }
    /// <summary>    ///Abstract Product class/// </summary>     Public Abstract classABSTRACTPRODUCTB {//Abstract Methods         Public Abstract voidMethod1 (); }    /// <summary>    ///Specific Product Categories/// </summary>     Public classPRODUCTB1:ABSTRACTPRODUCTB { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("ProductB1"); }    }    /// <summary>    ///Specific Product Categories/// </summary>     Public classPRODUCTB2:ABSTRACTPRODUCTB { Public Override voidMethod1 () {//Business logic ProcessingConsole.WriteLine ("ProductB2"); }    }

    /// <summary>    ///Abstract Factory class
The factory class defines the product creation of two product families, there are N product families, there should be N creation method in the abstract factory class. /// </summary> Public Abstract classAbstractfactory {
Create a product family product Public Abstractabstractproducta createproducta ();
Create B product Family products Public AbstractABSTRACTPRODUCTB CREATEPRODUCTB (); } /// <summary> ///specific factory class, have m product grade should have m to implement the factory class, in each implementation plant, to achieve different product family production tasks. /// </summary> Public classCreator1:abstractfactory { Public Overrideabstractproducta createproducta () {return NewProductA1 (); } Public OverrideABSTRACTPRODUCTB CREATEPRODUCTB () {return NewProductB1 (); } } /// <summary> ///Specific factory class/// </summary> Public classCreator2:abstractfactory { Public Overrideabstractproducta createproducta () {return NewProductA2 (); } Public OverrideABSTRACTPRODUCTB CREATEPRODUCTB () {return NewProductB2 (); } }
    /// <summary>    ///Scene Class/// </summary>     Public classClient { Public Static voidMain (string[] args) {abstractfactory Creator1=NewCreator1 (); ABSTRACTPRODUCTA A1=Creator1.            Createproducta (); ABSTRACTPRODUCTB B1=Creator1.            CREATEPRODUCTB (); A1.            Method1 (); B1.            Method1 ();        Console.readkey (); }    }

4. Advantages and disadvantages of the model

(1) Advantages

. Encapsulation: The implementation class of each product is not a high-level module to be concerned about, what is it to care about? is the interface, is abstract, it does not care about how the object is created, who is responsible for it? Factory class, as long as you know who the factory class is, you can create a desired object.

(2) Disadvantages

The biggest drawback of the abstract factory model is that product family expansion is very difficult, why? Take the general code example, if you want to add a product C, that is, the product family from the original 2 to 3, see how many changes in our program it! Abstract class Abstractfactory to add a method CREATEPRODUCTC (), and then the two implementation classes are also to be modified, which is a serious violation of the opening and shutting principle.

Reference

1. Zen of Design Patterns

2.Terrylee Abstract Factory mode

3. Abstract Factory mode

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.