------Factory Method Pattern for Java 23 design Patterns

Source: Internet
Author: User

First, the factory method mode

There are three types of factory method models

1, Ordinary Factory mode : is to establish a factory class, the implementation of the same interface for some classes to create instances. First look at the diagram:

Here's an example: (Let's give an example of sending an email and a text message)

First, create a common interface for both:

    1. Public interface Sender {
    2. public void Send ();
    3. }

Second, create the implementation class:

    1. public class MailSender implements Sender {
    2. @Override
    3. public void Send () {
    4. System.out.println ("This is mailsender!");
    5. }
    6. }
    1. public class Smssender implements Sender {
    2. @Override
    3. public void Send () {
    4. System.out.println ("This is SMS sender!");
    5. }
    6. }

Finally, build the factory class:

  1. public class Sendfactory {
  2. Public Sender Produce (String type) {
  3. if ("Mail". Equals (type)) {
  4. return new MailSender ();
  5. } else if ("SMS". Equals (type)) {
  6. return new Smssender ();
  7. } else {
  8. System.out.println ("Please enter the correct type!");
  9. return null;
  10. }
  11. }
  12. }

Let's test it out:

    1. public class Factorytest {
    2. public static void Main (string[] args) {
    3. Sendfactory factory = new Sendfactory ();
    4. Sender sender = factory.produce ("SMS");
    5. Sender. Send ();
    6. }
    7. }

Output: This is SMS sender!

2, multiple factory method mode , is the improvement of the common factory method mode, in the normal factory method mode, if the passed string error, the object cannot be created correctly, and the multiple factory method pattern is to provide multiple factory methods, create the object respectively. Diagram:

Make the above code changes, change the next Sendfactory class on the line, as follows:

public class Sendfactory {public Sender producemail () {
    1. return new MailSender ();
    2. }
    3. Public Sender producesms () {
    4. return new Smssender ();
    5. }
    6. }

The test classes are as follows:

    1. public class Factorytest {
    2. public static void Main (string[] args) {
    3. Sendfactory factory = new Sendfactory ();
    4. Sender sender = Factory.producemail ();
    5. Sender. Send ();
    6. }
    7. }

Output: This is mailsender!

2.2, Static factory method mode , the above method of multiple factory methods to static, do not need to create an instance, directly call.

    1. public class Sendfactory {
    2. public static Sender Producemail () {
    3. return new MailSender ();
    4. }
    5. public static Sender producesms () {
    6. return new Smssender ();
    7. }
    8. }
[Java]View Plaincopy
    1. public class Factorytest {
    2. public static void Main (string[] args) {
    3. Sender sender = Sendfactory.producemail ();
    4. Sender. Send ();
    5. }
    6. }

Output: This is mailsender!

In general, the factory model is suitable: when a large number of products need to be created, and have a common interface, can be created through the factory method mode. In the above three modes, the first if the incoming string is wrong, the object is not created correctly, the third is relative to the second, do not need to instance the chemical plant class, so, in most cases, we will choose the third-static factory method mode.

3. Abstract Factory mode (Factory)

The problem with factory method mode is that class creation relies on the factory class, that is, if you want to expand the program, the factory class must be modified, which violates the closure principle, so from the design point of view, there are certain problems, how to solve? Use the abstract factory pattern to create multiple factory classes so that once you need to add new functionality, you can add new factory classes directly, without having to modify the previous code. Because the abstract factory is not very well understood, we first look at the diagram, and then the code, it is more easily understood.

Take a look at the example:

    1. Public interface Sender {
    2. public void Send ();
    3. }

Two implementation classes:

    1. public class MailSender implements Sender {
    2. @Override
    3. public void Send () {
    4. System.out.println ("This is mailsender!");
    5. }
    6. }
    1. public class Smssender implements Sender {
    2. @Override
    3. public void Send () {
    4. System.out.println ("This is SMS sender!");
    5. }
    6. }

Two factory classes:

    1. public class Sendmailfactory implements Provider {
    2. @Override
    3. Public Sender Produce () {
    4. return new MailSender ();
    5. }
    6. }
    1. public class Sendsmsfactory implements provider{
    2. @Override
    3. Public Sender Produce () {
    4. return new Smssender ();
    5. }
    6. }

In providing an interface:

    1. Public interface Provider {
    2. Public Sender produce ();
    3. }

Test class:

    1. public class Test {
    2. public static void Main (string[] args) {
    3. Provider Provider = new Sendmailfactory ();
    4. Sender sender = Provider.produce ();
    5. Sender. Send ();
    6. }
    7. }

In fact, the advantage of this model is that if you want to add a function now: Send timely information, you just need to do an implementation class, to implement the sender interface, at the same time do a factory class, implement Provider interface, OK, no need to change the ready-made code. To do so, expand better!

The content of this article https://www.cnblogs.com/hnrainll/archive/2011/12/29/2305582.html

------Factory Method Pattern for Java 23 design Patterns

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.