Simple Factory mode, factory method mode, abstract Factory mode

Source: Internet
Author: User

Simple Factory mode, abstract Factory mode, factory method mode, these three kinds of factory models are in the design mode of the creation mode, they are somewhat similar in form and characteristics, the ultimate goal is to help us to the object of the instantiation part of the extraction, thereby optimizing the system architecture, enhance the system's extensibility, In other words, it is better to embody the open closure principle.

Simple Factory mode:

Concept:

Simple Factory mode is the creation of a class, also known as the static factory method pattern, is a factory class based on the parameters of the input to create an instance of which product class, involving the factory role, abstract product roles and specific product roles.

Structure diagram:

Analysis:

One: The Simple factory class is the key to the entire pattern, containing the necessary logical judgments, based on the given information, to determine exactly which class of object should be created. The client can dispense with the responsibility of directly creating the Product object, and it is only responsible for the consumption object, which realizes the division of the right of responsibility and is advantageous to the optimization structure.

Second: The factory class centralizes all instances of logic, violates the principle of high cohesion, it can create the class can only be considered in advance, if the new class will need to change the factory class, and when the specific product increases, may appear the factory class according to different conditions to create different instances of the requirements, This kind of judgment of the condition and the judgment of the specific product type staggered together, prone to error, and the maintenance and expansion of the system is unfavorable. These have been improved in the factory method model.

Factory method Mode:

Concept:

Also called a virtual constructor pattern or polymorphic factory, which first interfaces to create objects, and then lets subclasses decide which class to instantiate. This means that the factory method defers the instantiation of a class to its subclasses.

Structure diagram:

Analysis:

One: In the factory method model, the core factory class is no longer responsible for all product creation, but rather the creation of the task to the subclass to complete, which allows the system to create new products without modifying the factory role.

Two: Factory method mode centralized encapsulation of the creation of objects, in the replacement of objects, do not make large changes can be achieved, reduce the customer program and product object coupling, is a simple factory model of further abstraction and promotion.

Abstract Factory mode:

Concept:

Provides an interface that creates a series of related or interdependent objects without specifying their specific classes.

Structure diagram:

among them:

Abstractproducta and ABSTRACTPRODUCTB are two abstract products , both of which may be implemented in two different ways.

Product1,Product2,Product3,Product4 are the implementation of the specific classification of two abstract products.

Abstractfactory Abstract Factory interface that contains an abstract method for creating all products.

ConcreteFactory1 and ConcreteFactory2 are specific factories that implement methods for generating abstract products, producing a specific product .

Analysis:

One: Easy to Exchange product line, specific factory class in an application only need to initialize the time to appear once, which makes it easy to change the specific factory of an application, it only needs to change the specific factory can use different product configurations.

Two: Let the concrete creation process and the client separation, the client is through their abstract interface manipulation instances, product specific class name is also separated from the specific factory, will not appear in the customer code.

Three: The expansion of the product is cumbersome, laborious, if you need to add a new product, you have to change almost all of the factory class.

class instance diagram:

Simple Factory mode:

Factory method Mode:

Abstract Factory mode:

the comparison:

Product Family: A collection of products that belong to a different product hierarchy, but that function is interrelated.

Simple Factory mode:

Generate any product in the same tier

Create a factory, the role of the factory is to create interfaces

The factory class contains the necessary logical judgment to dynamically instantiate the related class based on the client's choice

Factory method :

Build a fixed product in a unified hierarchy

There is only one abstract product class that can derive a number of specific product classes

Each specific factory class creates an instance of a specific product class

The internal logic of the simple factory is moved to the client to implement , to add functionality , modify the client code

Customer Service Simple Factory violation of the open closure principle , to maintain the advantages of the encapsulation object creation process

Abstract Factory :

Create a product with an abstract factory

Production of all products of different product families

There are several abstract product classes , each of which can derive multiple specific product classes

Each specific factory class creates instances of multiple specific product classes

Simple Factory mode, factory method mode, abstract Factory 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.