Analysis on the factory mode of JAVA design mode (III)
Before reading this article, read the simple factory mode and factory method mode in (1) and (2.
1 abstract factory model Introduction
Abstract Factory mode: Toolkit mode ). Abstract Factory mode is the most abstract and general factory mode in all forms, such.
Fig 1.1
There is A factory class level structure on the left, and two different product level structures on the right, namely product A's level structure and product B's level structure, factory 1 and plant 2 are responsible for the production of products of the same product family at different product levels. For example:
Fig 1.2
It indicates the hierarchical structure of two different products, Button and Text. There are two product families at the same time. UnixButton and UnixText belong to the Unix product family, And WinButton and WinText belong to the Windows product family. The phase diagram is as follows:
Fig 1.3
Although there are multiple product level structures, only one factory level structure is required to produce products with multiple levels. In the preceding example, only two specific sub-factories are required, unixFactory and WinFactory. The UnixFactory object is used to create products in the Unix product family, And the WinFactory object is used to create products in the Win product family. This is a typical application of the abstract factory. The UML diagram is as follows:
Fig 1.4
The buttons and Text of the above two products with different levels have parallel structures. Therefore, if the factory mode is used, two independent factory level structures must be used to cope with these two product levels. For example, the factory method is as follows:
Fig 1.5
Because of the similarity between the two product level structures, two parallel factory level structures will be created. As the product level structure increases, the number of factory level structures given by the factory method model will also increase.
Therefore, the advantage of using the abstract factory model is that the same factory level structure is responsible for creating product objects in different product levels, as shown in Figure 1.4 above, A factory level structure can create all objects of a product family with different product levels. Obviously, abstract factories are more efficient than factory methods at this time.
The biggest difference between the abstract factory mode and the factory method mode is that the factory method mode is for a product level structure, while the factory method mode needs to face multiple product level structures.
1.1 Abstract Factory mode (Toolbox mode ):
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 (create a abstractFactory package where all programs are placed ):
(1) first, create an abstract product interface (Pc. java ).
package abstractFactory;public interface Pc {public void run();}
(2) create two specific product classes (DellPc. java and LenovoPc. java) and implement Pc interfaces.
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 running ");}}
(3) create another abstract product interface (Notebook. java ).
package abstractFactory;public interface Notebook {public void run();}
(4) create two specific product classes (DellNotebook. java and LenovoNotebook. java) to implement the Lenovo interface.
Package implements actfactory; public class DellNotebook implements Notebook {public void run () {System. out. println ("Dell laptop running") ;}} package abstractFactory; public class LenovoNotebook implements Notebook {public void run () {System. out. println ("Lenovo laptop running ");}}
(5) Create an abstract factory interface (ComputerFactory. java), add two factory methods makePc () and makeNotebook (), or use an empty interface.
package abstractFactory;public interface ComputerFactory {public Pc makePc();public Notebook makeNotebook();}
(6) create two specific factory classes (DellComputerFactory. java and 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) create a client program (TestAbstractFactory. java) for testing.
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();dellpc1.run();dellnotebook1.run();}}
Running output:
Dell PC running
Dell laptop running
The operation is successful. The above shows that a specific factory object is responsible for the production of two products with different product level structures but belonging to the same product family.
Generally, how many product levels are there, how many factory methods are there in each specific factory role, and how many specific products are there in each product level structure, the number of specific factory roles in the factory hierarchy.
In our example, there are two product level structures (Pc and Notebook), so each specific factory (DellComputerFactory and LenovoComputerFactory) will have two factory methods (makePc (), makeNotebook ()).
There are exactly two specific products in each product level structure, so there are two product families (Dell's Pc and Notebook family, Lenovo's Pc and Notebook family ), therefore, there are two specific factory roles (DellComputerFactory and LenovoComputerFactory) in the factory hierarchy ). 1.6
Fig 1.6
(7) Finally, let's take a look at the UML diagram of the abstract factory model, 1.7.
Fig 1.7
Recommended articles:
Analysis on the factory mode of JAVA design mode (2)
Http://blog.csdn.net/minidrupal/article/details/38323457
Analysis on the factory mode of JAVA design mode (1)
Http://blog.csdn.net/minidrupal/article/details/38323341
Author: Vicky
Introduction: educators
Sign: never forget the past and never forget the future