Design Pattern 2 -- factory method pattern

Source: Internet
Author: User

I have a blog about"Simple factory Mode". This blog briefly introduces "Factory method mode ".
Introduction
Defines an interface for users to create objects, so that the subclass determines which class of the instance. Factory method delays the instantiation of a class to the subclass. ---------- Design patterns gof
The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role.
Structure

Example
Previously, 0 of design patterns-simple factory patterns used the following examples. We still use this example, but it is implemented using the factory method. :
A TV factory is dedicated to producing all kinds of TVs for various TV brands. To produce Haier TV, you only need to transmit the parameter "Haier". To produce Hisense TV, you only need to input "Hisense ". The factory returns TV sets of different brands based on different input parameters.
Class Diagram

Abstract product TV (TV ):

public interface TV{    void play();}

 

Hisensetv:

public class HisenseTV:TV{    public void play()    {        ……   }}

Specific product type haiertv:

public class HaierTV:TV{    public void play()    {        ……   }}

 

Abstract product factory interface:

interface ITVFactory{    TV CreateTV();}

Specific product factory haiertvfactory:

class HaierTVFactory : ITVFactory{    public TV CreateTV()    {        return new HaierTV();    }}

Hisensetvfactory:

class HisenseTVFactory : ITVFactory{    public TV CreateTV()    {        return new HisenseTV();    }}

Client code:

class Program{    static void Main(string[] args)    {        ITVFactory fTV = new HaierTVFactory();        TV hTV = fTV.CreateTV();        hTV.play();    }}

Comparison between simple factory models
Brief Introduction
The biggest advantage of the simple factory mode is that the factory class contains the necessary logic judgment, and the related classes are dynamically instantiated Based on the client selection conditions. For the client, the dependencies with specific products are removed.
When implementing the factory method mode, the client needs to decide which factory to instantiate to implement the specific 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, you would have changed the factory class. Now you want to modify the client.
Details
1. Structure Complexity

From this perspective, it is obvious that the simple factory model should be dominant. The simple factory mode requires only one factory class, while the factory class of the factory method mode increases with the number of product classes, which will undoubtedly increase the number of classes and increase the complexity of the structure.
2. Code complexity
There is a conflict between Code complexity and Structure Complexity. Since the simple factory mode is relatively simple in structure, it must be more complex in code than the factory method mode. The factory class in the simple factory mode needs to add many methods (or Code) as the product class increases, while the factory method mode only completes a single task for each specific factory class, and the code is concise.
3. Client programming difficulty
Although the factory method mode introduces interfaces in the factory class structure to meet the OCP (open-closed principle), the factory class needs to be instantiated in the client code. The factory class in the simple factory mode is a static class and does not need to be instantiated on the client. This is undoubtedly an attractive advantage.
4. Management difficulty
This is a key issue. As we all know, the factory method model fully satisfies OCP, that is, it has very good scalability. Does that mean that the simple factory model has no scalability? The answer is no. The simple factory model also has good scalability-only a small amount of code (modifying the code of the factory class) needs to be modified to meet the scalability requirements. Although this does not fully satisfy the OCP, sometimes it is not necessary to stick to the design theory, depending on the situation.
5. maintainability
Then we will analyze it from the perspective of maintainability. If a specific product class needs to be modified, it is likely that the corresponding factory class needs to be modified. When you need to modify multiple product classes at the same time, modification to the factory class will become quite troublesome (it is already a problem to check the number ). But simple factories do not have these troubles. When multiple product classes need to be modified, the simple factory model still only needs to modify the unique factory class (in any case, can it be changed to meet the requirements? ).

 

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.