Introduction to simple factory mode, Factory method mode, and abstract Factory mode in java

Source: Internet
Author: User
Tags abstract

The factory mode is suitable for the creation of a large number of objects, and these objects have the same interface, you can use the factory mode to create.

Simple factory mode
There are three simple factory models: common factory models, multiple method factory models, and static method factory models.


--------------------------------------------------------------------------------

Take sending QQ messages or WeChat messages as an example:
Common factory model

/**
* Two common interfaces
*/
Public interface Message {
Public void send ();
}
// QQ message
Public class QQMessage implements Message {
@ Override
Public void send (){
System. out. println ("Send QQ message ");
    }
}
// WeChat message
Public class WeiXinMessage implements Message {
@ Override
Public void send (){
System. out. println ("send WeChat message ");
    }
}
// Message sending factory type
Public class MessageFactory {
Public Message sendMessage (String type ){
If ("QQ". equals (type )){
Return new QQMessage ();
        }
Else if ("WeiXin". equals (type )){
Return new WeiXinMessage ();
} Else {
Return null;
        }
    }
}
// Message sending test
Public class MessageTest {
Public static void main (String [] args ){
MessageFactory messageFactory = new MessageFactory ();
Message message = messageFactory. sendMessage ("QQ ");
Message. send ();
    }
}

 


--------------------------------------------------------------------------------

Multiple method factory modes
In a common factory, if the passed strings are inconsistent with the predefined strings in the factory, the objects cannot be correctly created.
Improvements to the factory category are as follows:

// Message sending factory type
Public class MessageFactory {
Public Message sendQQ (){
Return new QQMessage ();
    }
Public Message sendWeiXin (){
Return new WeiXinMessage ();
    }
}

// Message sending test
Public class MessageTest {
Public static void main (String [] args ){
MessageFactory messageFactory = new MessageFactory ();
Message message = messageFactory. sendWeiXin ();
Message. send ();
    }
}

 


--------------------------------------------------------------------------------

Static method factory mode
In multiple methods, you also need to create a factory object to call the method. You can set the method in engineering to static.
Improvements to the factory category are as follows:

// Message sending factory type
Public class MessageFactory {
Public static Message sendQQ (){
Return new QQMessage ();
    }
Public static Message sendWeiXin (){
Return new WeiXinMessage ();
    }
}
// Message sending test
Public class MessageTest {
Public static void main (String [] args ){
MessageFactory. sendWeiXin (). send ();
    }
}

--------------------------------------------------------------------------------

Factory Method)
Analysis of the preceding simple factory model is easy to understand: the creation of classes depends on the factory class. That is to say, if you want to extend the program, you need to modify the factory class, which violates the closure principle. Therefore, the factory method mode is used: create a factory interface and create multiple factory implementation classes. In this way, the factory class can be directly added once new functions are required, instead of modifying the original code of the factory interface.

/**
* Common interfaces of the two messages
*/
Public interface Message {
Public void send ();
}
// QQ message
Public class QQMessage implements Message {
@ Override
Public void send (){
System. out. println ("Send QQ message ");
    }
}

// WeChat message
Public class WeiXinMessage implements Message {
@ Override
Public void send (){
System. out. println ("send WeChat message ");
    }
}
// Message sending factory interface
Public interface MessageFactory {
Public Message sendMessage ();
}

// Factory type for sending QQ messages
Public class QQFactory implements MessageFactory {
@ Override
Public Message sendMessage (){
Return new QQMessage ();
    }
}
// Factory type for sending WeChat messages
Public class WeiXinFactory implements MessageFactory {
@ Override
Public Message sendMessage (){
Return new WeiXinMessage ();
    }
}
// Message sending test
Public class MessageTest {
Public static void main (String [] args ){
MessageFactory messageFactory = new QQFactory ();
Message message = messageFactory. sendMessage ();
Message. send ();
    }
}

 

--------------------------------------------------------------------------------

Abstract Factory)
The abstract factory model has four roles:

• Abstract Factory role: This role is at the core of the Factory method model.
• Specific Factory role: This role directly creates an instance
• Abstract Product role: the class of this role is the parent class of the object created in the factory method mode or the interfaces they share.
• A specific Product role: This role is a specific Product.

The following is an example of sending a message. If two types of messages can be sent: QQ and WeChat, and QQ can send voice messages and video messages, WeChat can send text messages and video messages, among the four types of messages, voice and text are traffic-free messages, and images and videos are traffic-free messages.

First, create two message interfaces.

// QQ message interface
Public interface QQMessage {
}

// WeChat message interface
Public interface WeiXinMessage {
}
Second: (1) implement two message sending methods under QQ

// Product 1 of QQ message: QQ voice message
Public class SoundQQMessage implements QQMessage {
Public SoundQQMessage (){
System. out. println ("Send QQ message: QQ voice message ");
    }
}
// Product 2 of QQ message: QQ Video Message
Public class VideoQQMessage implements QQMessage {
Public VideoQQMessage (){
System. out. println ("Send QQ message: QQ Video Message ");
    }
}
(2) implement two message sending methods on WeChat

// Specific product 1 under WeChat message: WeChat text message
Public class CharacterWeiXinMessage implements WeiXinMessage {
Public CharacterWeiXinMessage (){
System. out. println ("send WeChat message: WeChat text message ");
    }
}

// Specific product 2 under WeChat message: WeChat Image message
Public class PhotoWeiXinMessage implements WeiXinMessage {
Public PhotoWeiXinMessage (){
System. out. println ("send WeChat message: WeChat Image message ");
    }
}
Third: the interface for sending messages

// The Factory Interface for sending messages
Public interface MessageFactory {
Public QQMessage sendQQMessage ();
Public WeiXinMessage sendWeiXinMessage ();
}
Fourth: implement a specific message sending factory

// Specific factory Type 1: only responsible for sending voice and text to save traffic information
Public class Sound_Character_MessageFactory implements MessageFactory {
@ Override
Public QQMessage sendQQMessage (){
Return new SoundQQMessage ();
    }
@ Override
Public WeiXinMessage sendWeiXinMessage (){
Return new CharacterWeiXinMessage ();
    }
}
// Specific factory Type 2: only responsible for sending video images and traffic charges
Public class Video_Photo_MessageFactory implements MessageFactory {

@ Override
Public QQMessage sendQQMessage (){
Return new VideoQQMessage ();
    }

@ Override
Public WeiXinMessage sendWeiXinMessage (){
Return new PhotoWeiXinMessage ();
    }
}
Fifth: Test message sending

// Send message test
Public class FactoryTest {
Public static void main (String [] args ){
MessageFactory messageFactory1 = null;
System. out. println ("factory 1: send traffic-saving messages ");
MessageFactory1 = new Sound_Character_MessageFactory ();
MessageFactory1.sendQQMessage ();
MessageFactory1.sendWeiXinMessage ();

System. out. println ("................................. ");

MessageFactory messageFactory2 = null;
System. out. println ("factory 2: message sending fee Traffic ");
MessageFactory2 = new Video_Photo_MessageFactory ();
MessageFactory2.sendQQMessage ();
MessageFactory2.sendWeiXinMessage ();
    }
}

 

--------------------------------------------------------------------------------

Difference between factory method mode and abstract Factory mode

• The Factory method mode has only one abstract product class, while the abstract Factory mode has multiple.
• The Factory method mode allows a specific factory to create only one specific product instance, while the abstract Factory mode can create multiple instances.
• The Factory method creates a "one" product. The Abstract factory needs to create a product column. The Abstract factory is like a factory, and the factory method is like a product production line of a 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.