Java Design Pattern cainiao series (4) Factory method pattern modeling and implementation

Source: Internet
Author: User

Java Design Pattern cainiao series (4) Factory method pattern modeling and implementation

 

Factory Method)

Factory method: As the name implies, it is to call methods in the factory to produce objects (products.
There are three ways to implement the factory method:

I. general factory Model. Is to create a factory class and create instances for some classes that implement the same interface.

1. uml modeling diagram:

2. Code Implementation

 

/*** Example (1): Common factory method ** disadvantage: If the passed string fails, the object cannot be correctly created */interface Sender {public void send ();} class EmailSender implements Sender {@ Overridepublic void send () {System. out. println (use email to send ...);}} class SmsSender implements Sender {@ Overridepublic void send () {System. out. println (sent via SMS ...);}} /*** product factory */class SendFactory {public Sender produceSender (String type) {if (email. equals (type) {return new EmailSender ();} else if (sms. equals (type) {return new SmsSender ();} else {System. out. println (not of this type ...); return null ;}}/ *** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {// create factory SendFactory sendFactory = new SendFactory (); // production product Sender sender = sendFactory. produceSender (email); // delivery sender. send ();}}

 

 

Ii. Multiple factory method Modes. Is an improvement on the common factory method mode. In the common factory method mode, if the passed string is incorrect, the object cannot be created correctly, multiple factory method modes provide multiple factory methods to create objects respectively.

1. uml modeling diagram:

2. Code Implementation

/*** Example (2): Multiple factory methods ** advantage: Multiple factory method modes provide multiple factory methods, create the object */interface Sender {public void send ();} class EmailSender implements Sender {@ Overridepublic void send () {System. out. println (use email to send ...);}} class SmsSender implements Sender {@ Overridepublic void send () {System. out. println (sent via SMS ...);}} /*** different methods are used to produce the corresponding product */class SendFactory {public Sender produceEmail () {return new EmailSender ();} public Sender produceSms () {return new SmsSender () ;}}/*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {// create factory SendFactory sendFactory = new SendFactory (); // production product Sender senderEmail = sendFactory. produceEmail (); // senderEmail for shipping. send ();}}

Iii. Static factory method mode. Set the methods in the above multiple factory method modes to static. You can directly call them without creating an instance.

 

1. uml modeling diagram:

2. Code Implementation

 

/*** Example (3): static factory method ** advantage: Multiple factory method modes provide multiple factory methods, create the object */interface Sender {public void send ();} class EmailSender implements Sender {@ Overridepublic void send () {System. out. println (use email to send ...);}} class SmsSender implements Sender {@ Overridepublic void send () {System. out. println (sent via SMS ...);}} /*** static Factory: Different instantiation factories ** different methods are used to produce corresponding products */class SendFactory {public static Sender produceEmail () {return new EmailSender ();} public static Sender produceSms () {return new SmsSender ();}} /*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {// directly produce the product Sender senderEmail = SendFactory. produceEmail (); // senderEmail for shipping. send ();}}

Iv. Summary

 

 

Generally, when a large number of products need to be created and have common interfaces, they can be created in the factory method mode. In the above three modes, if the input string is incorrect, the object cannot be correctly created, and the third mode does not need to instantiate the factory class. Therefore, in most cases, we will choose the third type --Static 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.