Java Design Pattern -------- simple factory pattern of creation Pattern

Source: Internet
Author: User

Java Design Pattern -------- simple factory pattern of creation Pattern

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

The factory mode is the most common mode, because the factory mode is equivalent to creating A new instance object. We often need to generate instance Objects Based on Class, such as a A = new (), the factory mode is also used to create instance objects. Therefore, you need to have multiple eyes in the future when you are new. Can you consider using the factory mode.

The simple factory model can be divided into three modes:

1) Common simple factory Model

2) simple multi-method factory Model

3) simple factory mode with Multiple Static Methods

Common simple factory mode:

Example: (simple arithmetic operation)

1. Create a common interface for the four arithmetic operations:

// Operation interface public interface Operator {public double Operate (double d, double e );}

2. Create an implementation class for addition, subtraction, multiplication, division:

// Addition class public class AddOperator implements Operator {@ Overridepublic double Operate (double a, double B) {// TODO Auto-generated method stubreturn a + B ;}}
public class SubOperator implements Operator {@Overridepublic double Operate(double a, double b) {// TODO Auto-generated method stubreturn a - b;}}
public class MulOperator implements Operator {@Overridepublic double Operate(double a, double b) {// TODO Auto-generated method stubreturn a * b ;}}
Public class DivOperator implements Operator {@ Overridepublic double Operate (double a, double B) {try {if (B = 0) throw new Exception ("the divisor cannot be 0! ");} Catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} return a/B ;}}

3. Create a factory class:

public class OperatorFactory {public Operator createOperator(String operate){Operator oper = null;switch(operate){case "+" :oper = new AddOperator();break;case "-" :oper = new SubOperator();break;case "*" :oper = new MulOperator();break;case "/" :oper = new DivOperator();break;}return oper;}}

4. Finally, it is the test class:

public class FactoryTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubOperatorFactory factory = new OperatorFactory();Operator oper = factory.createOperator("*");double result = oper.Operate(3.6,4.8);System.out.println(result);}}

Simple factory mode with multiple methods:

In the preceding common simple factory mode, imagine that an object cannot be correctly created if the passed parameter is null or another invalid character, the multi-method simple factory mode provides multiple factory methods to create objects respectively.

The difference is only in the factory class. The changes are as follows:

public class OperatorFactory {public Operator createAddOperator(){return new AddOperator();}public Operator createSubOperator(){return new SubOperator();}public Operator createMulOperator(){return new MulOperator();}public Operator createDivOperator(){return new DivOperator();}}
In this way, you do not need to pass the corresponding parameters during the test. You only need to call the required factory method:

public class FactoryTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubOperatorFactory factory = new OperatorFactory();/*Operator oper = factory.createOperator("*");*/Operator oper = factory.createMulOperator();double result = oper.Operate(3.6,4.8);System.out.println(result);}}


Simple factory mode with Multiple Static Methods:

The so-called static method sets the factory method in the above factory class to static. You can directly call it without creating an instance.

public class OperatorFactory {public static Operator createAddOperator(){return new AddOperator();}public static Operator createSubOperator(){return new SubOperator();}public static  Operator createMulOperator(){return new MulOperator();}public static Operator createDivOperator(){return new DivOperator();}}
As shown above, if the factory method is set to static, it can be called directly during testing.

public class FactoryTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub //OperatorFactory factory = new OperatorFactory();/*Operator oper = factory.createOperator("*");*/Operator oper = OperatorFactory.createMulOperator();double result = oper.Operate(3.6,4.8);System.out.println(result);}}

In general, the factory mode is suitable: when a large number of products need to be created and have common interfaces, they can be created through 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 static 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.