Abstract factory pattern-Abstract Factory Mode

Source: Internet
Author: User
Abstract factory pattern: abstract factory pattern   Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract FactoryThe pattern exists to create objects of various series. RelativeFactory methodSolution"Single Object"Demand changes,Abstract FactorySolution"Series objects". A clear understanding is that the abstract factory model provides the construction of multiple products.

Abstract Factory: it has nothing to do with the business logic of the application system.

Concrete Factory: this role is used to create a product instance directly by calling the client. This role contains the logic for selecting appropriate product objects, which is closely related to the business logic of the application system.

Abstract product: the class that assumes this role is the parent class of the object created in the factory method mode, or the interfaces they share.

Concrete Product: Any product object created in the abstract factory model is an instance of a specific product category. This is what the client eventually needs, and it must be filled with the commercial logic of the application system.

Essence: in fact, in the factory method mode, let the factory implement the production of multiple products, that is, to create multiple different classes.

Example:

As the market share has reached a certain level, in order to increase sales performance, the company decided to produce mobile phones to expand its new business. Therefore, the current factory not only needs to produce cars but also mobile phones, and began to build mobile phones. One production workshop also produces Audi cars and Nokia mobile phones. The other workshop not only produces Mercedes-Benz cars but also Apple mobile phones.
 1 # Include <iostream> 2   Class  Car  3  {  4       Public  :  5           Virtual   Void Run () = 0  ;  6   };  7   8   Class  Phone  9   {  10      Public  :  11           Virtual   Void Call () = 0  ;  12   };  13   14   Class Benzcar: Public  Car  15   {  16      Public  :  17           Void  Run ();  18   };  19   Void  Benzcar: Run ()  20   {  21 STD: cout < "  Benzcar is running  " <STD: Endl;  22   }  23   24   Class Audicar: Public  Car  25   {  26       Public  :  27           Void  Run ();  28  };  29   Void  Audicar: Run ()  30   {  31 STD: cout < "  Audicar is running  " < STD: Endl;  32   }  33   34   Class Nokiaphone:Public  Phone  35   {  36       Public  :  37           Void  Call ();  38   };  39   Void  Nokiaphone: Call ()  40   { 41 STD: cout < "  Nokiaphone is calling  " < STD: Endl;  42   }  43   44   Class IPhone: Public  Phone  45   {  46         Public :  47           Void  Call ();  48   };  49   Void  IPhone: Call ()  50   {  51 STD: cout < "  IPhone is running  " < STD: Endl; 52   }  53   54   Class  Abstractfactory  55   {  56       Public  :  57           Virtual Car * createcar () = 0  ;  58           Virtual Phone * createphone () = 0  ;  59   };  60   61   Class  Audinokiafactory:PublicAbstractfactory  62   {  63       Public  :  64 Car * Createcar (); 65 Phone * Createphone ();  66   };  67 Car * Audinokiafactory: createcar ()  68   {  69       Return    New  Audicar ();  70   }  71 Phone *Audinokiafactory: createphone ()  72   {  73 Retrun New  Nokiaphone ();  74   }  75   76   Class  Benziphonefactory:PublicAbstractfactory  77   {  78      Public  :  79 Car * Createcar ();  80 Phone * Createphone ();  81   };  82 Car * Benziphonefactory: createcar ()  83   {  84           Return   New Createcar ();  85   }  86 Phone * Benziphonefactory: createphone ()  87   {  88           Return   New  Createphone ();  89   }  90   91   Void Createandtest (abstractfactory *P)  92   {  93 Car * c = p-> Createcar ();  94 C-> Run ();  95   Delete C;  96 Phone * pH = p-> Createphone ();  97 Ph-> Call ();  98   Delete C; 99   }  100   101   Int Main ( Int Argc, Char ** Argv)  102   {  103 Abstractfactory * P1 = New  Audinokiafactory ();  104 Abstractfactory * P2 = New Benziphonefactory ();  105   Createandtest (P1 );  106   Createandtest (P2 );  107   Delete P2;  108   Delete P1;  109 }

Running result

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.