A brief analysis of the factory model of Java design pattern (iii)

Source: Internet
Author: User
Tags lenovo dell laptop

Before reading this article, please read the Simple factory model and the factory method pattern in (i) and (ii).

1Introduction to Abstract Factory mode

Abstract Factory (Abstract Factroy) Mode: Toolbox Mode ( Kit ). Abstract Factory mode is one of the most abstract and general forms of all forms of Factory mode, such as.

Figure 1.1

There is a factory class hierarchy on the left and two different product hierarchies on the right, respectively, the hierarchy of product A and The grade structure of product B, factory 1 and Plant 2 respectively responsible for different product grades, the production of the same product family products. Another example:

Figure 1.2

indicates that theButtonand theTexttwo different product hierarchies, with two product families, andUnixbuttonand theUnixtextis part ofUnixProduct Family,Winbuttonand theWintextis part ofWindowsproduct family. The phase diagram is as follows:

Figure 1.3

Although there are multiple product hierarchies, only one plant hierarchy is required to produce multiple hierarchical products, the above example requires only two specific sub-factories,unixfactoryand thewinfactory,unixfactoryobject is responsible for creatingUnixProducts in the product family,winfactoryobject is responsible for creatingWinProducts in the product family. This is a typical application of the abstract factory. UMLThe figure is as follows:

Figure 1.4

above 2 a product of different grade structure Button and the Text has a parallel structure, so if you adopt a factory method pattern, you must use two separate plant-level structures to address this 2 product hierarchy, such as the use of factory methods such as:

Figure 1.5

as this 2 the similarity of a product's hierarchical structure can lead to 2 a parallel plant hierarchy, the number of plant hierarchy structures given by the factory method pattern increases as the product hierarchy increases.

Therefore, the advantage of using abstract Factory mode is that the same factory hierarchy is responsible for the creation of product objects in multiple different product hierarchies, as shown above 1.4 , a factory hierarchy can create all objects of a product family that belong to different product hierarchies, and it is clear that abstract factories are more efficient than factory methods.

So the biggest difference between the abstract factory model and the factory method pattern is that the factory method pattern is for a product hierarchy, and the factory approach pattern requires multiple product hierarchies.

1.1Abstract Factory mode (Toolbox Modemode):

Abstract Factory mode has three roles: 1. Abstract product Interface 2. Specific product Class 3. Abstract factory interface 4. Specific factory class .

1.2 Implementation of the abstract factory (build a abstractfactory package, all programs are placed under the package):

(1) First, an abstract product interface (Pc.java) is built.

Package Abstractfactory;public interface Pc {public void Run ();}

(2) Construction of two specific product classes ( Dellpc.java , Lenovopc.java ), the PC interface needs to be implemented.

Package Abstractfactory;public class DELLPC implements PC {public void run () {System.out.println ("Dell PC Run");}} Package Abstractfactory;public class Lenovopc implements PC {public void run () {System.out.println ("Lenovo Pc Run");}}

(3) Build an abstract product interface (Notebook.java).

Package Abstractfactory;public interface Notebook {public void Run ();}

(4) Construction of two specific product classes ( Dell Notebook.java , Lenovo Notebook.java ) to achieve Lenovo interface.

Package Abstractfactory;public class Dellnotebook implements Notebook {public void run () {System.out.println ("Dell Portable computer Run ");}} Package Abstractfactory;public class Lenovonotebook implements Notebook {public void run () {System.out.println (" Lenovo laptop Running ");}}

(5) building an abstract Factory interface (Computerfactory.java), adding two factory methods Makepc () , Makenotebook () , or you can use an empty interface.

Package Abstractfactory;public interface Computerfactory {public Pc makepc ();p ublic Notebook Makenotebook ();}

(6) Construction of two specific factory classes (Dellcomputerfactory.java, Lenovocomputerfactory.java).

Package Abstractfactory;public class Dellcomputerfactory implements Computerfactory {public DELLPC Makepc () {return new DELLPC ();} Public Dellnotebook Makenotebook () {return new Dellnotebook ();}} Package Abstractfactory;public class Lenovocomputerfactory implements Computerfactory {public Lenovopc Makepc () {return New Lenovopc ();} Public Lenovonotebook Makenotebook () {return new Lenovonotebook ();}}

(7) Build a client program (Testabstractfactory.java) test.

Package Abstractfactory;public class Testabstractfactory {public static void main (String []args) throws exception{ Computerfactory computerfactory=new  dellcomputerfactory (); Pc Dellpc1=computerfactory.makepc (); Notebook Dellnotebook1=computerfactory.makenotebook ();d ellpc1.run ();d ellnotebook1.run ();}

Run output:

Dell PC runs

Dell Laptop Running

Running successfully, it was shown that a specific factory object is responsible for the production of two different product grade structures but belong to a product family.

In general, how many product hierarchies are there, how many factory methods are in each specific factory role, how many specific products are in each product hierarchy, how many product families there are, and how many specific factory roles will be in the plant hierarchy.

in our example, there are 2 product hierarchy structures (PC and notebook), so each specific factory (Dellcomputerfactory and Lenovocomputerfactory) will have two factory methods ( Makepc () , Makenotebook () ).

There are exactly 2 specific products in each product hierarchy, so there are 2 product families (Dell's PC and Notebook, Lenovo's PC and notebook), so there are two specific factory roles in the factory hierarchy ( dellcomputerfactory and the lenovocomputerfactory ). 1.6

Figure 1.6

(7) Finally, let's look at the UML diagram of the abstract Factory mode, 1.7.

Figure 1.7

Recommended articles:

A brief analysis of the factory model of Java design pattern (II.)

http://blog.csdn.net/minidrupal/article/details/38323457

A brief analysis of the factory model of Java design pattern (i)

http://blog.csdn.net/minidrupal/article/details/38323341


Author:vicky
Introduction: Educators
Sign : Nevermore, Future







A brief analysis of the factory model of Java design pattern (iii)

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.