Design Pattern-factory method pattern

Source: Internet
Author: User

Design Pattern-factory method pattern

Concept:

The factory method mode defines an interface for creating objects, so that the subclass decides which class to instantiate. The factory method delays the instantiation of a class to its subclass.

Role:

In the simple factory mode, we dynamically create specific product class objects in the factory class. Because it is responsible for determining the branch of a specific product object, it is prone to high coupling. Based on the Dependency inversion principle, we abstract the factory class as an interface, and then let the specific factory implement this interface method. The role is composed:

Abstract Factory, specific factory, abstract product, specific product.

Example:

A fresh and simple example can make it easy to understand obscure concepts. Let's take a look at a factory method model for car prices.

We know that the brand and quality of a car determine its price. Like BMW, Ferrari and Benz, the prices are definitely different. If you want to know the price, ask the sales staff. However, in the computer, we cannot directly ask the sales staff!

Although their prices are different, obtaining prices is a public operation. Therefore, you can create an abstract Car class and declare an interface to obtain the virtual GetPrice function. Create three Car subclasses: BMW, Ferrali, and Benz, which inherit from the Car respectively. Through the inheritance relationship, each subclass can rewrite the GetPrice function of the parent class, and perform multi-state calls on the client to get the car price.

Then define an abstract factory class PriceFactory, which only provides interfaces and does not implement methods. A specific factory class, such as BMWFactory, inherits the abstract factory class to create a BMW object for a specific automobile class.

UMLFigure:

 

 

Code:

# Include
 
  
Using namespace std; class Car {public: float m_fPrice; public: // The parent class is defined as a virtual function to implement multi-state virtual float GetPrice () {return m_fPrice * 1 ;}}; class BMW: public Car {public: float GetPrice () {return m_fPrice * 3 ;}}; class Ferrali: public Car {public: float GetPrice () {return m_fPrice * 11 ;}}; class Benz: public Car {public: float GetPrice () {return m_fPrice * 6 ;}}; // abstract class, only the interface class PriceFactory {public: // pure virtual function virtual Car * CreateGetPriceObj () = 0 ;}; // The specific automobile factory class decides which specific product class to instantiate class BMWFactory: public PriceFactory {public: Car * CreateGetPriceObj () {return (new BMW) ;}}; class ferralifacloud: public PriceFactory {public: Car * CreateGetPriceObj () {return (new Ferrali) ;}}; class BenzFactory: public PriceFactory {public: Car * CreateGetPriceObj () {return (new Benz) ;}; int main () {float fPrice = 0.0; int iTag = 0; PriceFactory * priceFactory; Car * car; cout <---- factory method mode Start ---- <
  
   
> ITag; // generate a specific product class judgment placed on the client switch (iTag) {case 1: priceFactory = new BMWFactory; break; case 2: priceFactory = new ferralifacloud; break; case 3: priceFactory = new BenzFactory; break; default: priceFactory = new BMWFactory; break;} // assign the sub-class pointer object to the parent class and convert the car to priceFactory-> CreateGetPriceObj (); car-> m_fPrice = 100000.0; // because GetPrice is defined as a virtual function in the parent class, the polymorphism fPrice = car-> GetPrice (); delete car; car = NULL can be implemented; cout <price: <
   
    

Differences between simple factory mode and factory method mode:

1. The workshop method model is derived from the simple factory method;

2. There is a factory class between them, but the core factory class of the factory method is no longer responsible for product creation. It is only used as an abstract factory role, only provides interfaces that must be implemented by specific factory subclass, which enables the system to introduce new products without modifying the specific factory role;

3. The factory method places the internal logic judgment of the simple factory on the client. For the newly added function, you do not need to change the factory class, as long as you change the client. In this way, you do not need to modify the existing classes to better comply with the open-closed principle (open to expansion and closed to modification ).

Application scenarios:

1. The first case is that for a product, the caller knows exactly which factory service should be used, instantiate the specific factory, and produce a specific product.

2. In the second case, we only need a product. There is no need to know which factory the product is produced. The factory decision-making process is transparent to users. The final decision on which specific factory is selected is on the producer side. They instantiate a specific factory based on the current system and return it to the user.

 

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.