Java Design pattern (creation: Factory method Mode)

Source: Internet
Author: User

(The content of the original, there are shortcomings in the hope of more advice!) )

Design patterns can be divided into three categories according to the objective criteria:

    1. Create: Creational The design pattern of the created type is related to the creation of the object.

    2. Structured: Structural handles the combination of classes and objects.

    3. Behavioral: Behavioral Describes how classes and objects interact and assigns responsibilities.

    1. What is the factory method model?

At the user's point of view, the factory method pattern refers to the requirement that the user proposes specific functions to be implemented, or specific things that they want to produce, and delivers the thing or function to the user after it has been manufactured through the factory. The user does not know the implementation of the specific process, only to do the request and the implementation of the results of the use.

Strictly speaking, the Factory mode (Factory method) is also called the Factory pattern, which belongs to the class creation pattern, the Factory parent Class (interface) is responsible for defining the public interface of the product object, and the subclass factory is responsible for creating the specific Product object.

Objective: To defer the instantiation of the product to the subclass factory, and to determine which product specific object should be instantiated by the factory subclass.

There are four components:

1. Abstract Factory role: the interface that a specific factory must implement or the parent class that must inherit. Typically implemented by an abstract class or interface.

2. Specific factory role: is the specific application called to create the corresponding specific product object. Generally it is implemented by specific classes.

3. Abstract Product Role: The parent class of a specific product inheritance or an implemented interface. Typically implemented by an abstract class or interface.

4. Specific product role: the object created by the specific factory role is its instance. Typically implemented by specific classes.

    1. The four parts and application of the factory method pattern are distinguished by the example.

Example: Boss proposes to send a message to employees

Public class factorymethod{

/**

* Define the Common interface send

*/

Interface Send {

void Send ();

}

/**

* Define specific factory roles and inherit public interfaces

*/

class SMSfactory implements send{

@Override

Public void Send () {

TODO Auto-generated method stubs

System. out. println ("Send message via SMS");

}

}

class Mailfactory implements send{

@Override

Public void Send () {

TODO Auto-generated method stubs

System. out. println ("Send messages by e-mail");

}

}

/**

* Match the method specified by the user with the sub-factory, using the factory to achieve

*

*/

class factory{

Public void Method (String a) {

if ("SMS". Equals (a)) {new smsfactory (). Send ();}

Else if ("Email". Equals (a)) {new mailfactory (). Send ();}

Else

System. out. Print ("input error");

}

}

/**

* Define User

*/

Public Static void Main (String args[]) {

FactoryMethod temp=New FactoryMethod ();

Factory temp1=temp. New Factory ();

Temp1.method ("SMS");

Temp1.method ("email");

}

}

Output: Send message via SMS

Send a message by e-mail

Java Design pattern (creation: Factory method Mode)

Related Article

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.