Java Design pattern (Factory method)

Source: Internet
Author: User

This article address: http://www.cnblogs.com/archimedes/p/java-factory-method-pattern.html, reprint please indicate source address.

Factory method Mode (alias: Virtual construction)

Defines an interface for creating objects, letting subclasses decide which class to instantiate. The Factory method defers the instantiation of a class to its subclasses.

Overview

A factory method pattern can be used to design a system when the system is ready to provide the user with an instance of a subclass of a class and does not want the user code to be coupled with the subclass. The key to a factory method pattern is to define an abstract method in an interface or abstract class that returns an instance of a subclass of a class that has its subclasses or classes that implement the interface to return an instance of a subclass by overriding the abstract method.

Applicability

1. When a class does not know the class of the object it must create.

2. When a class wants its subclass to specify the object it creates.

3. When the class delegates the responsibility of creating an object to one of several helper subclasses, and you want to put which helper subclass is the agent this information is localized.

participants

1.Product defines the interface of the object created by the factory method.

2.CONCRETEPRODUCT implements the product interface.

3.Creator declares the factory method, which returns an object of type product. Creator can also define a default implementation of a factory method that returns a default Concreteproduct object. You can call the factory method to create a product object.

4.ConcreteCreator redefine the factory method to return a concreteproduct instance.

Structure and use of factory method pattern

The structure of the pattern consists of four roles:

• Abstract products (product)

• Specific Products (concreteproduct)

• Constructor (Creator)

• Concrete constructor (Concretecreator)

UML Class diagrams for schemas

Actual combat part

Example 1: Suppose there are three refills, the red refill, the blue refill, and the black refill, respectively. The user wants to use a ballpoint pen to clarify the color of the refill.

Description and use of the structure of the pattern

1. Abstract (product): Pencore.java

 Public Abstract class pencore{   String color;     Public Abstract void Writeword (String s);}

2. Specific products (concreteproduct) _1: Redpencore.java

 Public class extends pencore{    Redpencore () {      color= "Red";    }      Public void Writeword (String s) {       System.out.println ("write" +color+ "word:" +s);}    }

Specific products (concreteproduct) _2: Bluepencore.java

 Public class extends pencore{    Bluepencore () {      color= "Blue";    }      Public void Writeword (String s) {       System.out.println ("write" +color+ "word:" +s);}    }

Specific products (concreteproduct) _3: Blackpencore.java

 Public class extends pencore{    Blackpencore () {      color= "Black";    }      Public void Writeword (String s) {       System.out.println ("write" +color+ "word:" +s);}    }

3. Constructor (Creator): Ballpen.java

 Public Abstract class ballpen{    Ballpen () {       System.out.println ("produced a ballpoint pen containing" +getpencore (). color+ "refill);    }      Public Abstract // Factory Method }

4. Concrete Constructor (Concretecreator):

Redballpen.java Public classRedballpenextendsballpen{ PublicPencore Getpencore () {return NewRedpencore (); }}Blueballpen.java  Public classBlueballpenextendsballpen{ PublicPencore Getpencore () {return NewBluepencore (); }}Blackballpen.java  Public classBlackballpenextendsballpen{ PublicPencore Getpencore () {return NewBlackpencore (); }}

5. Application Application.java

 Public classapplication{ Public Static voidMain (String args[]) {Pencore pencore; Ballpen Ballpen=NewBlueballpen (); Pencore=Ballpen.getpencore (); Pencore.writeword ("Hello, nice to meet you"); Ballpen=NewRedballpen (); Pencore=Ballpen.getpencore (); Pencore.writeword ("How is"); Ballpen=NewBlackballpen (); Pencore=Ballpen.getpencore (); Pencore.writeword ("Nice to meet"); }} 
Advantages of the Factory method model

• Use the factory method to decouple the code of the user from that of a subclass of a particular class.

• The factory method eliminates the need for users to know how the object it is being created, just know what the object has to do with it.  

Java Design pattern (Factory method)

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.