Description: Encapsulates a base class that inherits a base class and can be implemented by a factory class when a different object needs to be instantiated. Example: I am using a calculator applet to achieve. Operation Base class
/// <summary> ///Operation class/// </summary> Public classoperaction {Private Double_numbera =0; Private Double_numberb =0; Public DoubleNumbera {Get{return_numbera;} Set{_numbera =value;} } Public DoubleNumberb {Get{return_numberb;} Set{_numberb =value;} } Public Virtual DoubleGetResult () {Doubleresult =0; returnresult; } }
Op Subclass, I put it in the OperactionAll.cs.
<summary>///+ </summary> class Operactionadd:operaction {//<SUMMARY&G T Calculation method///</summary>///<returns></returns> public override double GetResult () {double result = 0; result = Numbera + Numberb; return result; }}///<summary>////</summary> class Operactionsub:operaction {public over Ride Double GetResult () {double result = 0; result = Numbera-numberb; return result; }}///<summary>///</summary> class Operactionmul:operaction {public over Ride Double GetResult () {double result = 0; result = Numbera * NUMBERB; return result; }}///<summary>//except for//</summary> class Operactiondiv:operaction {public oveRride Double GetResult () {double result = 0; result = Numbera/numberb; return result; } }
Factory class
<summary>/ //Factory class/ //</summary> public class Operactionfactory { //method to create an instance Public static Operaction createoperaction (string operacte) { operaction oper = null; Switch (OPERACTE) {case "+": oper = new Operactionadd (); break; Case "-": oper = new Operactionsub (); break; Case "*": oper = new Operactionmul (); break; Case "/": oper = new Operactiondiv (); break; } return oper; } }
Client Calls
<summary> ///Client call//</summary> class program { static void Main (string[] args ) { operaction oper = null; Oper = operactionfactory.createoperaction ("-"); Create entity Oper from the factory . Numbera = ten; Oper. Numberb = 3; Double result = oper. GetResult ();//Operation Method Console.WriteLine (result); Console.ReadLine (); } }
Finished.
01-Simple Factory