Scalability and maintainability are now particularly strong, and even if you want to count multiple numbers, it's easy and there are few changes. It can also prevent malicious programmers from tampering with the code.
If you need to do more than two numeric operations, add a method to the 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 () {};p rivate static add addinstance = Null;pu Blic 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 () {};p rivate static sub subinstance = Null;pu Blic 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 () {};p rivate static Mul mulinstance = Null;pu Blic 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 () {};p rivate static div divinstance = Null;pu Blic 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;}}
............ A random algorithm 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 {Buffered Reader reader = new BufferedReader (new InputStreamReader (system.in));//Enter the first number System.out.println ("Please enter the first number:"); String NUM1 = Reader.readline ();//Enter second number System.out.println ("Please enter second number:"); String num2 = Reader.readline ();//addition//int result = Add.getaddinstance (). GetResult (Integer.valueof (NUM1), Integer.valueof (num2));//system.out.print ("calculated as:");//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);}}
SOURCE download: Click I download
Java Simplified design mode----Simple Factory mode (i)----separation of business logic