0 of design patterns-simple factory models

Source: Internet
Author: User

 

Introduction

The reason why the name is "0 of design patterns" is that the simple factory model does not belong to one of 23 gof design patterns. It is determined by a factory object to create a product instance. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models.

Essence

A factory class dynamically determines which product class should be created based on input parameters (these product classes inherit from a parent class or interface.

Role

Factory)

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)

The parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.

Specific product (Concrete Product)

Is the creation target of the simple factory mode. All created objects are instances of a specific class that acts as this role.

Example

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()    {        ……    }}

Factory type:

public class TVFactory{    public static TV CreateTV(string ser)    {        if(ser=="Haier")            return new HaierTV();        else if(ser=="Hisense")            return new HisenseTV();        else            throw new Exception("Error!");    }}

 

Client code:

class program{    static void main(string[] args)    {        TV cTv=TVFactory.CreateTV("Haier");        cTv.play();    }}

 

Advantages and disadvantages

Advantages

1. the factory class contains the necessary judgment logic to determine when to create a product class instance. The client can exempt the responsibility of directly creating product objects, instead of simply "consuming" products, the client does not need to know the class name of the created product class, but only needs to know the parameter corresponding to the specific product class.

2. The simple factory model separates responsibilities through these methods, and clarifies their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture.

Disadvantages

1. Because the factory class has centralized the creation logic of all instances, in violation of the High Cohesion responsibility allocation principle, all creation logic is centralized into a factory class.

2. It is difficult to expand the system. Once a new product is added, the factory logic has to be modified. This may cause the factory logic to be too complex and violate the "open-closed" principle.

3. when the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;

These shortcomings have been overcome in the factory method model.

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.