C # Design Pattern-Abstract Factory mode

Source: Internet
Author: User

One: Abstract Factory mode:

Abstract Factory mode provides an interface to create a product that is responsible for creating related or dependent objects without specifically specifying the specific class, and the abstract factory allows the customer to use an abstract interface to create a set of related products without needing to know or care what the actual product is actually produced. In the last introduction to the factory method pattern, we introduced that the factory method pattern can overcome the difficulty of expanding the simple factory design pattern, and the factory method pattern only completes the creation of a single instance in each specific factory, so it is very extensible. But it's too wasteful for a factory to create a single instance or product, either programmatically or in reality. Our factory should be able to create a series of products, continue to take the notebook factory as an example, a Lenovo notebook factory, it is impossible to produce only one model of Lenovo notebook, this way, the factory method mode is not applicable, but the abstract factory model can be a good solution to this problem.

Second, scenario examples:

Continue to the notebook factory, for example, Lenovo Notebook developed to now have a lot of series, a series there are many branch models, if the use of factory method model, the entire Lenovo of each new model will be set up a factory, very unreasonable. To solve this problem only to achieve a factory can produce all models of Lenovo notebook, and because the sales situation around the same, the production ratio of each place is not the same, making different production conditions around. Here's how to do this with the abstract factory pattern.

Third, the relevant code:

1. Create an abstract class for each product first:

 /// <SUMMARY>  ///  Lenovo 15 abstract class  /// Span style= "COLOR: #808080" ></summary>  public  abstract  class   Lenovo15 {public  abstract  void      SayHello (); }
    /// <summary>    /// Lenovo 16 abstract class     /// </summary>     Public Abstract class Lenovo16    {        publicabstractvoid  SayHello ();    }

2, to achieve different types of different parts of the production number is not the same:

 public  class   guangzhoulenovo15:lenovo15 { //  Guangzhou Production Association 15 number of units  public  override  void   SayHello () {Console.WriteLine (  "  Guangzhou Lenovo 15 production 300 units          " 
 public  class   guangzhoulenovo16:lenovo16 { //  Guangzhou production Lenovo 16 number of units  public  override  Span style= "COLOR: #0000ff" >void   SayHello () {Console.WriteLine ( "  Guangzhou Lenovo 16 production 200 units   "        
 public  class   shanghailenovo15:lenovo15 { //  Shanghai production Lenovo 15 number of units  public  override  void   SayHello () {Console.WriteLine ( 
   
     " 
     Shanghai Lenovo 15 production 400 units  
     
   
     Public class shanghailenovo16:lenovo16    {        // Shanghai production of Lenovo 16 number        of units  Public Override void SayHello ()        {            Console.WriteLine (" Shanghai Lenovo 16 production 100 units ");        }    }

3, create abstract factory class, provide a series of models of production interface:

    /// <summary>    /// Abstract Factory class, providing an interface    for creating each product /// </summary>     Public Abstract class abstractfactory    {        publicabstract  Lenovo15 CreateLenovo15 ();          Public Abstract Lenovo16 CreateLenovo16 ();    }

4, local factories responsible for their own areas of different models of notebook production work:

     Public classGuangzhoufactory:abstractfactory {//Guangzhou Production Association         Public OverrideLenovo15 CreateLenovo15 () {return NewGuangZhouLenovo15 (); }        //Guangzhou Production Association         Public OverrideLenovo16 CreateLenovo16 () {return NewGuangZhouLenovo16 (); }    }
     Public classShanghaifactory:abstractfactory {//Shanghai Production Association         Public OverrideLenovo15 CreateLenovo15 () {return NewShangHaiLenovo15 (); }        //Shanghai Production Association         Public OverrideLenovo16 CreateLenovo16 () {return NewShangHaiLenovo16 (); }    }

6. Call

            //Create an abstract factory in GuangzhouAbstractfactory guangzhoufactory =Newguangzhoufactory (); //Guangzhou Abstract Factory production of Guangzhou LenovoLENOVO15 GUANGZHOULENOVO15 =guangzhoufactory.createlenovo15 (); Guangzhoulenovo15.            SayHello (); Lenovo16 guangzhoulenovo16=guangzhoufactory.createlenovo16 (); //Guangzhou Abstract Factory production of Guangzhou Lenovoguangzhoulenovo16.            SayHello (); //Create an abstract factory in ShanghaiAbstractfactory shanghaifactory =Newshanghaifactory (); //Shanghai Abstract Factory production of Guangzhou LenovoLENOVO15 SHANGHAILENOVO15 =shanghaifactory.createlenovo15 (); Shanghailenovo15.            SayHello (); Lenovo16 shanghailenovo16=shanghaifactory.createlenovo16 (); //Shanghai Abstract Factory production of Guangzhou Lenovoshanghailenovo16.            SayHello (); Console.readkey ();

7. Expansion

If Lenovo wants to open a factory in Shenzhen, at this time, only need to add three classes: one is the specific factory in Shenzhen, responsible for the creation of Shenzhen Lenovo 15 and Lenovo 16, the other two classes are the number of production in Shenzhen Lenovo 15 class and Lenovo 16 class. As seen from the above code, abstract factory for the changes in the series of products to support the open closure principle, the expansion is very simple, but the abstract factory for the addition of new products this situation does not support the open closure principle, such as Lenovo a new model of the Lenovo 17, so it is necessary to modify the interface of the abstract factory, This is also the fault of the abstract factory.

Iv. Summary:

Advantages: Delay the creation of specific products into the sub-class of the specific factory, so that the creation of objects encapsulated, can reduce the dependence between the client and the specific product class, so that the system coupling is low, which is more conducive to later maintenance and expansion

Cons: It is difficult to support changes in new kinds of products. This is because the abstract factory interface has identified a collection of products that can be created, and if a new product needs to be added, it is necessary to modify the interface of the abstract factory, which involves the transformation of the abstract factory class and all subclasses, thus violating the development closure principle.

Systems that use abstract Factory mode should meet several prerequisites:

    • A system does not require reliance on how product class instances are created, combined, and expressed (the premise of all Factory mode applications).
    • The system has a number of products, and the system only consumes one of the products
    • The system requires a library of product classes, all products appear on the same interface, and the client does not need to rely on a specific implementation.

C # Design Pattern-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.