Java Simplified design mode----Simple Factory mode (i)----separation of business logic

Source: Internet
Author: User
Tags mul

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

Related Article

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.