The first chapter in "big talk Design Patterns" is an example of a simple calculator built with a simple factory model, there is a factory class operaationfactory on the P10-P11 page in the book to construct instances of each operation class, but there is a problem here: if you need to add a new operation class, in addition to modifying the interface code, you also need to add a new statement in the switch of operaationfactory! The following problems are solved with reflection ~~~
///////// The following category is edited. DLL /////////////////////////////////////// //
-------------------------------------
Operation. CS
-------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace usage
{
/// <Summary>
/// Operation class
/// </Summary>
Public class operation
{
Private double _ numbera = 0;
Private double _ numberb = 0;
/// <Summary>
/// Number
/// </Summary>
Public double numbera
{
Get
{
Return _ numbera;
}
Set
{
_ Numbera = value;
}
}
/// <Summary>
/// Number B
/// </Summary>
Public double numberb
{
Get
{
Return _ numberb;
}
Set
{
_ Numberb = value;
}
}
/// <Summary>
/// Obtain the calculation result
/// </Summary>
/// <Returns> </returns>
Public Virtual double getresult ()
{
Double result = 0;
Return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
Operationadd. CS
-----------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace usage
{
/// <Summary>
/// Addition class
/// </Summary>
Class operationadd: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera + numberb;
Return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
Operationsub. CS
-----------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace usage
{
/// <Summary>
/// Subtraction class
/// </Summary>
Class operationsub: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera-numberb;
Return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
Operationmul. CS
-----------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace usage
{
/// <Summary>
/// Multiplication class
/// </Summary>
Class operationmul: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera * numberb;
Return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
Operationdiv. CS
-----------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace usage
{
/// <Summary>
/// Division class
/// </Summary>
Class operationdiv: Operation
{
Public override double getresult ()
{
Double result = 0;
If (numberb = 0)
Throw new exception ("the divisor cannot be 0. ");
Result = numbera/numberb;
Return result;
}
}
}
-----------------------------------------
**************************
///////////// The following category is edited. DLL // end /////////////////////////
-------------------------------------------
Operationfactory. CS
-------------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. reflection;
Namespace usage
{
/// <Summary>
/// Computing Factory
/// </Summary>
Public class operationfactory
{
Public static operation createoperate (string dllpath)
{
// Operate = "response." + operate;
Operation operation;
// Object OBJ = activator. createinstance (type. GetType (operate ));
// Operator = (operation) OBJ;
Assembly ass = assembly. loadfrom (dllpath + ". dll ");
Dllpath = "pipeline." + dllpath;
Type type = ass. GetType (dllpath); // obtain the type using the namespace and name of the type
Object OBJ = activator. createinstance (type); // use the specified parameter instance address type
Operator = (operation) OBJ;
Return response;
}
}
}
----------------------------------------- Program. CS ------------------------------------------ using system; using system. collections. generic; using system. text; namespace detail {class program {static void main (string [] ARGs) {operation duration; while (true) {console. write ("Enter the number A:"); string strnumbera = console. readline (); // select the operator to read from the configuration file XML // console. write ("Please select the operator number:/noperationadd represents plus (+)/noperationsub represents minus (-)/noperationmul represents multiplication (*)/noperationdiv represents Division (/) /n "); string stroperate = console. readline (); operator = operationfactory. createoperate (stroperate); console. write ("Enter the number B:"); string strnumberb = console. readline (); iterator. numbera = convert. todouble (strnumbera); random. numberb = convert. todouble (strnumberb); double result = random. getresult (); console. writeline ("Result:" + result );}}}}