Java (simple factory) and java (factory from scratch)

Source: Internet
Author: User

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 ();}}}

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.