Example Analysis of C + + design mode programming simple factory model using _c language

Source: Internet
Author: User

The simple factory pattern specifically defines a class that is responsible for creating instances of other classes, which typically have common parent classes. It is also called the Static Factory method pattern, which belongs to the creation pattern of the class.
UML class diagrams for simple factory patterns

Simple Factory mode program through encapsulation inheritance to reduce the coupling degree of the program, design mode makes the program more flexible, easy to repair, easy to reuse.

A simple factory is to make judgments in the factory class, thus creating the corresponding product.


The essence of a simple factory pattern is that a factory class dynamically determines which instance of the product class (these product classes inherit from a parent class or interface) should be created based on the parameters passed in.

Roles included in this pattern and their responsibilities

1. Factory (Creator) role

The core of the simple factory pattern, which is responsible for implementing the internal logic of creating all instances. The factory class can be called directly from outside to create the desired product object.

2. Abstract (Product) role

The parent class of all objects created by the simple factory pattern, which is responsible for describing the common interfaces common to all instances.

3. Specific product (concrete product) role

Is the goal of creating a simple factory pattern, and all created objects are instances of a specific class that acts as the role.
In general, it is a subclass of abstract product classes that implements all the interface methods defined in the abstract product class.

Features of the Simple factory model:

The creation goal of a simple factory pattern, all created objects are instances of a specific class that acts as the role.
In this model, the factory class is the key to the entire pattern. It contains the necessary judgment logic, can decide which concrete class object should be created according to the information given by the outside world. Users can create the required instances directly from the factory class when they use them, without having to understand how they are created and how they are organized. Benefit the optimization of the whole software architecture.
It is not difficult to find that the shortcomings of the simple factory model are also reflected in the factory class, because the factory class concentrates all instances of the creation logic, so "high cohesion" does not do well. In addition, when the specific product classes in the system are increasing, there may be requirements for factory classes to do the corresponding changes, scalability is not very good.

For example: There is a manufacturer of a production processor core, which has only one factory that can produce two types of processor cores. What kind of processor core the customer needs, be sure to show the production plant.
An implementation scenario is given below.

#include <iostream> using namespace std;

Enum Coretype {core_a, core_b};

Class Singlecore {public:virtual void show () = 0;}; * * A Model single core/class Singlecorea:public Singlecore {public:void Show () {cout<< "show Singlecorea" <<end
 L

}
}; /* B Model single core/class Singlecoreb:public Singlecore {public:void Show () {cout<< "show Singlecoreb" <<end
 L

}
}; 
 * * Only factory, can produce single core two types of processors/class Factory {public:singlecore* Createsinglecore (Coretype CType) {switch (CType)

 {Case Core_a:return new Singlecorea ();
 
 Case Core_b:return New Singlecoreb ();
 Default:return NULL;


}
 }
};
 int main () {Factory *pstfactory;
 
 Singlecore *pstsinglecore;

 Pstfactory = new Factory ();
 * * * Production of A core * * Pstsinglecore = Pstfactory->createsinglecore (core_a);

 Pstsinglecore->show ();

 System ("pause");
 * * Production of B core/Pstsinglecore = Pstfactory->createsinglecore (Core_b);

 Pstsinglecore->show (); * * * Production of a core/PstsinglecOre = Pstfactory->createsinglecore (core_a);

 Pstsinglecore->show ();

 * * * Production of a nuclear * * pstsinglecore->show ();

 System ("pause");
 * * Production of B core/Pstsinglecore = Pstfactory->createsinglecore (Core_b);
 
 Pstsinglecore->show ();
return 0;

 }

Run Result:

Show Singlecorea
Please press any key to continue ...
Show Singlecoreb show
singlecorea show
Singlecorea
please press any key to continue ...
Show Singlecoreb

Related Article

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.