Abstract Factory-The most understandable design pattern parsing

Source: Internet
Author: User
Tags abstract
1. Introduction 1.1 Definitions

Abstract Factorypattern, which provides an interface for creating a series of related or interdependent objects without specifying their specific classes, and the specific factory is responsible for implementing specific product instances.

The biggest difference between the abstract factory model and the factory method pattern is that each plant in the abstract factory can create a variety of products, whereas factory methods can only create a class of 1.2 main functions per plant

Allows the use of an abstract interface to create a set of related products without needing to know or care what the actual product is, so that it can be decoupled from the specific product. 1.3 Resolved issues

Only one type of product can be created per factory

The disadvantage of the factory method mode 2. Mode principle 2.1 UML class diagram

2.2 Pattern Composition

composition (role) Relationship function
Abstract product family (abstractproduct) Abstract product's parent class Describe the public interface of an abstract product
Abstract (product) The parent of the specific product Describe the public interface for a specific product
Specific products (concrete product) A subclass of an abstract product; a target class created by a factory class Describe specific products for production
Abstract Factory (Creator) The parent class of the specific factory Describe the public interface of a specific factory
Specific factory (concrete Creator) A subclass of an abstract factory; Called by the outside world. Describe specific factories; Implement FactoryMethod factory method create an instance of a product

How to understand the differences between abstract product families, abstract products and specific products? Take a look at the picture below

2.3 Use steps

Step 1: Create an abstract factory class that defines the public interface for the specific factory;
Step 2: Create the abstract product race, define the public interface of the abstract product;
Step 3: Create abstract product classes (inherit abstract product races), define the public interface of the specific product;
Step 4: Create specific product classes (inherit abstract product classes) & Define specific products for production;
Step 5: Create a specific factory class (inherit abstract factory Class), define the method to create the corresponding product instance;
Step 6: The client creates an instance of a different product class by instantiating the specific factory class and invoking its method of creating different target products 3. Example explanation

Next I'll use an example to introduce the abstract factory pattern in a step further. 3.1 Example background: Xiao Cheng has two plastic processing plants (a factory only produces container products; b plant only produces mold products); With the change of customer demand, a factory location of customers need also mold products, B factory where customers also need container products; conflict: no Resources (funds + Rental) in the local opening of a number of injection molding plant solutions: In the original two plastic factory to add the production requirements of the function, that a plant can produce containers + mold products, b factory can produce mold + container products.

Abstract Factory mode 3.2 use steps

Step 1: Create an abstract factory class that defines a common interface for a specific factory

Abstract class factory{public
   abstract Product manufacturecontainer ();
    Public abstract Product manufacturemould ();
}

Step 2: Create an abstract product race that defines the public interface for the specific product;

Abstract class abstractproduct{public
    abstract void Show ();
}

Step 3: Create an abstract product class that defines the public interface for the specific product;

Container products Abstract class
Containerproduct extends abstractproduct{@Override public
    abstract void Show ();
}

Mold Products Abstract class
Mouldproduct extends abstractproduct{@Override public
    abstract void Show ();
}

Step 4: Create a specific product class (inherit abstract product Class), define the production of specific products;

Container product A class
Containerproducta extends containerproduct{
    @Override public
    void Show () {
        System.out.println ("Production of container Product A");}
}

Container Product B class
CONTAINERPRODUCTB extends containerproduct{
    @Override public
    void Show () {
        System.out.println ("Production of container Product B");}
}

Mould Products Class A classes
mouldproducta extends mouldproduct{

    @Override public
    void Show () {
        System.out.println ("Production of mold Product A");}
}

Mould Products Class B classes
MOULDPRODUCTB extends mouldproduct{

    @Override public
    void Show () {
        System.out.println ("Production of mold product B");
    }
}

Step 5: Create a Specific factory class (inherit abstract factory Class), define the method to create the corresponding product instance;

Factory A-production mold + container product
class Factorya extends factory{

    @Override public
    product Manufacturecontainer () {
        return new Containerproducta ();
    }

    @Override public
    Product manufacturemould () {
        return new mouldproducta ();
    }
}

Factory B-Production mold + container product
class Factoryb extends factory{

    @Override public
    product Manufacturecontainer () {
        return new CONTAINERPRODUCTB ();
    }

    @Override public
    Product manufacturemould () {
        return new MOULDPRODUCTB ();
    }
}

Step 6: Clients create instances of different product classes by instantiating specific factory classes and invoking methods that create different target products

Production Workflow Public
class Abstractfactorypattern {public
    static void Main (string[] args) {
        Factorya Mfactorya = New Factorya ();
        Factoryb Mfactoryb = new Factoryb ();
        A factory local customer needs container product a
        mfactorya.manufacturecontainer (). Show ();
        A factory local customers need mold product a
        mfactorya.manufacturemould (). Show ();

        B Factory local customers need container product B
        mfactoryb.manufacturecontainer (). Show ();
        B factory local customers need mold product B
        mfactoryb.manufacturemould (). Show ();

    }
}

Results:

Production of a container product a
produced a container product B
produced a mold product a produced a
mold product b
4. AdvantagesReduced coupling
Abstract Factory mode creates a specific product DelayTo the sub-class of the specific factory, so that the creation of the object is encapsulated, can reduce the dependence between the client and the specific product class, so that the system coupling is low, which is more conducive to later maintenance and expansion, more in line with the open-closed principle
When you add a product class, you only need to add the specific product class and the corresponding factory subclass to

Simple Factory mode needs to modify the judgment logic of the factory class

Conforms to the principle of single responsibility
Each specific factory class is responsible for creating the corresponding product

Complex switch logic judgments for factory classes in simple factories

You can form an inheritance-based hierarchy without using the static factory method.

Factory class for simple Factory mode using static Factory method 5. Disadvantages Abstract Factory mode is difficult to support the change of new kinds of products.
This is because the abstract factory interface has identified a collection of products that can be created, and if a new product needs to be added, it is necessary to modify the interface of the abstract factory, which involves the transformation of the abstract factory class and all subclasses, thus violating the "develop-close" principle.

The new product family conforms to the open-close principle; for the new product category does not conform to the open-closed principle, this characteristic is called the open-close principle the inclination. 6. Application Scenarios

After understanding the pros and cons, I summed up the application scenario for the factory approach pattern: A system that does not require reliance on how product class instances are created, combined, and expressed, is a prerequisite for all plant model applications. This system has a number of series of products, and the system only consumes one of the series system requirements to provide a Product class library, all products with the same interface appear, the client does not need to rely on the implementation of concrete.

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.