Factory Model Trilogy: Factory method mode

Source: Internet
Author: User

Objective

Immediately after the previous article began to write, this is the second article in this series, this article mainly talk about the factory method mode. In the last article on the abstract Factory model, the three articles will be summarized.

This article does not give a reflection mechanism to implement the code, mainly because the factory method mode is to solve some of the shortcomings of the simple factory model, but the use of the reflection mechanism of the simple factory model has no such shortcomings, so there is no need to say in this article. This article is mainly a single design mode from the point of view of this model, take you to understand this design pattern.

I understand that may not be deep enough, this series of articles in the existing problems, welcome to put forward, thank you!

What is the factory method model?

The factory method pattern is very similar to the simple factory model, and the approximate structure is basically similar. The difference is that the factory method model further abstracts the factory class, abstracting a previous factory class as an abstract factory and factory subclass, and the abstract factory defines an interface to create an abstract subclass that implements these interfaces and determines which abstract subclasses to instantiate. The factory subclass determines which abstract subclass to create, and what kind of factory subclass is created by the outside world, and the abstract subclass and factory subclass are one by one corresponding.

In factory method mode, as with the simple factory model, the creation of abstract subclasses is hidden from the outside, and only the factory class can be cared for, and the factory subclass responsible for the instantiation determines the final result.

The factory method model consists of four parts:

    • Factory Abstract class: Defines an interface that creates an abstract subclass that returns specific abstract subclasses through an interface.

    • Factory subclass: Inherits from the factory abstract class, and overrides the parent class's method to create the corresponding abstract subclass.

    • Abstract class: Defines the properties and methods required for abstract subclasses, which are obtained by inheriting from the abstract class.

    • Abstract subclass: Inherits from the abstract class, implements the concrete operation.

Why use factory method mode?

In the code for simple Factory mode, if we don't use the reflection mechanism, it's just the standard simple factory pattern code. One problem is that if you add additional computing capabilities, you need to create an abstract subclass, but you also need to modify the code logic in the factory class, which does not conform to the open closure principle. The open closure principle is closed for modifications and is open for extensions. and the judgment and instantiation of all the operations subclasses are done by a factory class, and if the complexity of the business results in a heavier burden on the factory class.

The factory method pattern will be the factory class that was previously responsible for generating concrete abstract subclasses, which are abstracted into a series of classes consisting of factory abstract classes and factory subclasses. Each creation of an abstract subclass, you need to create a factory subclass, and one by one corresponding, by the factory subclass to generate the corresponding abstract subclass, by the outside party to decide which factory subclass to generate. In this way, when new requirements are added, it is not necessary to modify the factory abstract class, but to create the corresponding factory subclass for the new abstract sub-class.

Business Scenarios

The business scenario here also uses an example of the operations mentioned in the previous simple factory pattern, which calculates the results of two values, depending on the operator.

UML Class Diagram

Factory method Mode

This class diagram adds a lot of factory subclasses, each of which corresponds to a factory subclass. The advantage of this is that it is more flexible, and each time a new abstract subclass is added, a factory subclass is generated, with no effect on other classes.

The simple factory model violates the open closure principle, and each time an abstract subclass is added and removed, it is necessary to operate the factory class, which not only opens up the expansion of the factory class, but also opens up the modification of the factory class, which is contrary to the open closure principle. Because in accordance with the open closure principle, a new requirement should be expanded on the basis of the original class, rather than modifying the original class. In this way, when the whole pattern is generating a new algorithm class, it only expands and does not modify the code in the schema, which conforms to the open closure principle.

Code implementation

Factory method Mode when you create a new abstract subclass, the operation of the selection algorithm still exists, the external flexibility still exists, but the original factory class on the abstract subclass of the creation, to the factory sub-class to complete the creation. Before adding and removing modified factory classes, it is now an addition to the factory subclass, which is also an embodiment of the open closure principle. The previous simple factory created abstract subclasses in the factory class, and the factory method pattern created abstract subclasses in the factory subclass, still encapsulating the process of object instantiation.

Because the code is much more, the code for addition and subtraction is posted here, and the other operation code is similar.

Create an operation abstract class, declaring the two attributes and operation methods that participate in the operation, and the following concrete abstract subclass inherits from this class.

@Interface operation : nsobject

@Property (nonatomic, assign) cgfloat numberone;

@Property (nonatomic, assign) cgfloat numbertwo;

- (cgfloat)getresult;

@End

@Implementation Operation

- (cgfloat)getresult {

return 0;

}

@End

@Interface operationadd : operation

@End

@Implementation Operationadd

- (cgfloat)getresult {

return self . Numberone + self. Numbertwo;

}

@End

@Interface operationsub : operation

@End

@Implementation operationsub

- (cgfloat)getresult {

return self . Numberone - self. Numbertwo;

}

@End

Abstract Factory class, which defines the methods that instantiate the actual operation class, inheriting and instantiating different operations classes by subclasses.

@Interface Factory : nsobject

+ (operation *)createoperation;

@End

@Implementation Factory

+ (operation *)createoperation {

return nil;

}

@End

@Interface factoryadd : Factory

@End

@Implementation Factoryadd

+ (operation *)createoperation {

return [operationadd New];

}

@End

@Interface factorysub : Factory

@End

@Implementation factorysub

+ (operation *)createoperation {

return [operationsub New];

}

@End

When used by the outside world, it is possible to instantiate a factory subclass directly and select the specific operation class by instantiating a factory subclass from outside.

- (void)viewdidload {

operation *oper = [factoryadd createoperation];

Oper. Numberone = ;

Oper. Numbertwo = ;

NSLog(@"Result:%f", [oper getresult]);

}

When the requirements change, it is necessary to switch the algorithm, the outside world only need to call the factory subclass class name method to exchange the class names, other places do not have to change. This is like a "switch", where the type of the abstract subclass is controlled by the types of the factory subclasses, and we do not know the process of instantiating the abstract subclass.

Advantages and disadvantages of the factory method model

Advantages

The advantage of the factory method model is greater flexibility, which adds or removes an operation that has no effect on other places, and better conforms to the open closure principle.

And the use of abstraction better in-depth, the factory class is also abstract for the abstract factory class and factory sub-class, external calls more flexible, which is a manifestation of polymorphism.

Disadvantages

The disadvantage of the factory method pattern is also very obvious, adding an abstract subclass to the factory method pattern, which means that the factory subclass is going to be added in pairs, resulting in too many classes being generated, and the complexity of the factory method pattern will increase accordingly.

For this shortcoming, the reflection mechanism can certainly solve this problem well, the factory design pattern and the reflection mechanism's coordination, may make this design pattern to be more easy to use and flexible, reduces the condition judgment and the class quantity.

Faq

Using the factory method pattern, does it seem that it is not more troublesome to create concrete abstract subclasses directly from the outside? It also creates the factory subclass in such a troublesome way, and then works the class to create the abstract subclass.

I will answer this question in two ways:

    1. Assuming that the project is relatively large, in many parts of the world directly using the abstract sub-class directly to the operation, this way in writing code is very fast and very cool. However, suppose one day, the product manager came to say that to change the demand, I now do not subtraction these four kinds of operations, I would like to switch to more advanced other operations ... If you change it so much, you need to modify all the places that are instantiated directly using the abstract subclass.

    2. This is only a design mode of thinking, in the development of the program does not have a design pattern is omnipotent, in the appropriate place with the appropriate design mode, or according to business needs to develop a set of patterns, this is the best. The best model is the one that best suits the business.

Factory Model Trilogy: 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.