Java design Pattern (2)--factory method mode

Source: Internet
Author: User

The factory method pattern also belongs to the class's creation pattern, also known as the polymorphic factory pattern . the meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.

I. Roles contained in the model and their responsibilities

1. Abstract Factory (Creator) role
At the core of the factory method pattern, any factory class must implement this interface.
2. Specific factory (concrete Creator)
Role-Specific factory class is an implementation of an abstract factory that is responsible for instantiating product objects .
3. Abstract (Product) role
The parent class of all objects created by the factory method pattern, which is responsible for describing the common interfaces common to all instances.
4. Specific products (concrete product) role
Specific instance objects created by the factory method pattern

Ii. Comparison of plant method models and simple factory models
    1. The structural differences between the factory method pattern and the simple factory model are not obvious. the core of the Factory method class is an abstract factory class , while the simple factory model places the core on a specific class.
    2. The factory method pattern has an alias called the Polymorphism factory pattern because the specific factory class has a common interface, or a common abstract parent class.
    3. When the system extension needs to add a new product object, just need to add a specific object and a specific factory object , the original factory objects do not need to make any changes, and do not need to modify the client, well in line with the " open-closed " principle. The simple factory model has to modify the factory method after adding the new Product object, the extensibility is not good.
    4. The factory method model can evolve into a simple Factory mode after degradation.
Third, the demo abstract role
 Public Interface Fruit {    /     * *     collect * /public     void  get ();}
Specific Products
 public  class  Apple implements   fruit{ /*   * capture  */ public  void  " Capture Apple "
 Public class Implements fruit{    /     * *     Collect */public     void  get () {        System.out.println ("collect Bananas");}    }
Abstract Factory role
 Public Interface fruitfactory {    public  Fruit getfruit ();}
Specific Factory
 Public class Implements fruitfactory {    public  Fruit getfruit () {        returnnew  Apple ( );    }}
 Public class Implements fruitfactory {    public  Fruit getfruit () {        returnNew  Banana ();    }}
Test method
 Public classMainClass { Public Static voidMain (string[] args) {//Get ApplefactoryFruitfactory FF =Newapplefactory (); //get Apple instance objects with ApplefactoryFruit Apple =Ff.getfruit ();                Apple.get (); //Get BananafactoryFruitfactory FF2 =Newbananafactory (); Fruit Banana=Ff2.getfruit ();            Banana.get (); }}
Console output
Collect apples to collect bananas
Four, UML class diagram

Java design Pattern (2)--factory method mode

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.