Design mode-Factory mode (created)

Source: Internet
Author: User

The factory model belongs to the creation model, which is divided into three types, simple Factory mode, factory method mode and abstract Factory mode.

First, simple Factory mode

In the factory to make judgments, according to the product type to create the corresponding products, when adding new products need to modify the factory class.

For example:

Enum CTYPE {corea, Coreb};   Class Singlecore  {public  :      virtual void Show () = 0;};  Single Core a  class Singlecorea:public Singlecore  {public  :      void Show () {cout<< "Singlecore a" << Endl }  };  Single Core B  class Singlecoreb:public Singlecore  {public  :      void Show () {cout<< "Singlecore B" < <endl; }  };  The only factory that can produce two types of processor cores, internally judged  class Factory {public  :       singlecore* createsinglecore (enum CTYPE CType)      {          if (ctype = = Corea)//Factory internal judgment              return new Singlecorea ();//production Core a          else if (ctype = = Coreb)              return new Singlecoreb (); Production nuclear b          else              return NULL;      }  };  

Second, the factory method model

The disadvantage of the simple factory model is that when adding new product types, it is necessary to modify the factory class, violating the open/closed principle. A factory method pattern is defined as a factory class base class that allows its subclasses to decide which class to instantiate. The factory method pattern has the disadvantage of comparing to the simple factory model, which is to define a factory class, which requires more class definitions than the simple factory model.

For example:

Class Singlecore  {public  :      virtual void Show () = 0;};  Single Core a  class Singlecorea:public Singlecore  {public  :      void Show () {cout<< "Singlecore a" << Endl }  };  Single Core B  class Singlecoreb:public Singlecore  {public  :      void Show () {cout<< "Singlecore B" < <endl; }  };  Class Factory  {public  :      virtual singlecore* createsinglecore () = 0;};  Production of a core factory  class Factorya:public Factory  {public  :      singlecorea* Createsinglecore () {return new Singlecorea; }  };  Production of B-Core factory  class Factoryb:public Factory  {public  :      singlecoreb* Createsinglecore () {return new Singlecoreb; }  };  

III. Abstract Factory Model

The abstraction of Factory mode is the upgrade of factory method mode. The base class in the factory method pattern is the interface that defines a production product, and the abstract factory pattern is the interface for creating a series of related products.

For example:

Single Core class Singlecore {public:virtual void Show () = 0;};  Class Singlecorea:public Singlecore {public:void Show () {cout<< "single Core A" <<endl;}  };  Class Singlecoreb:p ublic singlecore {public:void Show () {cout<< "single Core B" <<endl;}  };  Multi-core class Multicore {public:virtual void Show () = 0;};    Class Multicorea:public multicore {public:void Show () {cout<< "Multi Core A" <<endl;}  };  Class Multicoreb:public multicore {public:void Show () {cout<< "Multi Core B" <<endl;}  };    Factory class Corefactory {public:virtual singlecore* createsinglecore () = 0;  Virtual multicore* createmulticore () = 0;}; Factory A, designed to produce a type a processor class Factorya:p ublic corefactory {public:singlecore* createsinglecore () {return new Singl Ecorea ();  } multicore* Createmulticore () {return new Multicorea ();}  }; Factory B, a processor class Factoryb:public corefactory specifically designed for the production of Model B {public:singlecore* Createsinglecore () {return new Singlecoreb ();}  multicore* Createmulticore () {return new Multicoreb ();}  };

  

Reference: http://blog.csdn.net/wuzhekai1985/article/details/6660462

Design mode-Factory mode (created)

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.