Simple Factory mode

Source: Internet
Author: User

   Simple Factory mode

First, Introduction

The simple Factory mode is also called the Static Factory method (Factory) mode.

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

Simply put, it is a specialized class that is responsible for creating all instances.

Ii. Roles and Responsibilities

  Factory (Creator) role

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

  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.

  Specific products (concrete product) roles

    is the creation target of the simple factory pattern, and all created objects are instances of a specific class that acts as the role.

Iii. Advantages and Disadvantages

  Advantages :

    1. Separating the creators and consumers of the products facilitates the optimization of the software system structure.

2. Depending on the information given by the outside world, decide which specific class The object should be created.

  Disadvantages:

    1. In violation of the open closure principle, if you need to add a new class, you need to change the factory class.

2. Once the factory is not working properly, the whole system will be affected.

3. Because the factory class is static, the subclass cannot be inherited, and cannot form an inheritance tree structure based on the base class.

Iv. use of the scene

  The factory class is responsible for creating fewer objects;

Five, the source code

  From "Big Talk design mode"

1 classcoperation2 {3  Public:4     intM_nfirst;5     intM_nsecond;6     Virtual Doublegetresult () = 0; One }; A //addition - classAddoperation: Publiccoperation - { the  Public: -     Virtual DoubleGetResult () -     { -         returnM_nfirst +M_nsecond; +     } - }; + //Subtraction A classSuboperation: Publiccoperation at { -  Public: -     Virtual DoubleGetResult () -     { -         returnM_nfirst-M_nsecond; -     } in }; -  to //Factory class + classccalculatorfactory - { the  Public: *     Staticcoperation* Create (Charcoperator); $ };Panax Notoginseng  -coperation* Ccalculatorfactory::create (Charcoperator) the { +Coperation *Oper; A     //In C # You can use reflection to cancel the use of the switch, in C + + what? RTTI??  the     Switch(Coperator) +     { -      Case '+': $Oper=Newaddoperation (); $          Break; -      Case '-': -Oper=Newsuboperation (); the          Break; -     default:WuyiOper=Newaddoperation (); the          Break; -     } Wu     returnOper; - } About  $ Client - intMain () - { -     intb; ACin>>a>>b; +Coperation * op = ccalculatorfactory::create ('-'); theop->m_nfirst=A; -Op->m_nsecond=b; $Cout<<op->getresult () <<Endl; the     return 0; the}

Simple Factory 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.