Java and Design Patterns-Abstract Factory mode

Source: Internet
Author: User

first of all, the article is longer, to ensure that you have the patience to read must be able to understand, no patience directly point X can.

Abstract Factory mode is one of the creation design patterns. Abstract factory model suitable for product determination, product line of uncertain types, how to say? Let's go through a specific example. For example, a computer manufacturer to produce a computer, that is, computer this product is determined, and the computer configuration is not certain, this situation can be solved by the abstract Factory mode. Class Diagram:



The implementation of the code is completely combined with UML class diagrams, and the system can be created with graphs.

In this example are abstract class computerfactory (corresponding to abstractfactory in UML class diagrams):

Package Com.factory.demo;public Abstract class Computerfactory {public abstract CPU createcpu ();p ublic Abstract Mainboard Createmainboard ();p ublic abstract RAM createram ();}
There are three methods for producing CPUs, producing motherboards and producing memory.

Product Abstraction interface, CPU (corresponding to producta in UML class diagram):

Package Com.factory.demo;public interface CPU {void CPU ();}
Product Abstraction interface, motherboard (corresponding to PRODUCTB in UML class diagram):

Package Com.factory.demo;public interface mainboard  {void mainboard ();}
Product abstraction interface, memory (corresponds to PRODUCTC in UML class diagram):

Package Com.factory.demo;public interface Ram {void Ram ();}

Here are the specific products, CPU I3 (corresponding to ProductA1 in UML class diagrams):

Package Com.factory.demo;public class I3cpu implements CPU {public void CPU () {SYSTEM.OUT.PRINTLN ("i3 processor");}}
Specific products, CPU I5 (corresponding to ProductA2 in UML class diagrams):
Package Com.factory.demo;public class I5cpu implements CPU {public void CPU () {SYSTEM.OUT.PRINTLN ("i5 processor");}}

Specific products, motherboard Lenovo motherboard (corresponding to ProductB1 in UML class diagram):

Package Com.factory.demo;public class Lenovomainboard implements mainboard {@Overridepublic void mainboard () { SYSTEM.OUT.PRINTLN ("ASUS Motherboard");}}

Specific products, motherboard ASUS motherboard (corresponding to ProductB2 in UML class diagram):

Package Com.factory.demo;public class Asusmainboard implements mainboard {@Overridepublic void mainboard () { SYSTEM.OUT.PRINTLN ("Lenovo Motherboard");}}
Specific product memory Samsung memory (corresponding to ProductC1 in UML class diagrams):

Package Com.factory.demo;public class Samsungram implements RAM {public void Ram () {SYSTEM.OUT.PRINTLN ("Samsung Memory");}}
Specific products, memory Kingston memory (corresponding to ProductC2 in UML class diagrams):

Package Com.factory.demo;public class Kingstoneram implements RAM {public void Ram () {SYSTEM.OUT.PRINTLN ("Kingston Memory");}}

Specific factory class 1,computer1 inherit abstract factory classes (corresponding to Factory1 in UML class diagrams):

Package Com.factory.demo;public class Computer1 extends Computerfactory {/** * assemble PC 1 with i3 processor, Lenovo Motherboard and Samsung Memory * @return */@Ove Rridepublic CPU createcpu () {return new i3cpu ();} @Overridepublic mainboard Createmainboard () {return new Lenovomainboard ();} @Overridepublic RAM Createram () {return new Samsungram ();}}

Specific factory class 2,computer2 inherit abstract factory classes (corresponding to Factory2 in UML class diagrams):

Package Com.factory.demo;public class Computer2 extends Computerfactory {/** * Assemble PC 2 with i5 processor, ASUS Motherboard and Kingston Memory * @return */@Ov Erridepublic CPU createcpu () {return new i5cpu ();} @Overridepublic mainboard Createmainboard () {return new Asusmainboard ();} @Overridepublic RAM Createram () {return new Kingstoneram ();}}

This creates a test class:

Package Com.factory.demo;public class Testfactory {public static void main (string[] args) {//Computer 1ComputerFactory Computer1=new Computer1 (); SYSTEM.OUT.PRINTLN ("Assembled Product Type 1:"); Computer1.createcpu (). CPU (); Computer1.createmainboard (). mainboard (); Computer1.createram (). RAM ();//Computer 2ComputerFactory computer2=new Computer2 (); SYSTEM.OUT.PRINTLN ("Assembled Product Type 2:"); Computer2.createcpu (). CPU (); Computer2.createmainboard (). mainboard (); Computer2.createram (). RAM ();}}

To run the test class:


To summarize: Each of the above classes or interfaces corresponds to each element of the UML class diagram, and you can build the system with reference to a UML class diagram. The advantages of this system are the separation of the interface and implementation, to achieve the product type switching is very flexible and easy (that is, to make a product Computer3 production line is very easy). The drawbacks are also obvious, which can lead to explosion-like growth by reference to:


Another drawback is that it is not easy to add other product classes, adding a product class requires modifying the abstract factory, then all specific factory classes must also be modified.

Let's look at how to add a Computer3 line:

The Computer3 configuration is i7 cpu+ ASUS Motherboard + Kingston memory. At this point we first implement a i7 CPU (corresponding to the ProductA3 in the UML class diagram):

Package Com.factory.demo;public class I7cpu implements CPU {public void CPU () {SYSTEM.OUT.PRINTLN ("i7 processor");}}

Then the specific factory class 3,computer3 inherits the abstract factory class (corresponding to the Factory3 in the UML class diagram):

Package Com.factory.demo;public class Computer3 extends Computerfactory {/** * assemble pc 3,i7 cpu+ ASUS motherboard + KINGSTON Memory * @return */@Overri Depublic CPU createcpu () {return new i7cpu ();} @Overridepublic mainboard Createmainboard () {return new Asusmainboard ();} @Overridepublic RAM Createram () {return new Kingstoneram ();}}

The test class adds Computer3 code, which is added as follows:

Package Com.factory.demo;public class Testfactory {public static void main (string[] args) {//Computer 1ComputerFactory Computer1=new Computer1 (); SYSTEM.OUT.PRINTLN ("Assembled Product Type 1:"); Computer1.createcpu (). CPU (); Computer1.createmainboard (). mainboard (); Computer1.createram (). RAM ();//Computer 2ComputerFactory computer2=new Computer2 (); SYSTEM.OUT.PRINTLN ("Assembled Product Type 2:"); Computer2.createcpu (). CPU (); Computer2.createmainboard (). mainboard (); Computer2.createram (). RAM ();//Computer 3ComputerFactory computer3=new Computer3 (); SYSTEM.OUT.PRINTLN ("Assembled Product Type 3:"); Computer3.createcpu (). CPU (); Computer3.createmainboard (). mainboard (); Computer3.createram (). RAM ();}}

Run the following example:

It can be seen that it is easy to extend the production line and greatly enhance the system scalability.


My favorite friend pays attention to me.




Java and Design Patterns-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.