Water
A frog is laughing.
--Stone and water
Factory method Mode (Factory), which defines an interface for creating objects, letting the implementation class decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.
Here we write the factory on the basis of simple and factory:
Define an interface first
1 Package Cn.no5.factorymethod; 2 3 import cn.no1.simplefactory.Operation; 4 5 Public Interface ifactory {67 operation CreateOperation (); 8 }
Then define its subclasses:
1 Package Cn.no5.factorymethod;2 3 import cn.no1.simplefactory.Operation;4 import Cn.no1.simplefactory.OperationAdd;5 import Cn.no1.simplefactory.OperationSub;6 import cn.no1.simplefactory.OperationMultiply;7 import cn.no1.simplefactory.OperationDivide;8 9 Public classAddfactory implements Ifactory {Ten One @Override A PublicOperation CreateOperation () { - return NewOperationadd (); - } the - } - Public classSubfactory implements Ifactory { - + @Override - PublicOperation CreateOperation () { + return Newoperationsub (); A } at - } - Public classMultiplyfactory implements Ifactory { - - @Override - PublicOperation CreateOperation () { in return Newoperationmultiply (); - } to + } - Public classDividefactory implements Ifactory { the * @Override $ PublicOperation CreateOperation () {Panax Notoginseng return Newoperationdivide (); - } the +}
Test class:
1 Package Cn.no5.factorymethod;2 3 import Java.util.Scanner;4 5 import cn.no1.simplefactory.Operation;6 7 Public class_test {8 9 Public Static voidMain (string[] args) {TenScanner sc =NewScanner (System.inch); OneSystem. out. println ("Enter the number 1:"); A DoubleNumA =sc.nextdouble (); -System. out. println ("Enter the number 2:"); - DoubleNumB =sc.nextdouble (); theSystem. out. println ("input Operator:"); -Stringoperator=Sc.next (); - sc.close (); -Ifactory operfactory =NULL; + Switch(operator) { - Case "+": +Operfactory =Newaddfactory (); A Break; at Case "-": -Operfactory =Newsubfactory (); - Break; - Case "*": -Operfactory =Newmultiplyfactory (); - Break; in Case "/": -Operfactory =Newdividefactory (); to Break; + - } theOperation oper =operfactory.createoperation (); * Oper.setnuma (NumA); $ Oper.setnumb (NumB);Panax Notoginseng Doubleresult =oper.calculate (); -System. out. println ("The calculation results are:"+result); the } +}
It should now be possible to find the difference between a simple factory and a factory method:
Factory method More trouble than a simple factory? This emotional response cannot be counted, and the difference between them must be analyzed from the process.
Simple factory: The client does not have to decide which instance object to create, but it needs to pass the symbolic data to the simple factory, and the simple factory returns the instance object based on the incoming symbol.
Factory Method: The client needs to determine which factory object is created according to the symbol, so no more symbols are passed in, and the factory object calls the method directly to return the instance object.
To consider such a scenario, you need to create 10 addition instances:
If you use a simple factory, you need to pass 10 symbols (such as 10 times "+") to the simple factory, since know all is addition, why repeat 10 times "+"?! Sorry, the client does not consider these, he is only responsible for the factory to pass parameters to get examples;
If you use the factory method, create the addition factory directly and call its production method 10 times.
The scope of use of the simple factory is clear. is to use simple factories in a relatively simple situation. Like calculators. The client only operation once, get an operation instance, but who do not know what user to enter what operation symbol, simply throw the symbol to simple factory.
Java Learning notes--the design pattern of the five. Factory method