5. Simple factory mode and factory Mode

Source: Internet
Author: User

5. Simple factory mode and factory Mode
Zookeeper

1 The core content of the simple factory model is:

A: independent methods

B: The factory does not. Instead, it uses the switch method to determine which method to use.

C: Understanding: Bring the same girl to the same place, and the girl can choose different dishes.

2. Role of the simple factory mode: Use a simple switch and a method to control which method to use.

3. Simple factory model description

Factory model: the customer class and the factory class are separated.

A consumer needs a product at any time and only requests from the factory.

Consumers can accept new products without modification. The disadvantage is that when the product is modified,

The factory class must also be modified accordingly. For example, how to create and provide data to clients.

 

It's easy to catch up with MM. McDonald's chicken wings and KFC's chicken wings are what MM loves to eat,

Although the taste is different, whether you take MM to McDonald's or KFC,

Just say "four chicken wings" to the waiter. McDonald's and KFC are factories that produce chicken wings.

 

First, the base class stores data.

Second, there are many derived classes that store data.

Third, implement interface classes, and use static functions to call various derived classes

4. Simple factory pattern class diagram


5. Code:

# Include <iostream>

# Include <string>

Usingnamespacestd;

 

// Factory mode: the customer class and the factory class are separated.

// The consumer needs a product at any time and only requests from the factory.

// The consumer can accept new products without modification. The disadvantage is that when the product is modified,

// Modify the factory class. For example, how to create and provide data to clients.

 

// Try to catch up with MM. The chicken wings of McDonald's and KFC are what MM loves to eat,

// Although the taste is different, whether you take MM to McDonald's or KFC,

// Just say "four chicken wings" to the waiter. McDonald's and KFC are factories that produce chicken wings.

 

// First, the base class stores data.

// Second, there are many derived classes. The Derived classes store data.

// The third implementation interface class, using static functions to call various derived classes

 

// The base class stores data.

ClassOperation

{

Public:

DoublenumberA, numberB; // two numbers

Virtualdouble getResult () // obtain the result

{

Return 0;

}

};

 

// Operation on storing Derived classes

ClassaddOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA + numberB;

}

};

 

// Subtraction

ClasssubOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA-numberB;

}

};

 

// Multiplication operation

ClassmulOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA * numberB;

}

};

 

// Except operations

ClassdivOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA/numberB;

}

};

 

// For my understanding, this controller controls the operation to be called by passing tags.

ClassoperFactory

{

Public:

StaticOperation * createOperation (charc)

{

Switch (c)

{

Case '+ ':

ReturnnewaddOperation;

Break;

Case '-':

ReturnnewsubOperation;

Break;

Case '*':

ReturnnewmulOperation;

Case '/':

ReturnnewdivOperation;

Break;

}

}

};

 

Intmain ()

{

Operation * operator = operFactory: createOperation ('-');

Latency-> numberA = 9;

Accept-> numberB = 99;

Cout <strong-> getResult () <endl;

 

Cin. get ();

Return 0;

}

The running result is as follows:

-90


What is the simple factory model?

A dedicated class is defined to create instances of other classes. The created instance usually has a common parent class. It is also called the static factory method mode and belongs to the class creation mode.
UML class diagram in simple factory mode (see the picture on the right)
The essence of the simple factory mode is that a factory class dynamically determines the product class to be created (these product classes inherit from a parent class or interface) based on the input parameters.
Roles and responsibilities in this mode
Factory (Creator) Role
The core of the simple factory mode is to implement the internal logic for creating all instances. The factory class can be directly called by the outside world to create the required product objects.
Abstract (Product) Role
The parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.
Product (Concrete Product) Role
Features of the simple factory model:
In simple factory mode, all created objects are instances of a specific class that acts as the role.
In this mode, the factory class is the key to the entire model. It contains the necessary judgment logic to determine the specific class object to be created based on the information given by the outside world. You can directly create required instances based on the factory class during use without understanding how these objects are created and organized. It is conducive to the optimization of the entire 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 the creation logic of all instances, it is not easy to do "high cohesion. In addition, when the number of product categories in the system increases, the factory category may be required to be modified accordingly, and the scalability is not very good.

What are the similarities and differences between the simple factory model and the factory method model?

It is recommended that LZ study the simple factory, factory method and abstract factory together. Haha, my teacher asked me to study the differences between the three models, so I may be more familiar with learning together.
The simple factory mode is also called the static factory method mode. After renaming, we can see that this mode must be very simple. Its purpose is to define an interface for creating objects.
Let's take a look at its composition:
1) Factory roles: this is the core of this model and contains some commercial logic and judgment logic. In java, it is often implemented by a specific class.
2) Abstract Product role: it is generally the parent class or implemented interface inherited by a specific product. It is implemented by interfaces or abstract classes in java.
3) specific product role: the object created by the factory class is the instance of this role. It is implemented by a specific class in java.
The factory method mode removes the static attribute of the factory method in the simple factory mode so that it can be inherited by the quilt class. In this way, the pressure on the factory method in the simple factory mode can be shared by different factory subclass in the factory method mode.
Take a look at its composition:
1) Abstract Factory role: this is the core of the factory method model and has nothing to do with the application. It is an interface that must be implemented by a specific factory role or a parent class that must be inherited. In java, it is implemented by abstract classes or interfaces.
2) specific factory role: it contains Code related to the specific business logic. An application is called to create the objects of a specific product.
3) Abstract Product role: it is the parent class or implemented interface inherited by a specific product. In java, abstract classes or interfaces are generally used for implementation.
4) specific product role: the object created by the specific factory role is the instance of this role. It is implemented by specific classes in java.

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.