Simple Factory Model-java article

Source: Internet
Author: User

Simple Factory mode is the question of how to instantiate an object, that is, in the end to instantiate who, in the future will not increase the instantiation of the object, such as the Calculator class to add open root elements, should consider a separate class to create the instance of the process, this is the factory. The following examples of calculator classes explain the Simple factory model:

1. Operator classes:

1  PackageCom.lujie;2 3  Public classOperation {4     Private Double_numbera=0;5     Private Double_numberb=0;6     7      Public voidSet_numbera (Double_numbera) {8          This. _numbera =_numbera;9     }Ten      Public voidSet_numberb (Double_numberb) { One          This. _numberb =_numberb; A     } -      Public DoubleGet_numbera () { -         return_numbera; the     } -      Public DoubleGet_numberb () { -         return_numberb; -     } +      Public  DoubleGetResult () { -         DoubleResult=0; +         returnresult; A     } at}

2. Use inheritance and polymorphism to implement code loose coupling:

  2.1 The addition class inherits the self-operator class:

1  PackageCom.lujie;2 3  Public classOperationaddextendsoperation{4     5      Public DoubleGetResult () {6         DoubleResult =0;7result= This. Get_numbera () + This. Get_numberb ();8         returnresult;9         Ten     } One}

  2.2 The subtraction class inherits the self-operator class:

1  PackageCom.lujie;2 3  Public classOperationsubextendsoperation{4     5      Public DoubleGetResult () {6         DoubleResult =0;7result= This. Get_numbera ()- This. Get_numberb ();8         returnresult;9         Ten     } One}

  2.3 The multiplication class inherits the self-operator class:

1  PackageCom.lujie;2 3  Public classOperationmulextendsoperation{4     5      Public DoubleGetResult () {6         DoubleResult =0;7         if( This. Get_numberb () ==0)8result= This. Get_numbera () * This. Get_numberb ();9         returnresult;Ten          One     } A}

  2.4 The Division class inherits from the operator class:

1  PackageCom.lujie;2 3  Public classOperationdivextendsoperation{4     5      Public DoubleGetResult () {6         DoubleResult =0;7         Try {8         9result= This. Get_numbera ()/ This. Get_numberb ();Ten              One}Catch(Exception e) { ASYSTEM.OUT.PRINTLN ("Divisor cannot be 0!") "); -         } -          the         returnresult; -     } -}

3. Simple operation of the factory class:

1 classoperationfactory{2      Public StaticOperation Createoperate (String operate) {3         4Operation Oper=NULL;5         Switch(operate) {6          Case"+":7Oper=NewOperationadd ();8              Break;9          Case"-":TenOper=Newoperationsub (); One              Break; A          Case"*": -Oper=NewOperationmul (); -              Break; the          Case"/": -Oper=NewOperationdiv (); -              Break; -         } +          -         returnOper; +     } A}

4. Factory Model Example:

1  Public Static voidMain (string[] args) {2         3 operation Oper;4Oper=operationfactory.createoperate ("/");5Oper.set_numbera (1);6Oper.set_numberb (0);7         Doubleresult=Oper.getresult ();8SYSTEM.OUT.PRINTLN ("result is:" +result);9}

Simple Factory Model-java 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.