Design Pattern -- abstract factory pattern Learning

Source: Internet
Author: User

Design Pattern -- abstract factory pattern Learning

To understand the design pattern correctly, we must first clarify what problems it proposes to solve.

Abstract Concept of factory design patterns:

For the abstract factory design model, I have found a lot of materials. I feel that only those who involve the product level and product family can understand the essence of the abstract factory design model, the factory method mode is for a product level structure, while the abstract factory mode is for multiple product levels. Some people think that the abstract factory model is to solve the coupling problem between client code and factory class. I think the solution of this idea is only an application of the simple factory model, the abstract factory mode is as follows:

Factory mode + simple factory mode = Abstract Factory mode, which is incorrect.

 

Problems:

 

The hierarchical structure of multiple products. The relative factory model is for a product level structure.

 

Example description

 

We use a simple example to explain the abstract design pattern, and this example runs through the full text:

---------------------------------------------------------

 

When assembling a computer, we usually need to select a series of accessories, such as CPU, hard disk, memory, motherboard, power supply, and chassis. For the sake of simplicity, we only need to consider the choice of CPU and motherboard.

In fact, when selecting a CPU, there are a series of problems, such as the brand, model, number of pins, and clock speed. Only by determining these problems can we determine the specific CPU.

Similarly, when selecting a motherboard, there are also a series of problems, such as brand, chipset, integrated chip, bus frequency, and so on. Only these problems are identified can the specific motherboard be determined.

Choosing a different CPU and motherboard is the requirement that each customer puts forward to the installation company when assembling a computer, that is, the installation solution developed by each of us.

Before final determining the installation scheme, we also need to consider the compatibility between various accessories as a whole. For example, CPU and motherboard cannot be assembled if Intel CPU and AMD motherboard are used. Because the number of Intel CPU pins is not compatible with the CPU plug-in provided by the AMD motherboard, that is to say, if Intel's CPU is used, it cannot be inserted into the AMD motherboard, so the installation solution is holistic, the selected accessories are associated.

For the installation engineer, he only knows how to assemble a computer and the relevant accessories, but the customer has to say what kind of accessories to use. That is to say, the installation engineer is only responsible for assembly, and the customer is responsible for selecting the specific accessories required for assembly. Therefore, when the installation engineers assemble computers for different customers, they only need to obtain the corresponding accessories according to the customer's installation scheme and then assemble the computers.

---------------------------------------------------------

 

Two basic concepts:

 

Before learning the specific example of an abstract factory, you need to understand two basic concepts: Product Family and product level.

 

A product family is a family of products with functions associated with different product levels. For example, AMD's motherboard, chipset, and CPU constitute a family, while Intel's motherboard, chipset, and CPU constitute a family.

Both families come from three product levels: motherboard, chipset, and CPU. A hierarchical structure consists of products with the same structure, as shown below:

 

What is the difference between the factory model and the abstract factory model for Program Design Based on the above example?

 

 

Factory model:

The preceding three different hierarchical structures have parallel structures. Therefore, if the factory method is used, it is necessary to use three independent factory level structures to deal with these three product levels. The similarity between the three product levels leads to three parallel factory levels. As the number of product level structures increases, the number of factory level structures given by the factory method model also increases. For example:

 

 

 

Abstract Factory mode:


 

 

From the above two figures, we can see that a factory level structure can create all objects in a product family with different product levels. Obviously, the abstract factory mode is more efficient than the simple factory mode and factory method mode at this time. Each product family has a specific factory. Each specific factory is responsible for creating products that belong to the same product family but belong to different levels of structure.


 

The abstract factory pattern design class diagram is as follows:

 

 

The source code is as follows:

 

Abstract component Cpu

 

public interface Cpu {public void calculate();}

Specific components: IntelCpu and AmdCpu

 

 

