Java (simple factory) and java (factory from scratch)
Implementation of simple factory
Implement a calculator: enter two numbers and operators to obtain the result.
Operation class
package com.pb.demo1;public class Operation { private double num1; private double num2; public double getResult(double num1,double num2){ double result=0; return result; } 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; } }
Add, subtract, show, divide, modulo
Package com. pb. demo1;/** addition */public class AddOperation extends Operation {@ Override public double getResult (double num1, double num2) {return (num1 + num2 );}}
Package com. pb. demo1;/** subtraction */public class Substruction extends Operation {@ Override public double getResult (double num1, double num2) {return (num1-num2 );}}
Package com. pb. demo1;/** /*/public class Multiplication extends Operation {@ Override public double getResult (double num1, double num2) {return (num1 * num2 );}}
Package com. pb. demo1;/** Division */public class Division extends Operation {@ Override public double getResult (double num1, double num2) {return (num1/num2 );}}
Package com. pb. demo1;/** remainder */public class ModeOperation extends Operation {@ Override public double getResult (double num1, double num2) {return (num1% num2 );}}Factory type:
Package com. pb. demo1;/** simple Factory class */public class Factory {// The return type is the class Object public Operation getResult (String op) {Operation operation = null; // declare the object based on the operator and return switch (op) {case "+": operation = new AddOperation (); break; case "-": operation = new Substruction (); break; case "*": operation = new Multiplication (); break; case "/": operation = new Division (); break; case "% ": operation = new ModeOperation (); break; def Ault: System. err. println ("the input operator is incorrect! "); Break;} // return the generated object return operation ;}}Test class:
Package com. pb. demo1; import java. util. inputMismatchException; import java. util. calculator;/** calculator */public class Demo1 {public static void main (String [] args) {// declare the Scanner type variable Scanner input = new calculator (System. in); // declare the Factory object Factory fac = new Factory (); try {System. out. println ("Enter the first INTEGER:"); double num1 = input. nextDouble (); System. out. println ("enter the second INTEGER:"); double num2 = input. nextDouble (); System. out. println (" Enter the operators +,-, *,/, % "); String op = input. next (); // The response object of the receiving factory. The factory generates the object Operation operation = fac based on the operator. getResult (op); // receives the calculation result double result = operation. getResult (num1, num2); // displays the calculation result System. out. println (num1 + "and" + num2 + "running result:" + result);} catch (InputMismatchException e) {System. err. println ("incorrect input type! ");} Catch (Exception e) {e. printStackTrace ();}}}