Design Mode 04 _ Abstract Factory mode, design mode 04 Abstract Factory
This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/46440915
Abstract Factory mode is the object creation mode, which is further promoted by the factory method mode.
The biggest difference between the abstract factory mode and the factory method mode is that the factory method mode targets a product level structure, while the abstract factory mode requires multiple product level structures.
Assume that a sub-system requires some product objects, and these products belong to more than one product level structure. Abstract Factory models can be introduced to separate the responsibility for consuming these product objects from the responsibility for creating these product objects. In this way, the consumer party does not need to directly participate in the creation of the product, but only needs to request the required product from a public factory interface.
Advantages:
(1) Separation interface and implementation
The client uses an abstract factory to create the required objects, and the client does not know the specific implementation. The client is only for product-oriented interface programming. That is to say, the client is decoupled from the specific product implementation.
(2) Make it easy to switch product families
Because A specific factory implementation represents A product family, for example, from product A to product B, you only need to switch to A specific factory.
Disadvantages:
(1) It is not easy to expand new products.
If you want to add a new product to the entire product family, you need to modify the abstract factory so that all factory implementation classes can be modified.
Use the following code to familiarize yourself with the abstract factory mode:
Different abstract factories:
Package com. design. abstractFactory; // each brand of product is responsible for the production factory, that is, different manufacturers are responsible for the production of their own brand public abstract class Factory1 {abstract public IproductA getproductA1 (); abstract public IproductB getProductB1 ();}
package com.design.abstractFactory;public abstract class Factory2 {abstract public IproductA getproductA2();abstract public IproductB getproductB2();}
Different factories:
Package com. design. abstractFactory; // specific factory production product public class ConcreateFactory1 extends Factory1 {@ Overridepublic IproductA getproductA1 () {return new ProductA1 () ;}@ Overridepublic IproductB getProductB1 () {return new ProductB1 ();}}
package com.design.abstractFactory;public class ConcreateFactory2 extends Factory2{@Overridepublic IproductA getproductA2() {return new ProductA2();}@Overridepublic IproductB getproductB2() {return new ProductB2();}}
Define different product interfaces:
Package com. design. abstractFactory; // defines certain standards for different products. Use interfaces to implement public interface IproductA {public void method ();}
package com.design.abstractFactory;public interface IproductB {public void method();}
Different products:
Package com. design. abstractFactory; // a series of specific products that implement product standards public class ProductA1 implements IproductA {@ Overridepublic void method () {System. err. println ("A1 product production A1 ");}}
Package com. design. abstractFactory; public class ProductA2 implements IproductA {@ Overridepublic void method () {System. err. println ("A2 production product A2 ");}}
Package com. design. abstractFactory; public class ProductB1 implements IproductB {@ Overridepublic void method () {System. err. println ("B1 production product B1 ");}}
Package com. design. abstractFactory; public class ProductB2 implements IproductB {@ Overridepublic void method () {System. err. println ("B2 production product B2 ");}}
Client implementation:
package com.design.abstractFactory;public class Client {public static void main(String[] args) {Factory1 factory = new ConcreateFactory1();IproductA a1 = factory.getproductA1();IproductB b1 = factory.getProductB1();a1.method();b1.method();}}
Test results:
A1 product production A1
B1 production product B1