JAVA design pattern-abstract factory pattern

Source: Internet
Author: User

JAVA design pattern-abstract factory pattern

The previous chapter describes the factory method model. We know that there is only one abstract Product role in the factory method model, and the specific factory role corresponds to the specific product role one by one.

The difference between Abstract Factory roles and specific factory roles today is that abstract factory roles can have multiple abstract product roles, and each abstract Product role can have multiple specific product roles,

The structure of the factory role remains unchanged. An abstract factory role is used to derive multiple factory roles, but multiple product roles can be created in a specific factory role.

For example:

When assembling a computer, we often need to choose a series of accessories, such as CPU and motherboard. Some problems are encountered when selecting the CPU, such as the brand, model, and number of pins.

Question. After we select the CPU and motherboard, we start to assemble the computer, but we also need to consider the problem of supporting CPU and motherboard before installation. That is to say, the installation solution is holistic and the selected hardware is associated.

Next we will use the simple factory mode and the abstract factory mode for installation. Through this example, we will observe the similarities and differences between the abstract factory mode and the factory mode.

Simple factory mode installation:

Abstract CPU and motherboard code:

Package hirain. simple. cpu;/*** CPU interface */public interface CPUService {/*** CPU computing function */public void calculate ();}
Package hirain. simple. mainboard; public interface MainBoardService {/*** CPU can be installed on the motherboard */public void installCPU ();}

Specific CPU and motherboard Product Code:

CPU: amd cpu and INTEL CPU

Package hirain. simple. cpu;/*** amd cpu implementation */public class AMDCPU implements CPUService {/*** Number of CPU pins */private int pins = 0; /*** constructor: Number of CPU pins passed in * @ param pins Number of CPU pins */public AMDCPU (int pins) {this. pins = pins;} public void calculate () {System. out. println ("Intel CPU pins =" + pins );}}
Package hirain. simple. cpu;/*** Intel CPU implementation */public class IntelCPU implements CPUService {/*** Number of CPU pins */private int pins = 0; /*** constructor: Number of CPU pins passed in * @ param pins Number of CPU pins */public IntelCPU (int pins) {this. pins = pins;} public void calculate () {System. out. println ("Intel CPU pins =" + pins );}}
The motherboard has a gigabyte motherboard and a Microstar motherboard.

Package hirain. simple. mainboard;/*** */public class GAMainboard implements MainBoardService {/*** number of holes in the CPU slot */private int cpuHoles = 0; /*** constructor: Enter the number of holes in the CPU slot * @ param cpuHoles number of holes in the CPU slot */public GAMainboard (int cpuHoles) {this. cpuHoles = cpuHoles;} public void installCPU () {System. out. println ("Number of CPU slot holes in the gigabyte motherboard =" + cpuHoles );}}
Package hirain. simple. mainboard;/*** MSI motherboard */public class MSIMainboard implements MainBoardService {/*** number of holes in the CPU slot */private int cpuHoles = 0; /*** constructor: Enter the number of holes in the CPU slot * @ param cpuHoles number of holes in the CPU slot */public MSIMainboard (int cpuHoles) {this. cpuHoles = cpuHoles;} public void installCPU () {System. out. println ("Number of CPU slot holes in the gigabyte motherboard =" + cpuHoles );}}
There are two factories: the factory that obtains the CPU and the factory that obtains the motherboard.

