Java: a simple design model-a simple factory model (I)-business logic Separation

Source: Internet
Author: User

Java: a simple design model-a simple factory model (I)-business logic Separation

Now the scalability and maintainability are particularly strong. Even if you want to calculate more than one number, it is easy, with few changes. It also prevents malicious programmers from tampering with code.

 

If you need to perform more than two numeric operations, you can add a method to this interface.

 

package separatetwo.operate;public interface Operate {public abstract int getResult(int numberOne, int numberTwo);}

 

 

Addition
package separatetwo.operate;public class Add implements Operate {private Add() {};private static Add addInstance = null;public static Add getAddInstance() {if (addInstance == null) {addInstance = new Add();}return addInstance;}@Overridepublic int getResult(int numberOne, int numberTwo) {return numberOne + numberTwo;}}
Subtraction

 

package separatetwo.operate;public class Sub implements Operate {private Sub() {};private static Sub subInstance = null;public static Sub getSubInstance() {if (subInstance == null) {subInstance = new Sub();}return subInstance;}@Overridepublic int getResult(int numberOne, int numberTwo) {return numberOne - numberTwo;}}

Multiplication

 

 

package separatetwo.operate;public class Mul implements Operate {private Mul() {};private static Mul mulInstance = null;public static Mul getMulInstance() {return mulInstance == null ? new Mul() : mulInstance;}@Overridepublic int getResult(int numberOne, int numberTwo) {return numberOne * numberTwo;}}
Division

 

 

package separatetwo.operate;public class Div implements Operate {private Div() {};private static Div divInstance = null;public static Div getDivInstance() {return divInstance == null ? new Div() : divInstance;}@Overridepublic int getResult(int numberOne, int numberTwo) {if(numberTwo != 0){return numberOne / numberTwo;}return Integer.MIN_VALUE;}}

 

 

All algorithms can be implemented.


Specific usage:

 

Package separatetwo; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import separatetwo. operate. div; public class ShowData {public static void main (String [] args) throws IOException {BufferedReader reader = new BufferedReader (new InputStreamReader (System. in); // enter the first number System. out. println (enter the first number :); String num1 = reader. readLine (); // enter the second number System. out. println (enter the second number :); String num2 = reader. readLine (); // addition // int result = Add. getAddInstance (). getResult (Integer. valueOf (num1), Integer. valueOf (num2); // System. out. print (Calculation Result:); // System. out. println (result); // subtraction // System. out. println (Sub. getSubInstance (). getResult (Integer. valueOf (num1), Integer. valueOf (num2); // multiplication // System. out. println (Mul. getMulInstance (). getResult (Integer. valueOf (num1), Integer. valueOf (num2); // division int result = Div. getDivInstance (). getResult (Integer. valueOf (num1), Integer. valueOf (num2); if (result = Integer. MIN_VALUE) {System. out. println (the divisor is 0);} System. out. println (result );}}



 

 

 

 

 

 

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.