Simple factory mode and factory method mode

Source: Internet
Author: User

Turn: http://blog.csdn.net/zhengzhb/article/details/7348707

------------------------------------

1. Preface

The design model has its own characteristics. Today, we will summarize the differences and links between the simple factory model and the factory method model in common models.

2. Two modes Overview

1. Simple factory Model

For an introduction to the simple factory model, refer to my other Blog Code, which is "excellent? ---- Simple factory Model

2. Factory method mode

 

  • The factory method mode is also called the factory mode, or the virtual constructor mode is a class creation mode.
  • In the factory method mode, the parent class defines the public interfaces for creating objects, while the Child class is responsible for generating specific objects. the purpose is to delay the class instantiation operation to the subclass. the subclass determines which class to instantiate.

 

Factory method mode structure:

3. Comparison of the two modes

For the two modes, see the following table:

 

Simple factory Mode

Factory method mode

Type

Creation Mode

Creation Mode

Concept

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.

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.

Features

You don't need to know how these objects are created and organized.

A small-Granularity Design Model is derived from a simple factory model. An abstract factory corresponds to an abstract product.

Advantages

1. You can create product instances at any time with the necessary logic judgment.

2. The client can avoid the responsibility of directly creating product objects.

3. Low coupling and clearly differentiate their respective responsibilities

4. conducive to the optimization of software architecture

1. overcome the disadvantages of the simple factory Model

2. The specific factory class only completes a single task

3. concise code

4. fully meets the requirements of OCP and has good scalability.

Disadvantages

1. When the product has a complex multi-level hierarchical structure, the factory class only has its own, so it should remain unchanged

2. The factory class integrates the creation logic of all products. Once it fails to work properly, the entire system will be affected.

3. Difficult System Expansion

4. Violation of the open-closed Principle

1. Not easy to maintain

2. It is troublesome to modify multiple product classes at the same time.

Applicability

1. Fewer objects are created.

2. The customer only knows how to pass in the factory class

1. When a class does not know the class of the object it must create or a class wants the subclass to specify the object it creates

2. When the class delegates the responsibility of creating an object to one of multiple help sub-classes and you want to localize the information of which help sub-classes are proxies

Commonalities

In essence, it is to extract the unchanged parts and leave the variable parts as interfaces to maximize reuse.

4. Two modes

The structure diagram and the following comparison code are examples of a simple calculator function, which contains four basic operations: addition, subtraction, multiplication, and division.

 

1. Simple factory Model

2. Factory method mode

Note:

In simple factory mode, a factory class is at the center of product class instantiation. It knows the details of each product class and determines which product should be instantiated. the advantage of the simple factory mode is that the client can be independent from the product creation process, and the client does not have to be modified when new products are introduced to the system. the disadvantage is that when a new product is added to the system, the factory class must be modified to add the necessary processing logic. the fatal disadvantage of the simple factory mode is the factory class in the core position, because once it cannot determine which class to instantiate, it cannot be used, the factory method can solve this problem well.

 

In the factory method mode, the factory method is used to create the product required by the customer, and the details of the specific product category to be instantiated are also hidden from the client, the core of the factory method mode is an abstract factory class. Various factory classes inherit factory methods through the abstract factory class, so that customers can only care about abstract products and abstract factories, you don't have to worry about the specific product that will be returned or how it is created by a specific factory.

 

5. Two modes of code

1. Simple factory Model

[CSHARP]View plaincopyprint?
  1. Public class operationfactory // creates a factory. The factory determines how to calculate the factory after the user has entered the symbol.
  2. {
  3. Public static operation createoperate (string operate)
  4. {
  5. Operation operator = NULL;
  6. Switch (operate)
  7. {
  8. Case "+ ":
  9. Operator = new operationadd ();
  10. Break;
  11. Case "-":
  12. Operator = new operationsub ();
  13. Break;
  14. Case "*":
  15. Operator = new operationmul ();
  16. Break;
  17. Case "/":
  18. Operator = new operationdiv ();
  19. Break;
  20. }
  21. Return response;
  22. }
  23. }
  24. }
Public class operationfactory // create a factory. After the user has entered the symbol, the factory will determine how to calculate {public static operation createoperate (string operate) {operation operator = NULL; switch (operate) {Case "+": condition = new operationadd (); break; Case "-": condition = new operationsub (); break; Case "*": break = new operationmul (); break; Case "/": Break = new operationdiv (); break;} return break ;}}}

 

2. Factory method mode

[CSHARP]View plaincopyprint?
  1. Interface ifacloud
  2. {
  3. Operation createoperation ();
  4. }
  5. Class subfactory: ifacloud // subtraction class factory
  6. {
  7. Public operation createoperation ()
  8. {
  9. Return new operationsub ();
  10. }
  11. }
  12. Class addfactory: ifacloud // addition class factory
  13. {
  14. Public operation createoperation ()
  15. {
  16. Return new operationadd ();
  17. }
  18. }
  19. Class mulfactory: ifacloud // multiplication class factory
  20. {
  21. Public operation createoperation ()
  22. {
  23. Return new operationmul ();
  24. }
  25. }
  26. Class divfactory: ifacloud // division class factory
  27. {
  28. Public operation createoperation ()
  29. {
  30. Return new operationdiv ();
  31. }
  32. }
Interface ifacloud {operation createoperation ();} class subfactory: ifacloud // subtraction class factory {public operation createoperation () {return New operationsub () ;}} class addfactory: ifacloud // addition class factory {public operation createoperation () {return New operationadd () ;}} class mulfactory: ifacloud // multiplication class factory {public operation createoperation () {return New operationmul () ;}} class divfactory: ifacloud // division class factory {public operation createoperation () {return New operationdiv ();}}

 

 

Note:

 

For the application of the two modes in code, the simple factory mode requires modifying the factory class if you need to add the function. When the factory method mode is implemented, the client needs to decide which factory to instantiate to implement the computing class and select whether the problem still exists. That is to say, the factory method moves the internal logic judgment of the simple factory to the client code, if you want to add a function, it would have been to modify the factory class. Now it is to modify the client. The factory mode is actually an evolution of the simple mode. The factory model effectively avoids the restriction of violating the open-closed principle in the simple factory model, coupled with scalability. That is to say, the factory method mode is further promoted by the simple factory mode!

 

In fact, no matter which design pattern is applied, it basically puts forward immutable things and uses mutable things as interfaces to achieve maximum reuse and expansion, in the development process, we need to determine the mode we should use as needed. Each mode has its own advantages and disadvantages. How to use these models and when to use them depends on our technology! This is the difference between cainiao and the great god! The greatest benefit is to use the least investment! This is our goal!

This article systematically summarizes the two modes. You are welcome to exchange and guide!

Simple factory mode and factory method mode

Related Article

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.