Factory method Patterns and abstract factory patterns in Java design pattern programming _java

Source: Internet
Author: User

Factory method Mode

Motivation
Creating an object often requires a complex process, so it is not suitable to be included in a composite factory, when there are new products, the need to modify this compound factory, is not conducive to expansion.

Also, some objects can be created using information that cannot be accessed by the composite factory, so defining a factory interface, by implementing this interface to determine the instantiation of that product, is the factory method pattern, which defers the instantiation of the class to the subclass.

Objective
1. Define an interface for subclasses to decide which product to instantiate.
2. Create objects from a common interface.

Realize

1. Product interface and specific products are well understood.
2. The factory class provides a factory method that returns a Product object. But the factory approach is abstract.
3. The concrete factory class realizes the factory method, completes the concrete product creation.

Several Button classes class 
button{/* ... */} class 
Winbutton extends button{/* ... * * 
class Macbutton extends button{ /* ... 
  
////Their factory class 
interface buttonfactory{ 
  abstract Button Createbutton (); 
} 
Class Winbuttonfactory implements buttonfactory{ 
  Button Createbutton () {return 
    new Winbutton (); 
  } 
} 
class Macbuttonfactory implements buttonfactory{ 
  Button Createbutton () {return 
    new Macbutton (); 
  } 
} 

Applicable scenarios
1. When creating objects with more repetitive code, consider using the factory method pattern to perform these duplicated parts.
2. Creating objects requires access to certain information that should not be included in the factory class, so subclasses can be created to implement the object.
3. You need to centrally manage the creation of objects and keep the program consistent.


Abstract Factory pattern
definition
The abstract factory pattern provides a way to encapsulate a set of separate factories with the same subject. In normal use, the client program needs to create a concrete implementation of the abstract factory, and then use the abstract factory as an interface to create specific objects for this topic. The client program does not need to know (or care) that it obtains the specific types of objects from these internal factory methods because the client program uses only the generic interfaces of those objects. Abstract Factory mode separates the implementation details of a set of objects from their general usage.

A "factory" is where a product (object) is created to separate the creation of the product from the use of the product. The purpose of abstract Factory mode is to separate the interface of several abstract products from the concrete implementation of different theme products. This allows you to add new specific factories without modifying the client code that references the abstract factory.

Using the abstract factory pattern, you can change your factory's client code, even at run time, when a specific plant changes. However, using this pattern, or a similar design pattern, can create unnecessary complexity and extra work for writing code. Proper use of design patterns can offset such "extra work".

Realize

1. Abstractfactory-Defines an interface method for creating abstract products.
2. Concretefactory-Implement method to create a specific product.
3. Abstractproduct-declares interfaces for different types of products.
4. Product-Define concretefactory corresponding specific products, implement Abstractproduct interface.
5. Client-use Abstractfactory and Abstractproduct classes.

Abstract class abstractproducta{public abstract void operationA1 (); 
public abstract void operationA2 (); 
  Class ProductA1 extends abstractproducta{ProductA1 (String Arg) {System.out.println ("Hello" +arg); 
  }//Implement the code here is public void operationA1 () {}; 
public void OperationA2 () {}; 
  Class ProductA2 extends abstractproducta{ProductA2 (String Arg) {System.out.println ("Hello" +arg); 
  }//Implement the code here is public void operationA1 () {}; 
public void OperationA2 () {}; 
  Abstract class abstractproductb{//public abstract void operationB1 (); 
public abstract void operationB2 (); 
  Class ProductB1 extends abstractproductb{ProductB1 (String Arg) {System.out.println ("Hello" +arg); }//Implement the code here} class ProductB2 extends abstractproductb{ProductB2 (String Arg) {System.out.pri 
  Ntln ("Hello" +arg); }//Implement the code here} abstract class abstractfactory{abstract ABstractproducta createproducta (); 
Abstract ABSTRACTPRODUCTB CREATEPRODUCTB (); Class ConcreteFactory1 extends abstractfactory{abstractproducta createproducta () {return new ProductA1 ("Prod 
  UctA1 "); 
  } ABSTRACTPRODUCTB CREATEPRODUCTB () {return new ProductB1 ("ProductB1"); } class ConcreteFactory2 extends abstractfactory{abstractproducta createproducta () {return new ProductA2 ("P 
  RoductA2 "); 
  } ABSTRACTPRODUCTB CREATEPRODUCTB () {return new ProductB2 ("ProductB2"); }//factory Creator-an Indirect way of instantiating the factories class factorymaker{private static Abstract 
  Factory Pf=null; 
    Static Abstractfactory GetFactory (String choice) {if (Choice.equals ("a")) {pf=new ConcreteFactory1 (); 
        }else if (choice.equals ("B")) {pf=new ConcreteFactory2 (); 
  } return PF; }//Client public class client{public static void Main (String args[]) {abstractfactory Pf=factorymakEr.getfactory ("a"); 
    Abstractproducta product=pf.createproducta (); 

 More function calls on product}}

The Factorymaker class uses a simple factory model, and the implementation of a specific factory is in the factory method mode.

Applicable scenarios
1. A system is independent of the creation, composition, and presentation of its products.
2. When a system is to be configured by one of several product families.
3. Emphasis needs to be placed on the design of a series of related product objects for joint use.
4. Provide a Product class library, and only want to show their interfaces rather than implementation.

Advantages
1. The specific product is separated from the customer code
2. Easy to change the product series
3. Unify a family of product families together to create

Disadvantages
1. It is difficult to extend new products in the product family, and it needs to modify the interfaces of the abstract factory and the specific factories.

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.