A simple factory model is a factory object (Factory) that determines which product class instances are created.
Requirements: Simulate a calculator class
First, define an abstract operator class
1 PackageCom.sun.FactoryMethod;2 3 Public Abstract classabsoperation {4 protectedDouble _numbera =0d;5 protectedDouble _numberb =0d;6 7 PublicDouble Get_numbera () {8 return_numbera;9 }Ten One Public voidSet_numbera (Double _numbera) { A This. _numbera =_numbera; - } - the PublicDouble Get_numberb () { - return_numberb; - } - + Public voidSet_numberb (Double _numberb) { - This. _numberb =_numberb; + } A at protected AbstractDouble GetResult (); -}
An abstract method that inherits an abstract class that implements a number addition:
1 PackageCom.sun.FactoryMethod;2 3 Public classOperationaddextendsabsoperation {4 5 @Override6 protectedDouble GetResult () {7Double result =0d;8result = _numbera +_numberb;9 returnresult;Ten } One}
An abstract method that inherits an abstract class that implements a number subtraction:
1 PackageCom.sun.FactoryMethod;2 3 Public classOperationsubextendsabsoperation {4 5 @Override6 protectedDouble GetResult () {7Double result =0d;8result = _numbera-_numberb;9 returnresult;Ten } One}
Provide a factory class, according to the operator to instantiate a calculation instance, the resulting absoperation is actually a reference to his child (Java sub-class can be cast to the parent class, the parent class cannot be cast to subclass, unless
A reference to a parent class is a pointer to a subclass):
1 PackageCom.sun.FactoryMethod;2 3 Public classOperationfactory {4 Public StaticAbsoperation Createoperate (Charoperate) {5Absoperation operation =NULL;6 Switch(operate) {7 Case+:8Operation =NewOperationadd ();9 Break;Ten Case‘-‘: OneOperation =Newoperationsub (); A Break; - default: -Operation =NewOperationadd (); the Break; - } - - returnoperation; + } -}
Here is the test class
1 PackageCom.sun.FactoryMethod;2 3 Public classtestoperation {4 5 Public Static voidMain (string[] args) {6 absoperation Operadd;7Operadd = operationfactory.createoperate (' + '));8Operadd._numbera =1d;9Operadd._numberb =2d;Ten System.out.println (Operadd.getresult ());//3.0 One A absoperation opersub; -Opersub = Operationfactory.createoperate ('-')); -Opersub._numbera =2d; theOpersub._numberb =4d; - System.out.println (Opersub.getresult ());//-2.0 - } - +}
Here, by the way, a switch case statement is noted here that the parameters of switch () are not supported by strings (string) before jdk1.7, and are now supported;
So what happens if I don't write a break when I'm in the switch case?
If our case ' + ' does not write a break, then he will always find the next method of break to return; the result of Test return is -1,-2;
If it is our case '-' without a break, then he will always find the next method of break to return; then he will go to default: if there is another ' * ' behind case '-', then he will instantiate ' * '
The result, of course, we have to ensure that ' * ' has a break;
The default, he does not need to break;
Design mode-Simple Factory mode