Factory design Patterns for Java Design patterns (Factory)

Source: Internet
Author: User

Design patterns can be divided into three types depending on the type of use:
1. creation mode : Factory (Factory mode), Singleton (single State), Builder ((builder mode), Prototype (prototype mode), factory method mode.

2. structure mode : Flyweight (Shared mode), Bridge (bridge mode), Decorator ((decorative mode), Composite ((combo mode), Adapter (adapter mode), Proxy (proxy mode), facade (appearance mode).

< EM id= "__mcedel" > 3, behavior mode : Iterator (iteration mode), Template (stencil mode), Chain of Responsibility ((responsibility chain mode), Memento ((Souvenir mode), em> mediator (mediation mode), interpreter ((interpreter mode),

Strategy (Policy mode) , State, Observer ((Observer mode), Visitor (access mode),command(commands mode).

first, the factory model (Factory)

The factory model is a design pattern that is used very frequently in our daily development, defined as: Define an interface for creating a object, but let subclasses decide which class to INS Tantiate. Factory Method lets a class defer instantiation to subclasses (defines an interface for creating objects, so that subclasses decide which specific class to instantiate, And the instantiation of the class into the subclass). Simply put, the factory model is to create the object when the same as the factory, according to the different needs of users to produce different products, so as to avoid the need for a new product for each customer needs a specialized company to produce (i.e. need to go to new different objects)

  Simple Factory: specific code links

scenario: A person with two roles (teacher, student), does not use the Factory mode when it is necessary to go to the new two types of objects, using the Factory mode only need to be in the factory class according to the different characteristics of separate to create different objects

    Advantages:

1, the realization of the Division of responsibility, the creation of objects are completed in the factory to avoid the client to create products

2, the client only need to know the product name created without having to care about the specific product's class name

3. You can replace and add new physical products without modifying the client code by introducing a configuration file

    Disadvantages:

1, all objects are created in the factory class, once the factory class is abnormal, the entire system will be affected

2, will increase the number of classes in the system, increase the complexity of the system

3, the system is poor expansibility, in the new product will have to modify the factory class code

Person class:

        

Factory class:

        

Student's implementation class:

      

Teacher's implementation class:

        

Consumer Category:

      

    

  Factory methods (Factory method): Code link

    Compared to the simple factory model, the factory method pattern abstracts the factory roles and product roles, and puts the instantiation of each specific class into the subclass.

    Scenario: people who have two roles (teachers, students) are required to take the exam, but because of the different roles in this activity, the action is different (students take exams, teacher supervisor)

    

    Advantages:

1, user value only need to care about the factory of the product, without the key to create the details

2. The polymorphic design of this mode allows the factory to determine what product objects are created, and the details of the creation are encapsulated inside the entity factory

3, adding new products without modifying the abstract factory and abstract products, only need to add a physical factory and a solid product class, in line with the OCP principle

    Disadvantages:

1, in the addition of new products need to write the physical product class and corresponding to the Entity factory class, in Chengdu to increase the complexity of the system, the entire system to bring additional costs

2, the use of the abstract layer in the client definition, increase the system's abstraction and understanding of the difficulty, and the realization of the use of reflection, it is necessary to increase the difficulty of the implementation of the system in Chengdu

 

Abstract Product class:

      

Specific implementation classes:

      

      

Abstract Factory class:

      

Factory Implementation class:

      

Consumer Category:

      

   Abstract Factory: Code link

Scenario: production of two phones (IPhone 7 and Huawei P10) is now required

Advantages:

1, isolated the generation of specific classes, so that users do not need to care about when the physical product was created

2, when multiple objects of an abstract product (such as P8, P9, P10 in the Huawei P-series phones) are designed to achieve the same abstract product, the client can be guaranteed to use only the same product

Disadvantages:

1. It is difficult to expand the abstract factory when adding new products.

Abstract Factory class:

        

        

        

Entity Factory class:

      

Abstract Product class:

      

      

Physical Product class:

      

      

Consumer Category:

      

   Summarize:

      The use of factory design patterns mainly in order to reduce the coupling between the entire system module Chengdu, increase the scalability of the system, in the new or modified function without modification or as little as possible to modify the original code, so that the later system maintenance is easier

Factory design pattern for Java design mode (Factory)

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.