Preliminary Exploration of design patterns-Abstract Factory patterns

Source: Internet
Author: User

Abstract Factory mode, Abstract Factory, which belongs to the object creation mode by GOF. It is used to provide an interface for creating a series of related or mutually dependent objects without specifying the classes they implement, the "interface-oriented programming" principle is well practiced.

I. Application scenarios:

1. A system must be independent of the creation, combination, and representation of its products, such as a text editor across operating systems, the text editor operates on the editor view component independent of the Operating System Abstraction, rather than the implementation of an operating system.

2. When a system needs to be configured by one of multiple product series, for example, a common system topic, the system can have multiple sets of themes, and customers can choose one as they like.

3. When you want to emphasize the design of a series of related product objects for joint use, this will help provide consistent interfaces and implementations.

4. When you want to provide a product library, you only want to display their interfaces instead of implementations. Product libraries can be reused. Transition coupling with specific implementations naturally reduces the value of product libraries.

Ii. UML diagram:


Iii. java implementation

/*** Abstract Factory interface, used to produce A series of related products * @ author qbg */public interface ifacloud {/*** production product A */IProductA createProductA (); /*** production product B */IProductB createProProductB ();}
/*** Abstract interface of product A, used to abstract public operations of product A * @ author qbg **/public interface IProductA {public void method1 (); public void method2 ();}
/*** Abstract interface of Product Family B, used to abstract public operations of product B * @ author qbg **/public interface IProductB {public void method1 (); public void method2 ();}
/*** A specific implementation of the abstract factory interface, used to generate Series, product B * @ author qbg **/public class ConcreteFactory1 implements ifacloud {@ Overridepublic IProductA createProductA () {return new ProductA1 () ;}@ Overridepublic IProductB createProProductB () {return new ProductB1 ();}}
/*** Another specific implementation of the abstract factory interface is used to generate two series, product B * @ author qbg **/public class ConcreteFactory2 implements ifacloud {@ Overridepublic IProductA createProductA () {return new ProductA2 () ;}@ Overridepublic IProductB createProProductB () {return new ProductB2 ();}}
/*** A specific implementation of product A: ProductA1 * @ author qbg **/public class ProductA1 implements IProductA {@ Overridepublic void method1 () {System. out. println ("productA1-method1") ;}@ Overridepublic void method2 () {System. out. println ("productA1-method2 ");}}
/*** A specific implementation of product B series: ProductB1 * @ author qbg **/public class ProductB1 implements IProductB {@ Overridepublic void method1 () {System. out. println ("productB1-method1") ;}@ Overridepublic void method2 () {System. out. println ("productB1-method2 ");}}
/*** Another specific implementation of product A: ProductA2 * @ author qbg **/public class ProductA2 implements IProductA {@ Overridepublic void method1 () {System. out. println ("productA2-method1") ;}@ Overridepublic void method2 () {System. out. println ("productA2-method2 ");}}
/*** Another specific implementation of product B series: ProductB2 * @ author qbg **/public class ProductB2 implements IProductB {@ Overridepublic void method1 () {System. out. println ("productB2-method1") ;}@ Overridepublic void method2 () {System. out. println ("productB2-method2 ");}}
/*** Client code: the user of the abstract factory and the abstract product interface * @ author qbg **/public class Client {private IFactory factory; private IProductA productA; private IProductB productB; public Client (IFactory factory) {this. factory = factory;} public void product () {productA = factory. createProductA (); productB = factory. createProProductB ();} public void consumeProductA () {productA. method1 (); productA. method2 ();} public void consumeProductB () {productB. method1 (); productB. method2 ();} public static void main (String [] args) {// Production and Consumption 1 Series A, product B Client c1 = new Client (new ConcreteFactory1 ()); c1.product (); c1.consumeProductA (); c1.consumeProductB (); // Production and Consumption 2 series A, product B Client c2 = new Client (new ConcreteFactory2 (); c2.product (); c2.consumeProductA (); c2.consumeProductB ();}}
Iv. Advantages and Disadvantages

Advantages:

1. Separate specific implementations for interface programming. Client code only needs to access abstract interfaces without concern about specific implementation classes and decoupling.

2. Easy to switch product series. Since the abstract factory provides a unified abstraction layer for the product series, if you want to switch the product series, you only need to exchange the specific implementation.

3. It is conducive to product consistency, which is effective in the consistent abstraction provided by the abstract factory.

Disadvantages:

1. It is difficult to support new types of products. If a new product is added to an existing product series, it is necessary to modify the abstract interfaces of the existing product series, which will inevitably lead to changes in the specific implementation of each product series.


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.