Java Design Pattern -- Abstract factory pattern (Abstract factory)

Source: Internet
Author: User

Java Design Pattern -- Abstract factory pattern (Abstract factory)

Abstract Factory mode can provide interfaces to customers so that the client can create product objects in multiple product families without specifying the specific product type.
System Design
The system class diagram designed using the abstract factory model is as follows:

It can be seen that the abstract factory model involves roles:
AbstractFactZ role? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + keys/nT0LXEvt/keys + vt/keys/Nu6e2y7XEtffTw8/keys + sa3ttTP87XEwt + expires + 8rdPr06bTw8 + expires/expires + s + expires/LL + expires/zuaSzp8Sjyr3L + bS0vai1xMjOus6y + expires/expires = "brush: java; ">Factory role source code public interface Creater {/*** factory method for Product Level A * @ author Fu yuwei * @ time 05:03:14 * @ return */public ProductA factoryA (); /*** factory method for Product Level B * @ author Fu yuwei * @ time 05:03:31 * @ return */public ProductB factoryB ();}

The specific factory role (generally, how many product level structures are there, we will find multiple factory methods in the factory role, and how many specific products are there in each product level structure, how many product families will be found in the factory level structure)

public class ConcreteCreator1 implements Creater { public ProductA factoryA() { return new ProductA1(); } public ProductB factoryB() { return new ProductB1(); }}public class ConcreteCreatro2 implements Creater { public ProductA factoryA() { return new ProductA1(); } public ProductB factoryB() { return new ProductB1(); }}public interface ProductA {}public interface ProductB {}public class ProductA1 implements ProductA { public ProductA1(){ }}public class ProductA2 implements ProductA { public ProductA2(){ }}public class ProductB1 implements ProductB { public ProductB1(){ }}public class ProductB2 implements ProductB { public ProductB2(){ }}

What the client needs is not a factory, but a specific product. In a real system, the product category should be closely related to the business logic of the application system.
Under what circumstances do I use the abstract factory model?
1. A system should not rely solely on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.
2. There are more than one product family in this system, and only one of them is consumed in the system.
3. products belonging to the same product family are used together. This constraint must be reflected in the system design.
4. The system provides a product library. All products use the same interface, so that the client does not rely on implementation.

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.