Design patterns that's something--factory method mode

Source: Internet
Author: User

Concept :

The factory method pattern defines an interface for creating objects , letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.

composition role :

In the simple factory model, we put the dynamic creation of specific product class objects in the factory class. Because it is responsible for the branch judgment of the specific product object, it is easy to produce high coupling. Based on the dependency reversal principle, we abstract the factory class into an interface and then let the concrete factory implement the interface method. The constituent roles are:

Abstract factories, concrete factories, abstract products, specific products.

Example :

A vivid and simple example always makes 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 the car determines its price. Like BMW, Ferrari (Ferrali) and Mercedes-Benz (Benz) three cars, their prices are certainly not the same. If you want to know the price of it, you can ask sales people and so on. But in the computer, we can not directly ask sales staff Ah!

For them, although their prices are different, getting the price is a common operation. As a result, you can create an abstract fatherautomobile class (car) that declares an interface to get the price virtual GetPrice function . Then create three specific car sub-categories: Bmw,ferrali and Benz, respectively, inherited from car. By inheriting relationships, each subclass can rewrite the parent class's GetPrice function to make polymorphic calls in the client to get the car price.

Then define an abstract factory class Pricefactory, which only provides interfaces, not implementation methods. and specific factory classes such as bmwfactory by inheriting the abstract factory class, to undertake the specific car class BMW object creation.

UML Figure :



    code:

#include <iostream>using namespace Std;class car{public:float m_fprice;public://The parent class is defined as a virtual function to implement the polymorphic 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 provides interface class pricefactory{public://pure virtual function virtual car* creategetpriceobj () = 0;};/ /Specific car factory class decide which specific product class Bmwfactory:public pricefactory{public:car* Creategetpriceobj () {return (new BMW);}; Class Ferralifactory: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----" <<endl;cout<< "Bmw:1,ferrali:2,benz:3" <<endl;cout< < "Please enter the price of the car you want to inquire about:" cin>>itag;//generate specific product class judgments placed on client switch (ITAG) {case 1:pricefactory = new BMWfactory;break;case 2:pricefactory = new Ferralifactory;break;case 3:pricefactory = new Benzfactory;break;default: Pricefactory = new Bmwfactory;break;} The subclass pointer object is assigned to the parent class, and the car = Pricefactory->creategetpriceobj () is transformed upward, Car->m_fprice = 100000.0;//because getprice is defined as a virtual function in the parent class, So polymorphic Fprice = Car->getprice ();d elete Car;car = null;cout<< "Price:" <<fPrice<<endl;cout<< " ----Factory method Mode End----"<<endl;return 1;}

the difference between a simple factory model and a factory method pattern :

1, the Workshop method model is a simple factory method derivation;

2. there is a factory class between them, but the factory method The Core factory class is no longer responsible for product creation, It's just as Abstract Factory role, only responsible for provide the interface that a specific factory subclass must implement, it Allows the system to introduce new products without modifying specific factory roles ;

3, factory method to the simple factory internal logic judgment put to the client, for the new added function, do not change the factory class, as long as the client on the line. This will not modify the existing classes, more in line with the open- closed principle (open to expansion, closed to changes).

Application Scenarios:

1, the first case is for a product, the caller clearly know which specific factory services should be used, instantiate the specific factory, to produce specific products.

2, the second situation, just need a product, do not need to know which factory production, the choice of factory decision-making process for users is transparent. The decision to decide which specific factory to choose is on the producer side, and they instantiate a specific factory back to the user based on the current system situation.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design patterns that's something--factory method mode

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.