C + + implementation of design Patterns 3. Abstract Factory

Source: Internet
Author: User

Simple Factory mode and Factory mode require the same type of product subclass to have a common approach, which limits the extension of the product subclass. Abstract factories can provide a client with an interface that creates product objects in multiple product families without having to specify a product. In an abstract factory, the same class of product subclasses are grouped into one class, allowing them to inherit the same abstract subclass and treat the specific product subclasses of an abstract subclass as a group. Product family refers to the family of products that are related to function in different product hierarchy. Are generally located at the same position in different hierarchical structures. The number of products in each product family is the same as the number of product grade structures. The user uses the factory and the family to judge

When the user is in use, it is necessary to know which factory and which product family the product class, that is, through the group and family of two-dimensional coordinates to determine the specific product subclass. Each factory subclass is responsible for a family of products, and there is a way to generate a type of product.

Give an example. For the abstract factory base class Abstractfactory derives 3 specific factory class Factory1,factory2,factory3, the number of families is 3, that is, each abstract product class has 3 specific subclasses. If there are 2 abstract product classes ABSTRACTPRODUCTA,ABSTRACTPRODUCTB, or 2 groups, there are 2 function CREATEPRODUCTA,CREATEPRODUCTB that create objects in the factory class.

A simple implementation:

Class abstractproducta{public:virtual void fun () = 0; Abstractproducta () {}virtual ~abstractproducta () {}};class producta_1:public abstractproducta{public:virtual void Fun () {cout<< "a_1" &LT;&LT;ENDL;} Producta_1 () {}virtual ~producta_1 () {}};class producta_2:public abstractproducta{public:virtual void Fun () {cout< < "a_2" &LT;&LT;ENDL;} Producta_2 () {}virtual ~producta_2 () {}};class producta_3:public abstractproducta{public:virtual void Fun () {cout< < "A_3" &LT;&LT;ENDL;} Producta_3 () {}virtual ~producta_3 () {}};//b Group class abstractproductb{public:virtual void fun () = 0; ABSTRACTPRODUCTB () {}virtual ~ABSTRACTPRODUCTB () {}};class productb_1:public abstractproductb{public:virtual void Fun () {cout<< "b_1" &LT;&LT;ENDL;} Productb_1 () {}virtual ~productb_1 () {}};class productb_2:public abstractproductb{public:virtual void Fun () {cout< < "b_2" &LT;&LT;ENDL;} Productb_2 () {}virtual ~productb_2 () {}};class productb_3:public abstractproductb{public:virtual void Fun () {cout< < "B_3" &Lt;<endl;} Productb_3 () {}virtual ~productb_3 () {}};class abstractfactory{public:abstractfactory () {};virtual ~AbstractFactory ( ); virtual abstractproducta* createproducta () = 0;virtual abstractproductb* createproductb () = 0;}; Class Factory1:public Abstractfactory{public:factory1 () {}~factory1 () {}abstractproducta* createproducta () {return New Producta_1 ();} abstractproductb* CREATEPRODUCTB () {return new productb_1 ();}}; Class Factory2:public Abstractfactory{public:factory2 () {}~factory2 () {}abstractproducta* createproducta () {return New producta_2 ();} abstractproductb* CREATEPRODUCTB () {return new productb_2 ();}}; Class Factory3:public Abstractfactory{public:factory3 () {}~factory3 () {}abstractproducta* createproducta () {return New Producta_3 ();} abstractproductb* CREATEPRODUCTB () {return new Productb_3 ();}};

The use of abstract factories, the original meaning is for UNIX and Windows, the two are the same for many operations, these same objects such as File,button, and so on, these things are the Windows family and the Unix family each have a copy, The factory class of the object such as file is called Unixfile and Windowsfile as a group, and the structure is divided.

Abstract factory supports different types of products, more convenient to use the middle of the same family of different types of products. Add a family of products, do not modify the factory class and Product class interface, do not violate the open and closed principle.

Cons: The structure is too bloated, even if I simply implement, and 3x2, wrote the above so much code. If the number of product groups becomes greater or the number of families increases, it can be difficult to manage. Each time you add a group of products, the factory class and product class interface should be modified to violate the open and closed principle.



C + + implementation of design Patterns 3. Abstract Factory

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.