Public class IntelCpu implements Cpu {private int pins; // indicates the number of CPU pins. public IntelCpu (int pins) {this. pins = pins;} @ Overridepublic void calculate () {System. out. println (number of Intel CPU pins: + pins );}}

Public class AmdCpu implements Cpu {private int pins; public AmdCpu (int pins) {this. pins = pins;} @ Overridepublic void calculate () {System. out. println (cmd cpu pin quantity: + pins );}}

Abstract component MainBoard

 

 

public interface MainBoard {public void installCpu();}

Specific components: IntelMainBoard and AmdMainBoard

 

 

Public class IntelMainBoard implements MainBoard {private int cpuHoles; // indicates the number of CPU slot holes on the motherboard, and the number of corresponding pins public IntelMainBoard (int cpuHoles) {this. cpuHoles = cpuHoles;} @ Overridepublic void installCpu () {System. out. println (number of CPU slot holes on the Intel motherboard: + cpuHoles );}}

Public class AmdMainBoard implements MainBoard {private int cpuHoles; public AmdMainBoard (int cpuHoles) {this. cpuHoles = cpuHoles;} @ Overridepublic void installCpu () {System. out. println (number of CPU slot holes on AMD motherboard: + cpuHoles );}}

The abstract factory AbstractFactory is set and is not responsible for manufacturing specific products:

 

 

Public interface AbstractFactory {/*** create Cpu object * @ return Cpu object */public Cpu createCpu (); /*** create a MainBoard object * @ return MainBoard object */public MainBoard creatMainBoard ();}

Design a specific factory to generate Intel and Amd-branded CPU and Motherboard

 

 

/*** Used to create all Intel-related products */public class IntelFactory implements AbstractFactory {@ Overridepublic Cpu createCpu () {// TODO Auto-generated method stubreturn new IntelCpu (555);} @ Overridepublic MainBoard creatMainBoard () {// TODO Auto-generated method stubreturn new IntelMainBoard (555 );}}

/*** Used to create AMD products */public class AmdFactory implements AbstractFactory {@ Overridepublic Cpu createCpu () {return new AmdCpu (8888);} @ Overridepublic MainBoard creatMainBoard () {return new AmdMainBoard (8888 );}}

 

Our installation engineers only need to perform installation operations, but the user determines which product series to install:

ComputerEngineer

Public class ComputerEngineer {private Cpu cpu = null; private MainBoard mainBoard = null; public void makeComputer (AbstractFactory af) {// 1. prepare the installed hardware. prepareHardWares (af); // 2. assemble the machine // 3. test Machine // 4. deliverable} public void prepareHardWares (AbstractFactory af) {this. cpu = af. createCpu (); // prepare the CPU, but do not care what CPUthis is. mainBoard = af. creatMainBoard (); // prepare the motherboard and do not care about the motherboard. // check whether the slot hole matches the number of pins. this. cpu. calculate (); this. mainBoard. installCpu ();}}

Client

Public class Client {public static void main (String [] args) {ComputerEngineer cEngineer = new ComputerEngineer (); AbstractFactory intelFactory = new IntelFactory (); // The customer selects a specific factory, the factory is responsible for the production matching component cEngineer. makeComputer (intelFactory); // engineer starts Assembly }}

The abstract factory function is to create an interface for a series of related objects or mutually dependent objects. Be sure to note that the methods in this interface are not arbitrarily stacked, but a series of related or interdependent methods. For example, the motherboard and CPU used in the above example are used to assemble related objects of a computer. Different Installation Schemes represent a specific computer series.


 

 

Since a series of objects defined by the abstract factory are usually related or mutually dependent, these product objects constitute a product family, that is, the abstract factory defines a product family.

This brings great flexibility. When switching product families, you only need to provide different abstract factory implementations. That is to say, a product family is switched as a whole.


Usage:

 

1. A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.

2. There are more than one product family in this system, and the system only consumes products of one of them.

3. products belonging to the same product family are used together. This constraint must be reflected in the system design. (For example, the Intel motherboard MUST use an Intel CPU or an Intel chipset)

4. The system provides a product library. All products use the same interface, so that the client does not rely on implementation.


Advantages:

 

1. Separation interfaces and Implementations

The client uses an abstract factory to create the required objects, and the client does not know the specific implementation. The client is only for product-oriented interface programming. That is to say, the client is decoupled from the specific product implementation.

 

2. Easy to switch and add product families

Because a specific factory implementation represents a product family, for example, from the Intel series to the AMD series in the above example, you only need to switch the specific factory. I can also add a new installation solution, a new product family, which is very convenient.


Disadvantages:

 

It is not easy to expand new product levels, such as adding a hard disk or memory. Modify the abstract factory so that all factory implementation classes can be modified.

 

 

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.