Java Design Pattern Rookie series (iv) Modeling and implementation of plant method model

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39760895


Factory method Mode (Factory )

Factory method: As the name implies, is called the factory method to produce objects (products).
There are 3 ways to implement factory methods:

first, the Common factory model。 is to create a factory class that creates instances of classes that implement the same interface.

1. UML Modeling Diagram:


2. Code implementation

/** * Demo sample (i): General Factory Method *  Disadvantage: Assuming that the passed string error, the object cannot be created correctly */interface Sender {public void send (); Class Emailsender implements sender {@Overridepublic void send () {System.out.println ("Send with e-mail ...");}} Class Smssender implements sender {@Overridepublic void send () {System.out.println ("Send with SMS ...");}} /** * Product Factory */class sendfactory {public Sender producesender (String type) {if ("email". Equals (Type)) {return new Emailsende R ();} else if ("SMS". Equals (Type)) {return new Smssender ();} else {System.out.println ("No such type ..."); return null;}} /** * Client Test class *  * @author Leo */public class Test {public static void main (string[] args) {//create factory sendfactory SENDFA Ctory = new Sendfactory ();//Sender sender = Sendfactory.producesender ("email");//shipping Sender.send ();}}


Two, multiple factory method mode。is an improvement to the common factory method pattern, in the normal factory method pattern, assuming that the passed string is faulted, the object cannot be created correctly, and multiple factory method patterns provide multiple factory methods to create the objects individually.

1. UML Modeling Diagram:

2. Code implementation

/** * Demo sample (ii): Multiple Plant methods *  * Advantages: Multiple factory method modes are provided for multiple factory methods, respectively creating the object */interface Sender {public void Send ();} Class Emailsender implements sender {@Overridepublic void send () {System.out.println ("Send with e-mail ...");}} Class Smssender implements sender {@Overridepublic void send () {System.out.println ("Send with SMS ...");}} /** * Different methods 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 SENDFA Ctory = new Sendfactory ();//Production product Sender Senderemail = Sendfactory.produceemail ();//shipping Senderemail.send ();}}

third, static factory method mode。The method in the above multiple factory method pattern is set to static, do not need to create an instance, call directly can.

1. UML Modeling Diagram:


2. Code implementation

/** * Demo sample (iii): Static Factory method *  * Advantages: Multiple factory method modes are provided with multiple factory methods, respectively creating the object */interface Sender {public void Send ();} Class Emailsender implements sender {@Overridepublic void send () {System.out.println ("Send with e-mail ...");}} Class Smssender implements sender {@Overridepublic void send () {System.out.println ("Send with SMS ...");}} /** * Static Factory: Different instance chemical Plant *  * different methods 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) {//Direct production product Sender Senderema Il = Sendfactory.produceemail ();//shipping Senderemail.send ();}}

Iv. Summary

On the whole, there are a lot of products that need to be created, and have a common interface, can be created through the factory method mode. In the above three modes, the first assumes that the passed-in string is incorrect, the object cannot be created correctly, and the third is not necessary to instantiate the chemical plant class, so in most cases we will choose the third- static factory method pattern .



Java Design Pattern Rookie series (iv) Modeling and implementation of plant method model

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.