Home  >  Article  >  Java  >  Learning the simple factory pattern in Java

Learning the simple factory pattern in Java

黄舟
黄舟Original
2017-10-13 10:16:101096browse

This article mainly introduces the simple factory pattern of java design pattern learning in detail, which has certain reference value. Interested friends can refer to it

Simple Factory Pattern

The simple factory pattern is a creational pattern, also called the Static Factory Method pattern, but it is not one of the 23 GOF design patterns. The simple factory pattern uses a factory object to decide which product class instance to create. The simple factory pattern is the simplest and most practical pattern in the factory pattern family and can be understood as a special implementation of different factory patterns.

Design a calculator using the simple factory pattern.

1: Create an operation class


package EasyFactoryModel;

abstract class Operation {

  public double num1=0;
  public double num2=0;
  public double getNum1() {
    return num1;
  }
  public void setNum1(double num1) {
    this.num1 = num1;
  }
  public double getNum2() {
    return num2;
  }
  public void setNum2(double num2) {
    this.num2 = num2;
  }
  
  public abstract double getResult();
}

2: Create an addition class.


class OpeartionAdd extends Operation {

  public double getResult(){
    double result=0;
    result=num1+num2;
    return result;
  }
}

3: Create a subtraction class.


public class OpeartionSub extends Operation {
 
  public double getResult(){
    double result=0;
    result=num1-num2;
    return result;
  }
}

4: Create a multiplication class.


public class OperationMul extends Operation {

  public double getResult(){
    double result=0;
    result=num1*num2;
    return result;
  }
}

5: Create a division class.


public class Operationp extends Operation {
 
  public double getResult(){
    double result=0;
    result=num1/num2;
    return result;
  }
}

6: After completing the basic operation class, you will find that you need to instantiate different classes according to the required addition, subtraction, multiplication and division. For this matter, you need a factory class.


public class OperationFactory {
 
  public static Operation creatOpera(String operation){
     
    Operation oper=null;
    switch(operation){
     
    case "+":oper=new OpeartionAdd();break;
    case "-":oper=new OpeartionSub();break;
    case "*":oper=new OperationMul();break;
    case "/":oper=new Operationp();break;
     
    }
    return oper;
  }
 
}

The factory class creates different objects according to different operations. Here is the use of polymorphism.

7: Test class


public class Calculator {

  public static void main(String[] args) {
    
    Operation oper;
    String operation="+";
    oper=OperationFactory.creatOpera(operation);
    oper.num1=1;
    oper.num2=2;
    double result=0;
    result=oper.getResult();
    System.out.println("result="+result);
    
  }

}

8: Console output:

result=3.0

Advantages: Simple The factory class is the key to the entire pattern, which contains the necessary logical judgment to determine which specific class object should be created based on external information.
By implementing the simple factory pattern, users do not need to know how the object is created, as long as they pass in Just the necessary information.

Disadvantages: The factory class concentrates the creation logic of all instances, violating the principle of high cohesion allocation.
As the specific product categories in the system continue to increase, it is necessary to continuously modify the factory categories, which is difficult to maintain and expand. At the same time, it also violates the principle of openness and closure.

The above is the detailed content of Learning the simple factory pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn