Java design pattern-Abstract Factory mode (factory)

Source: Internet
Author: User

Abstract Factory mode provides an interface to customers that enables clients to create product objects in multiple product families without having to specify a specific type of product.
Design of the system
The System class diagram is designed using the abstract factory model as follows:

As you can see, the abstract factory pattern involves a role:
Abstract Factory Role (Abstractfactory): This role is at the heart of the factory approach pattern, regardless of the business logic of the application system. Typically implemented using Java interfaces or abstract Java classes, all specific factory classes must implement this Java interface or inherit this abstract Java class.
Specific factory class (Concretefactory) Role: This role creates a product instance directly under the client's call. This role contains the logic to choose the right product object, which is closely related to the business logic of the application system. This role is typically implemented using specific Java classes.
Abstract Product Role: The classes that assume this role are the parent classes of objects created by the factory method pattern, or the interfaces they share. This role is typically implemented using Java interfaces or abstract Java classes.
Specific product (concrete product) role: any Product object created by the abstract factory pattern is an instance of a specific product class. This is what the client ultimately needs, and its internals must be filled with the business logic of the application system. This role is typically implemented using specific Java classes.

 factory role source code public  interface  creater  { /** * Product Class A factory method * @author  Shu Yuwei * @time  2015-3-21 pm 05:03:14 * @return  */ public  producta factorya  (); /** * Product Grade B factory method * @author  Shu Yuwei * @time  2015-3-21 pm 05:03:31 * @return  */ 
    
     public  productb 
     factoryb  ();} 
     

Specific factory roles (in general, how many product hierarchy structures will be found in the factory role of the plant method, each product hierarchy in the number of specific products, how many product families, will be found in the factory hierarchy structure of the number of specific factories)

 Public  class ConcreteCreator1 implements creater {     PublicProductAFactorya() {return NewProductA1 (); } PublicProductBFactoryb() {return NewProductB1 (); }} Public  class ConcreteCreatro2 implements creater {     PublicProductAFactorya() {return NewProductA1 (); } PublicProductBFactoryb() {return NewProductB1 (); }} 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(){    }}

The client needs not a factory, but a specific product. In a real system, the product class should be closely related to the business logic of the application system.
Under what circumstances is abstract Factory mode used?
1. A system should not rely on the details of how a product class instance is created, combined, and expressed, which is important for all forms of Factory mode.
2, the system's products have more than one product family, and the system only consumes one of the products of a family
3, belong to the same product family products are used together, this constraint must be reflected in the system design
4, the system provides a library of product classes, all products with the same interface appear, so that the client does not rely on the implementation

Java design pattern-Abstract Factory mode (factory)

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.