Design Mode 08-Abstract Factory (creation type)

Source: Internet
Author: User
1. Scenario Simulation

Take a common example in our daily life: when assembling a computer, we usually need to select a series of accessories. For example, when selecting a CPU, pay attention to the brand, model, and number of pins, clock speed. Only when these values are determined can a CPU be determined. The same is true for the motherboard. For the installation engineer, he only knows that the relevant accessories are required to install a computer. The customer determines the specific accessories.
2. General Solution (simple factory) 2.1cpu Interface

Package demo06.abstractfactory. example1;/*** CPU interface */public interface cpuapi {/*** method, CPU has computing function */Public void calculate ();}
2.2 motherboard Interface
Package demo06.abstractfactory. example1;/*** main board interface */public interface mainboardapi {/*** probe method. The main board has the CPU installation function */Public void installcpu ();}
2.3 inter CPU implementation
Package demo06.abstractfactory. example1;/*** Intel CPU implementation */public class intelcpu implements cpuapi {/*** 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 ("now in Intel CPU, pins =" + pins );}}
2.4 amd cpu implementation
Package demo06.abstractfactory. example1;/*** amd cpu implementation */public class amdcpu implements cpuapi {/*** 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 ("now in amd cpu, pins =" + pins );}}
2.5 implement the motherboard of Gigabyte
Package demo06.abstractfactory. example1;/*** */public class gamainboard implements mainboardapi {/*** number of holes in the CPU slot */private int cpuholes = 0; /*** constructor: 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 ("now in gamainboard, cpuholes =" + cpuholes );}}
2.6 MSI Motherboard
Package demo06.abstractfactory. example1;/*** MSI motherboard */public class msimainboard implements mainboardapi {/*** number of holes in the CPU slot */private int cpuholes = 0; /*** constructor: 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 ("now in msimainboard, cpuholes =" + cpuholes );}}
2.7 create a CPU Factory
Package demo06.abstractfactory. example1; /*** 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 cpuapi createcpuapi (INT type) {cpuapi CPU = NULL; // select and create a CPU object based on the parameter if (type = 1) {CPU = new intelcpu (1156 );} else if (type = 2) {CPU = new amdcpu (939) ;}return CPU ;}}
2.8 create a motherboard Factory
Package demo06.abstractfactory. example1; /*** 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 Motherboard method of the interface object */public static mainboardapi createmainboardapi (INT type) {mainboardapi 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 ;}}
2.9 engineer implementation code
Package demo06.abstractfactory. example1;/*** installation engineer's class */public class computerengineer {/*** defines the CPU required for assembling the machine */private cpuapi CPU = NULL; /*** define the motherboard required for assembling the machine */private mainboardapi mainboard = NULL; /*** installation process ** @ Param cputype * the customer selects the type of the required CPU * @ Param mainboardtype * the customer selects the type of the required motherboard */Public void makecomputer (INT cputype, int mainboardtype) {// 1: first, prepare the required accessories preparehardwares (cputype, mainboardtype); // 2: Assembly Device // 3: Test Machine // 4: delivery customer}/*** accessories required for installation ** @ Param cputype * the customer selects the type of the required CPU * @ Param mainboardtype * the 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. createcpuapi (cputype); this. mainboard = mainboardfactory. createmainboardapi (mainboardtype); // test whether the accessory is easy to use this. CPU. calculate (); this. mainboard. installcpu ();}}
2.90 client Test
Package demo06.abstractfactory. example1; public class client {public static void main (string [] ARGs) {// create an installation engineer object computerengineer engineer = new computerengineer (); // inform the installation engineer of the accessories selected by the installation engineer so that the installation engineer can assemble the computer engineer. makecomputer (1, 2 );}}
3. Problem:

The problem arises: if the client passes in the parameter 1, 2, the installation will fail if it does not match, so the problem will arise. How can this problem be solved?
4. solution (Abstract Factory mode) 4.1 mode definition:

Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
4.2 schema structure and description

 
5. Sample Code (Abstract Factory mode) 5.1 Abstract Factory Interface

Package demo06.abstractfactory. example2;/*** Abstract Factory interface, declare the operation for creating abstract product objects */public interface abstractfactory {/*** example method, create the object ** @ return of abstract product A */Public abstractproducta createproducta ();/*** example method, create the object ** @ return of abstract product B */Public abstractproductb createproductb ();}
5.2 product A Interface
Package demo06.abstractfactory. example2;/*** abstract product A interface */public interface abstractproducta {// define operations related to abstract product}
5.3 product B Interface
Package demo06.abstractfactory. example2;/*** abstract product B interface */public interface abstractproductb {// define operations related to abstract product B}
5.4 product A Implementation
Package demo06.abstractfactory. example2;/*** specific implementation of product A */public class producta1 implements abstractproducta {// implement the operation defined in the interface of product A} package demo06.abstractfactory. example2;/*** specific implementation of product A */public class producta2 implements abstractproducta {// implement the operations defined in the interface of product}
5.5 product B Implementation
Package demo06.abstractfactory. example2;/*** specific implementation of product B */public class productb1 implements abstractproductb {// implement the operation defined in the interface of product B} package demo06.abstractfactory. example2;/*** specific implementation of product B */public class productb2 implements abstractproductb {// implement the operations defined in the interface of product B}
5.6 specific factory implementation
Package demo06.abstractfactory. example2;/*** specific factory implementation object to create specific product objects */public class concretefactory1 implements abstractfactory {public abstractproducta createproducta () {return New producta1 ();} public abstractproductb createproductb () {return New productb1 () ;}} package demo06.abstractfactory. example2;/*** specific factory implementation object to create specific product objects */public class concretefactory2 implements abstractfactory {public abstractproducta createproducta () {return New producta2 ();} public abstractproductb createproductb () {return New productb2 ();}}
5.7 Client
Package demo06.abstractfactory. example2; public class client {public static void main (string [] ARGs) {// create an abstract factory object abstractfactory AF = new concretefactory1 (); // obtain a series of objects through the abstract factory, such as product A and product BAF. createproducta (); AF. createproductb ();}}
6. Rewrite the sample code 6.1 Structure Diagram

 
6.2 Abstract Factory Interface

Package demo06.abstractfactory. example3;/*** Abstract Factory interface, declare the operation to create an abstract product object */public interface abstractfactory {/*** create a CPU object ** @ return CPU object */Public cpuapi createcpuapi (); /*** create a main board object ** @ return main board object */Public mainboardapi createmainboardapi ();}
6.3 installation solution 1
Package demo06.abstractfactory. example3;/*** installation solution 1: Intel's CPU + gigabyte motherboard corresponds to the CPU and motherboard object created here, */public class schema1 implements matching actfactory {public cpuapi createcpuapi () {return New intelcpu (1156);} public mainboardapi createmainboardapi () {return New gamainboard (1156 );}}
6.4 installation solution 2
Package demo06.abstractfactory. example3;/*** installation solution 2: AMD's CPU + MSI motherboard corresponds to the CPU and motherboard object created here, */public class schema2 implements matching actfactory {public cpuapi createcpuapi () {return New amdcpu (939);} public mainboardapi createmainboardapi () {return New msimainboard (939 );}}
6.5 installation engineer
Package demo06.abstractfactory. example3;/*** installation engineer's class */public class computerengineer {/*** defines the CPU required for assembling the machine */private cpuapi CPU = NULL; /*** define the motherboard required for assembling the machine */private mainboardapi mainboard = NULL; /*** installation process ** @ Param schema * installation solution selected by the customer */Public void makecomputer (abstractfactory schema) {// 1: first, prepare the required accessories preparehardwares (schema); // 2: assemble the machine // 3: test the machine // 4: delivery customer}/*** the installation party selected by the customer to prepare the accessories required for installation ** @ Param schema * Case */private void preparehardwares (abstractfactory schema) {// here we are going to prepare the specific implementation of the CPU and motherboard. For the sake of simplicity, only the two are prepared here. // However, the installation engineer does not know how to create it. What should I do? // Use the abstract factory to obtain the corresponding interface object this. CPU = schema. createcpuapi (); this. mainboard = schema. createmainboardapi (); // test whether the accessory works well with this. CPU. calculate (); this. mainboard. installcpu ();}}
6.6 client usage
Package demo06.abstractfactory. example3; public class client {public static void main (string [] ARGs) {// create an installation engineer object computerengineer engineer = new computerengineer (); // The customer selects and creates the desired installation solution object abstractfactory schema = new schema1 (); // inform the installation engineer of the installation solution and let the installation engineer assemble the computer engineer. makecomputer (schema );}}
7. Introduction to abstract factory model 7.1 Abstract Factory and Dao

Dao is a data access object and a standard mode in J2EE. It is a design mode generated to solve different data access problems. In short, this design pattern is the thought of hierarchical design.
When implementing the DAO mode, the most common method is to abstract the factory mode. Of course, it is best to combine the factory method mode.
7.2 Advantages and Disadvantages of Abstract Factory

Advantages: separated interfaces and Implementations
This makes it easier to switch between product clusters.
Disadvantage: it is not easy to expand new products
Complex classes
7.3 abstract the essence of the factory Model

Select Product cluster implementation

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.