To implement Java-oriented objects, let's write a simple factory to implement examples of Java encapsulation, inheritance, reuse, and, by the way, a simple factory with functional simplicity.
First, simple factory (non-functional)
1, first of all, we put the common attribute method in a common class inside
/** * Created by LK on 2016/5/8. */public class Operation { private double numbera = 0; Private double numberb = 0; Public double Getnumbera () { return numbera; } public void Setnumbera (double numbera) { This.numbera = Numbera; } Public double Getnumberb () { return numberb; } public void Setnumberb (double numberb) { this.numberb = Numberb; } Public double GetResult () { double result = 0; return result;} }
2, if said, we now want to achieve the subtraction of the algorithm, then we create the corresponding class to inherit the above common class
Addition:
/** * Created by LK on 2016/5/8. */public class Operationsadd extends operation {public double GetResult () { double result = 0; result = Getnumbera () + Getnumberb (); return result;} }
Subtraction:
/** * Created by LK on 2016/5/8. */public class Operationsub extends operation{public double GetResult () { double result = 0; result = Getnumbera ()-Getnumberb (); return result;} }
Multiplication:
/** * Created by LK on 2016/5/8. */public class Operationmul extends operation {public double GetResult () { double result = 0; result = Getnumbera () * Getnumberb (); return result;} }
Division:
/** * Created by LK on 2016/5/8. */public class Operationdiv extends operation {public double GetResult () { double result = 0; if (getnumberb () = = 0) { System.out.println ("divisor cannot be 0! "); } result = Getnumbera ()/Getnumberb (); return result;} }
3, now each algorithm has been implemented, because they are fragmented, then we need to centrally manage these algorithms, which is used in factories, with factories to circle them
/** * Created by LK on 2016/5/8. */public class Operationfactory {public static operation Createoperate (String operate) { operation oper = null;< C2/>switch (operate) {case "+": oper = new Operationsadd (); break; Case "-": oper = new Operationsub (); break; Case "*": oper = new Operationmul (); break; Case "/": oper = new Operationdiv (); break; } return oper;} }
4, now is ready, only owe the director of the call
/** * Created by LK on 2016/5/8. */public class Factoryclient {public static void Main (string[] args) { /** * Simple Factory mode, method one * / Operation operation = Operationfactory.createoperate ("/"); Operation.setnumbera (2); Operation.setnumberb (1); Double result = Operation.getresult (); SYSTEM.OUT.PRINTLN (result); /** * Simple Factory mode, method two * //*string oper = "/"; Ioperationfunction operationfunction = Operationfunction.getoperationfunctionstrategy (oper); Double result1 = operationfunction.createoperation (2,1,oper); System.out.println (RESULT1); */ }}
Second, simple factory (functional type)
In order to simplify the generation of the implementation class, we can consider the function of the method to achieve
1. Create a function-type interface
/** * Created by LK on 2016/5/8. */@FunctionalInterface Public interface ioperationfunction {public double createoperation (double Numbera, Double numberb,string oper);}
2, create a function of the implementation of the interface, we put this subtraction algorithm is written in this implementation class, the class can be loaded when the initialization, this way, you need to use the direct call.
/** * Created by LK on 2016/5/8. */public class Operationfunction {private static map<string,ioperationfunction> Operationfunctionmap = new Concu Rrenthashmap<> (); static{/** * Addition operation */Operationfunctionmap.put ("+", (numbera,numberb,oper)->{ Double result = Numbera + Numberb; return result; }); /** * Subtraction Operation */Operationfunctionmap.put ("-", (numbera,numberb,oper)->{double result = Numbera-numberb; return result; }); /** * Multiplication Operation */Operationfunctionmap.put ("*", (Numbera,numberb,oper)-{double result = Numbera * NUMBERB; return result; }); /** * Division Operation */Operationfunctionmap.put ("/", (Numbera,numberb,oper)-{double result = Numbera/numberb; return result; }); } public static Ioperationfunction GetoperationfunctionstrAtegy (String oper) {return operationfunctionmap.get (oper); }}
Now, the difference with non-functional is that we don't need to create a lot of implementations and create four fewer classes
3, now, but also only owed the director of the call
/** * Created by LK on 2016/5/8. */public class Factoryclient {public static void Main (string[] args) { /** * Simple Factory mode, method one */ * Operation operation = Operationfactory.createoperate ("/"); Operation.setnumbera (2); Operation.setnumberb (1); Double result = Operation.getresult (); SYSTEM.OUT.PRINTLN (result); * */** * Simple Factory mode, method two * /String oper = "/"; Ioperationfunction operationfunction = Operationfunction.getoperationfunctionstrategy (oper); Double result1 = operationfunction.createoperation (2,1,oper); System.out.println (RESULT1); }}
Design Patterns-Simple Factory application Java