Package hirain. simple. factory; import hirain. simple. cpu. AMDCPU; import hirain. simple. cpu. CPUService; import hirain. simple. cpu. intelCPU; /*** create a simple CPU factory */public class CPUFactory {/*** method for creating a CPU interface object * @ param type select the cputype parameter * @ return of the CPU interface object method */public static CPUService createCPU (int type) {CPUService cpu = null; // select and create the corresponding CPU object if (type = 1) {cpu = new IntelCPU (1156 );} else if (type = 2) {cpu = new AMDCPU (939) ;}return cpu ;}}
Package hirain. simple. factory; import hirain. simple. mainboard. GAMainboard; import hirain. simple. mainboard. MSIMainboard; import hirain. simple. mainboard. mainBoardService; /*** create a simple factory for the motherboard */public class MainboardFactory {/*** method for creating the interface object for the motherboard * @ param type select the parameter of the motherboard type * @ return interface object for the motherboard method */public static MainBoardService createMainboard (int type) {MainBoardService mainboard = null; // select and create the corresponding motherboard object if (type = 1) {mainboard = new GAMainboard (1156 );} else if (type = 2) {mainboard = new MSIMainboard (939) ;}return mainboard ;}}
Installer code:

Package hirain. simple. person; import hirain. simple. cpu. CPUService; import hirain. simple. factory. CPUFactory; import hirain. simple. factory. mainboardFactory; import hirain. simple. mainboard. mainBoardService;/*** installation engineer's class */public class Person {/*** defines the CPU required for assembling the machine */private CPUService cpu = null; /*** define the motherboard required for assembling the machine */private MainBoardService mainboard = null;/*** installation process ** @ param cpuType the customer selects the type of CPU required * @ para M mainboardType select the type of the required motherboard */public void makeComputer (int cpuType, int mainboardType) {// 1: first, prepare the required accessories prepareHardwares (cpuType, mainboardType ); // 2: assemble the machine // 3: test the machine // 4: delivery customer}/*** accessories required for installation * @ param cpuType customer selects the type of the required CPU * @ param mainboardType customer selects the type of the required motherboard */private void prepareHardwares (int cpuType, int mainboardType) {// here we need to prepare the specific implementation of the CPU and motherboard. For the sake of simplicity, we only need to prepare the two. // However, the installation engineer does not know how to create them. What should I do? // Directly find the corresponding factory to obtain this. cpu = CPUFactory. createCPU (cpuType); this. mainboard = MainboardFactory. createMainboard (mainboardType); // test whether the accessory is easy to use this. cpu. calculate (); this. mainboard. installCPU ();}}
Client code:

Package hirain. simple; import hirain. simple. person. person; public class Client {public static void main (String [] args) {// create an installation engineer object Person = new person (); // inform the installation engineer of the accessories selected by the installation engineer so that the installation engineer can assemble the computer person. makeComputer (2, 1 );}}
Through the above code, we can find the problem:

The computer may not be assembled, and the CPU and motherboard may not be compatible. That is to say, the simple factory and factory method mode only care about the creation of a single product, it doesn't matter whether there is dependency between products.

Therefore, the abstract factory model came into being to solve similar problems.

Let's take a look at the implementation code of the abstract factory model:

First, the code of the abstract product and specific product remains unchanged.

Factory Code changes:

Package hirain. abstract1.factory; import hirain. abstract1.cpu. CPUService; import hirain. abstract1.mainboard. mainBoardService;/*** Abstract Factory interface, declare the operation to create an abstract product object */public interface AbstractFactory {/*** create a CPU object * @ return CPU object */public CPUService createCPU (); /*** create the motherboard object * @ return the motherboard object */public MainBoardService createMainboard ();}
Package hirain. abstract1.factory; import hirain. abstract1.cpu. CPUService; import hirain. abstract1.cpu. intelCPU; import hirain. abstract1.mainboard. GAMainboard; import hirain. abstract1.mainboard. mainBoardService;/*** installation solution 1: Intel's CPU + gigabyte motherboard * This corresponds to the CPU and motherboard object created here, */public class Schema1 implements matching actfactory {public CPUService createCPU () {return new IntelCPU (1135);} public MainBoardService createMainboard () {return new GAMainboard (1135 );}}
Package hirain. abstract1.factory; import hirain. abstract1.cpu. AMDCPU; import hirain. abstract1.cpu. CPUService; import hirain. abstract1.mainboard. MSIMainboard; import hirain. abstract1.mainboard. mainBoardService;/*** Installation Method 2: amd cpu + MSI motherboard * This corresponds to the CPU and motherboard object created here, */public class Schema2 implements matching actfactory {public CPUService createCPU () {return new AMDCPU (939);} public MainBoardService createMainboard () {return new MSIMainboard (939 );}}
Installer code:

Package hirain. abstract1.person; import hirain. abstract1.cpu. CPUService; import hirain. abstract1.factory. abstractFactory; import hirain. abstract1.mainboard. mainBoardService;/*** installation engineer's class */public class Person {/*** defines the CPU required for assembling the machine */private CPUService cpu = null; /*** define the motherboard required for assembling the machine */private MainBoardService mainboard = null;/*** installation process ** @ param schema the installation solution selected by the customer */public void makeComputer (AbstractFact Ory schema) {// 1: first, prepare the required accessory prepareHardwares (schema); // 2: assemble the machine // 3: test the machine // 4: delivery customer}/*** assembly required for installation * @ param schema installation solution selected by the customer */private void prepareHardwares (AbstractFactory schema) {// here we will prepare the specific implementation of the CPU and motherboard. For the sake of simple examples, we will only prepare the two. // However, the installation engineer does not know how to create them. What should I do? // Use the abstract factory to obtain the corresponding interface object this. cpu = schema. createCPU (); this. mainboard = schema. createMainboard (); // test whether the accessory works well with this. cpu. calculate (); this. mainboard. installCPU ();}}
Client code

Package hirain. abstract1; import hirain. abstract1.factory. abstractFactory; import hirain. abstract1.factory. schema2; import hirain. abstract1.person. person; public class Client {public static void main (String [] args) {// create an installation engineer object Person = new person (); // The customer selects and creates the desired installation solution object AbstractFactory schema = new Schema2 (); // inform the installation engineer of the installation solution and let the installation engineer assemble the computer person. makeComputer (schema );}}
In actual work, this model has never been used, and I don't know if it will be used in the future. Let's talk about the differences between the abstract factory and the factory model:

1. The factory method is an abstract product that can be derived from multiple specific products. An abstract factory is an abstract product that can be derived from multiple specific products.

2. Factory method each specific factory class can only create one specific product class object. Abstract Factory each specific factory class can create multiple specific product class objects.


















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.