Java design pattern-Abstract Factory mode

Source: Internet
Author: User

The previous chapter is about the factory method pattern, and we know that the factory method pattern has only one abstract product role and that the specific factory role corresponds to the specific product role one by one.

The difference between an abstract factory role and a specific factory role today is that abstract factory roles can have multiple abstract product roles, and each abstract product role can derive multiple specific product roles.

The factory role structure does not change, or an abstract factory role derives multiple specific factory roles, but a specific factory role can create multiple specific product roles.

As an example:

When we assemble the computer, we often choose a series of accessories, such as CPU, motherboard and so on. Choose the CPU when faced with some problems, such as brand, model, PIN number, etc. select motherboard also face the same some questions

Problem. When we finish selecting the CPU and the motherboard, we start assembling the computer, but also consider the CPU and motherboard matching problems before installing. In other words, the installation scheme is integrated, and the hardware of choice is related.

Here we use the Simple factory model and the abstract Factory mode to carry out the installation, through this example to see how the abstract factory model and Factory mode are different.

Simple Factory mode installed:

Abstract CPU and Motherboard code:

Package hirain.simple.cpu;/** * CPU Interface */public interface cpuservice{    /**     * CPU has operation function */     public    Void Calculate ();}
Package Hirain.simple.mainboard;public interface Mainboardservice {    /**     * Motherboard can install CPU */public    void Installcpu ();}

Specific CPU and motherboard product codes:

CPU with AMD CPU and Intel CPU

