Class Diagram:
Code:
1 Public classOperation {2 3 protected DoubleNumbera = 0;4 protected DoubleNumberb = 0;5 6 7 Public DoubleGetnumbera () {8 returnNumbera;9 }Ten Public voidSetnumbera (DoubleNumbera) { One This. Numbera =Numbera; A } - Public DoubleGetnumberb () { - returnNumberb; the } - Public voidSetnumberb (DoubleNumberb) { - This. Numberb =Numberb; - } + - protected DoubleGetResult () + { A Doubleresult = 0; at returnresult; - } - - -}
1 Public classAddoperationextendsoperation{2 3 @Override4 Public DoubleGetResult ()5 {6 returnNumbera +Numberb;7 8 }9 Ten One}
1 Public classOperationfactory {2 3 Public StaticOperation CreateOperation (String operate)4 {5 if(Operate.equals ("+"))6 {7 return Newaddoperation ();8 }9 Else if(Operate.equals ("*"))Ten { One return Newmutioperation (); A } - return NULL; - } the}
1 Public classClient {2 3 Public Static voidMain (string[] args)4 {5Operation add = operationfactory.createoperation ("+");6Add.setnumbera (1);7Add.setnumberb (2);8 System.out.println (Add.getresult ());9 }Ten}
Design mode-Simple Factory mode