Abstract Factory mode

Source: Internet
Author: User

2018年8月24日17:20:30

Abstract Factory Pattern Definition

Lend me a pencil of morning light and give it to you tomorrow.

Now I can also have your morning pencil, you are already gone, tomorrow or tomorrow.

Abstract Factory, which provides an interface for creating a series of related or interdependent objects without specifying their specific classes. --"design pattern: The basis of reusable object-oriented software"

Usage Scenarios

The first step is to recognize two new concepts: product family and product hierarchy .

Product Family, refers to the different product hierarchy structure, the function-related product group.

product Hierarchy Structure , that is, the product's inheritance structure, the inheritance (or implementation) of the same abstract product specific products belong to the same product hierarchy structure.

In the following code example, Chenguang stationery and True Color stationery are two product families, inheriting (or implementing) the pencil abstract product of the morning pencil and the true Color pencil is a product hierarchy structure that inherits (or implements) the eraser abstract product of the morning pencil and the true Color pencil is another product hierarchy. "This I use inheritance (or implementation) is because Java has an interface is implemented, abstract class is inherited, abstract products can be either an interface, or an abstract class, in order not to mislead myself, deliberately added." "Examples include: Intel's motherboard, chipset, CPU is a product family, AMD's motherboard, chipset, CPU is also a product family, and here are three product hierarchy, namely: Motherboard, chipset, CPU.

When we think of the code example, we think of the stationery, the morning light and the true color when we often use things. Checked, did not expect a small stationery, turnover over 1 billion.

Now our goal is to earn him a billion, open a blog garden stationery (product family), product hierarchy, they have, we directly inherit (or realize) their abstract product class is good, more convenient. This is the benefit of the abstract factory model. Add a product family, do not destroy the original code structure, in line with the opening and shutting principle.

But only the old products, there is no competitiveness, or we add a new product, namely, the level structure of products, and temporarily is the code of rural cheat Special smart pen, to help you easily deal with the written test, then we will be in the blog Park Stationery (product family) to add new members, in the blog Park stationery specific factory class to add related to create new does not conform to the opening and shutting principle. This is the drawback of the abstract factory model.

In summary, in combination with other cases, the following conditions can be used in the abstract Factory mode:

1. A system should not rely on the details of how a product instance is created, combined, and expressed, which is important for all types of factory models.

2. The system has more than one product, and only one product family is used at a time.

3, products belonging to the same product family will be used together, this constraint should be reflected in the system design.

4, all products with their own inheritance (or implementation) of the interface appears, so that the client does not rely on the implementation of concrete.

1th, no doubt. 2nd and 3rd, said that I choose the morning stationery, I use the morning light of the pencil will use the morning light of the eraser, to use, can not be I use the morning light of the pencil but use the eraser of the real color. This constraint seems a bit far-fetched in this example, but think about this scenario: The final exam to the same stationery, there are two options, one is the morning light, a true color, they are matching, the morning light of the pencil with the morning eraser, the real color of the pencil with the real color of the eraser, so comfortable. 4th, interface-oriented programming, reducing coupling.

2nd and 3rd, with the above mentioned Intel, AMD product family more appropriate, Intel's CPU is not compatible with AMD's Motherboard, AMD's CPU is not compatible with Intel's motherboard, so when assembling the computer, the default is this constraint: only the same product family of products can be used.

The origin of the abstract factory pattern is used to create Windows constructs that belong to different operating systems. For example, the command button and text box (text) are all Windows constructs, in the UNIX operating system Windows environment and Windows Operating System window environment, these two constructs have different local implementation, their details are different. Windows build, can only select an operating system button and text, if not, two operating systems are not used, I want to use this window?

Role

Abstract Factory: Factory-specific (product family) public interface

Specific factory role (concrete Factory): Product family

Abstract Product roles: Product hierarchy

Specific product roles (concrete product): Products

Icon

code example

code sample class dependency diagram:

Abstract Factory Role (Abstractfactory.java)

public interface AbstractFactory {    public Pencil createPencil();    public Eraser createEraser();}

Specific factory roles (Chenguangfactory.java, Truecolorfactory.java)

// 晨光工厂public class ChenGuangFactory implements AbstractFactory {    @Override    public Pencil createPencil() {        return new ChenGuangPencil();    }    @Override    public Eraser createEraser() {        return new ChenGuangEraser();    }}// 真彩工厂public class TrueColorFactory implements AbstractFactory{    @Override    public Pencil createPencil() {        return new TrueColorPencil();    }    @Override    public Eraser createEraser() {        return new TrueColorEraser();    }}

Abstract product roles (Pencil.java, Eraser.java)

// 铅笔public interface Pencil {    public void draw();}// 橡皮public interface Eraser {    public void erase();}

Specific product roles (Chenguangpencil.java, Truecolorpencil.java, Chenguangeraser.java, Truecoloreraser.java)

// 晨光铅笔public class ChenGuangPencil implements Pencil {    @Override    public void draw() {        System.out.println("用晨光铅笔画图。");    }}// 真彩铅笔public class TrueColorPencil implements Pencil {    @Override    public void draw() {        System.out.println("用真彩铅笔画图。");    }}// 晨光橡皮public class ChenGuangEraser implements Eraser {    @Override    public void erase() {        System.out.println("用晨光橡皮擦除晨光铅笔画的图。");    }}// 真彩橡皮public class TrueColorEraser implements Eraser {    @Override    public void erase() {        System.out.println("用真彩橡皮擦除真彩铅笔画的图。");    }}

Test Class (Abstractfactorytest.java)

public class AbstractFactoryTest {    public static void main(String[] args) {        // 1、晨光        AbstractFactory factory = new ChenGuangFactory();        Pencil pencil = factory.createPencil();        pencil.draw(); // 用晨光铅笔画图。        Eraser eraser = factory.createEraser();        eraser.erase(); // 用晨光橡皮擦除晨光铅笔画的图。                // 2、真彩        AbstractFactory factoryT = new TrueColorFactory();        Pencil pencilT = factoryT.createPencil();        pencilT.draw(); // 用真彩铅笔画图。        Eraser eraserT = factoryT.createEraser();        eraserT.erase(); // 用真彩橡皮擦除真彩铅笔画的图。    }}

Test results:

Advantages

1, with simple Factory mode, the same advantages of factory method mode: The client does not need to know the details of the object being created

2. When multiple objects in a product family are designed to work together, it ensures that the client always uses only the products from the same product family.

3, to increase product family, in line with the open and closed principle.

Disadvantages

1, increase the product grade structure, does not conform to the opening and shutting principle.

Summarize

Abstract Factory mode on the basis of the factory method model, add 产品族 the 产品等级结构 concept of and only use the same product family constraints, so that the abstract factory pattern conforms to some real life scenes, become a practical design pattern, coupled with the abstract Factory mode to achieve high cohesion and low coupling, It also provides the basis for its wide application.

Finish

(to the morning and true color advertising, is it supposed to collect some advertising fees??? )

Reference:

The abstract factory model of Java and patterns

2018年8月25日18:31:58

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.