package hirain.simple.cpu;/** * AMD CPU Implementation */public class AMDCPU implements cpuservice{/** * CPU PIN number */PR    ivate int pins = 0;    /** * Construction method, number of pins passed into the CPU * @param pins CPU PIN Number * * Public amdcpu (int pins) {this.pins = pins;    } public void Calculate () {System.out.println ("Number of Intel CPU pins =" +pins); }}
Package hirain.simple.cpu;/** *intel CPU Implementation */public class INTELCPU implements cpuservice{/**     * Number of pins on the CPU     * /    private int pins = 0;    /**     * Construction method, number of pins passed into the CPU     * @param pins CPU PIN Number * * Public    intelcpu (int pins) {       this.pins = pins;    } Public    Void Calculate () {       System.out.println ("Number of Intel CPU pins =" +pins);}    }
motherboard with gigabyte motherboard and MSI motherboard

package    hirain.simple.mainboard;/** * Gigabyte motherboard */public class Gamainboard implements Mainboardservice {/** * number of holes in the CPU socket */    private int cpuholes = 0;  /** * Construction method, number of holes passed into the CPU slot * @param cpuholes CPU Slots */public gamainboard (int cpuholes) {this.cpuholes =    Cpuholes;    } public void Installcpu () {System.out.println ("gigabyte motherboard CPU Socket hole number =" +cpuholes); }}
Package hirain.simple.mainboard;/** * MSI motherboard */public class Msimainboard implements mainboardservice{    /**     * Number of holes in the CPU slot     */    private int cpuholes = 0;    /**     * Construction method, number of holes passed into the CPU slot     * @param cpuholes CPU Slots *    /public msimainboard (int cpuholes) {       This.cpuholes = Cpuholes;    }    public void Installcpu () {       System.out.println ("gigabyte motherboard CPU Socket hole number =" +cpuholes);}    }
There are two factories: the factory that gets the CPU and the factory that gets the motherboard.

package Hirain.simple.factory;import Hirain.simple.cpu.amdcpu;import Hirain.simple.cpu.cpuservice;import hirain.simple.cpu.intelcpu;/** * Simple factory to create CPU */public class Cpufactory {/** * method to create CPU interface objects * @param type Select CPU Class       Type of parameter * @return CPU Interface Object method */public static cpuservice createcpu (int type) {Cpuservice CPU = NULL;       Select and create the appropriate CPU object according to the parameters 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;/** * Simple Factory */public class for creating motherboards mainboardfactory {    /**     * method for creating a motherboard interface object     * @param type Select parameter for board type     * method of @return Motherboard interface     Object    */Public static Mainboardservice createmainboard (int type) {    Mainboardservice mainboard = null;       Select and create the appropriate motherboard object according to the parameters       if (type==1) {           mainboard = new Gamainboard (1156);       } else if (type==2) {           mainboard = new Msimainboard (939);       }       return mainboard;}    }
Installed person 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;/** * */public class for installed engineers    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 Customer Select the type of CPU required * @param mainboardtype customer Select the type of motherboard required */public void Makecomputer (int cputype,int mainboardtype)       {//1: First prepare the necessary accessories preparehardwares (Cputype,mainboardtype); 2: Assembly Machine//3: Test machine//4: Delivery Customer}/** * Accessories required to prepare the installation * @param cputype customer Select the type of CPU required * @par Am Mainboardtype customer chooses the type of motherboard required */private void preparehardwares (int cputype,int mainboardtype) {//This is going to prepare the CPU and motherboard             Implementation, for example simple, here only to prepare the two//However, the installation engineer does not know how to create, how to do?       Directly find the appropriate factory to obtain THIS.CPU = CPUFACTORY.CREATECPU (Cputype); This.maInboard = Mainboardfactory.createmainboard (Mainboardtype);       Test whether the accessories 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 person    = new person ();       Tell the installation engineer to assemble the computer    Person.makecomputer (2,1) by assembling the installation Engineer's own choice of accessories.}    
with the above code we can find the problem:

The computer may not be assembled, and the CPU and motherboard may not be matched, that is, the simple factory and factory method models only care about the creation of a single product, regardless of whether there is a dependency between the products.

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

Let's take a look at the implementation code for the Abstract factory pattern:

First, the code for abstract products and specific products is unchanged.

The factory code has changed:

Package Hirain.abstract1.factory;import Hirain.abstract1.cpu.cpuservice;import hirain.abstract1.mainboard.mainboardservice;/** * Abstract Factory interface, declaring operations to create abstract product objects */public interface Abstractfactory {    /**     * Create CPU Objects     * @return CPU Objects *     /public    cpuservice createcpu ();    /**     * Create a Motherboard object     * @return Board 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 Scenario One: Intel's CPU + Gigabyte motherboard * When creating CPU and motherboard objects, is corresponding, can match on the */public class SCHEMA1 implements Abstractfactory{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;/** * Installed Solution II: AMD CPU + MSI motherboard * When creating CPU and motherboard objects, is corresponding, can match on the */public class Schema2 implements abstractfactory{public    Cpuservice createcpu () {       return new amdcpu (939);    }    Public Mainboardservice Createmainboard () {       return new Msimainboard (939);    }  
Installed person Code:

Package Hirain.abstract1.person;import Hirain.abstract1.cpu.cpuservice;import  Hirain.abstract1.factory.abstractfactory;import hirain.abstract1.mainboard.mainboardservice;/** * Installation Engineer Class */public    Class Person {/** * Defines the CPU required by the Assembly machine */private cpuservice cpu= null;     /** * Define the motherboard required for assembling the machine */private mainboardservice mainboard = null; /** * Installation process * @param schema Customer selected installation plan */public void Makecomputer (abstractfactory schema) {//1: First Ready for installation       The required Accessories preparehardwares (schema); 2: Assembly Machine//3: Test machine//4: Delivery Customer}/** * Accessories required for the preparation of the installation * @param schema customer selected installation plan */PRIV ate void Preparehardwares (abstractfactory schema) {//Here to prepare the CPU and motherboard specific implementation, for example simple, here only to prepare these two//However, the installation engineer does not know how to create, how to do             It?       Use the abstract factory to obtain the corresponding interface object THIS.CPU = Schema.createcpu ();             This.mainboard = Schema.createmainboard ();       Test whether the accessories use 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 person       = new person ();       The customer chooses and creates the installed Plan object to be used       abstractfactory schema = new Schema2 ();       Tell the installation engineer to choose the installation plan, let the installation engineer assemble the computer       person.makecomputer (schema);}    }
The actual work process in this mode is not used to, do not know later will not use, said the abstract factory and Factory mode difference:

1, the factory method is an abstract product, can be derived from a number of specific products; Abstract Factory is a number of abstract products, each product can be derived from a number of specific products.

2, factory method each specific factory class can only create a specific product class object. Abstract factory each specific factory class can create multiple specific product class objects.


















Java design pattern-Abstract Factory mode

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.