Factory model (C ++ description)

Source: Internet
Author: User

Then, the simple factory model described above conforms to four object-oriented principles: Maintenance, reusable, scalable, flexible, and low coupling, but he still has a very

 

The major problem is that to add new products, the factory must be modified, which violates the principle of software design.

 

We can use the factory model to solve this problem.

 

Suppose we want to add a new arithmetic multiplication class, we can design it as follows:

 

 

Design the multiplication Class Based on the computing base class, which is the same as the simple factory model, but when designing the factory, we classify the way of its processing products and design a General Plant (factory Base)

 

Class). In this way, when we need products, we only need to request to the General Plant, the General Plant let the factory to produce products. If there is a new product to be processed, we only need to design a new factory for processing.

 

The new product is ready and there is no need to change the original factory.

 

Implementation Code:

Here we will continue to reference the calculator class in the simple factory model. We will not change the existing products (Operation class and operation base class), but we will only design new factories;

// Multiplication class

# Ifndef operationpower
# Define operationpower

Class operationpower: Public operation
{

Public: Double getresult (double NUMA, double numb );

};
# Endif

 

// Base Factory

# Ifndef Factory
# Define Factory

Class Factory
{

Public: Virtual Operation * createoperation (char operate );

};

# Endif

// Original basic computing base class

# Ifndef factory1
# Define factory1

Class factory1: Public Factory
{
Public: Operation * createoperation (char operate );

};

# Endif

// Operation base class implementation

Operation * factory1: createoperation (char operate)
{
STD: cout <"it is the factory1! "<STD: Endl;

Operation * operator = NULL;
 
Switch (operate)
{
Case '+': Operator = new operationadd ();
Break;
Case '-': Operator = new operationsub ();
Break;
Case '*': Operator = new operationmul ();
Break;
Case '/': Operator = new operationdiv ();
Break;
 
Default: STD: cout <"the operation is error! "<STD: Endl;
Operator = new operation ();
Break;
}

Return response;
}

 

// Adds a multiplication operator processing factory.

# Ifndef factorynew1
# Define factorynew1

Class factorynew1: Public Factory
{
Public: Operation * createoperation (char operate );

};

# Endif

// Processing factory implementation

Operation * factorynew1: createoperation (char operate)
{
STD: cout <"it is a new factory! /N ";

Operation * operator = NULL;
Switch (operate)
{
Case '^': condition = new operationpower ();
Break;
Case 'E': exit (0 );
Break;

Default:
STD: cout <"the operation is error! "<STD: Endl;
Operator = new operation ();
Break;
}

Return response;
}

// Client implementation

Int main (void)
{
Factory * P = new factory1 ();
Operation * operator = p-> createoperation ('+ ');

Double result = 0;
Double NUMA = 20;
Double numb = 4;

 

Result = success-> getresult (NUMA, numb );

STD: cout <"NUMA + numb's result is:" <result <"/N ";

Delete P;
Delete objects;

P = new factorynew1 ();
Operator = p-> createoperation ('^ ');

Result = success-> getresult (NUMA, numb );

STD: cout <"NUMA ^ numb's result is:" <result <"/N ";

System ("pause ");

Return 0;
}

 

Note:

/*
* Advantage: it makes up for the shortcomings of the simple engineering model. To expand new products, you can directly design sub-factories without modifying the factory.
* Disadvantage: There is nothing to do with creating different series of New Products
*/

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.