The modified calculator Code has the following problem: 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! Using C # reflection technology can solve the problem well. The following code does not use DLL. For example:
-----------------------------------
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;
}
}
/// <Summary>
/// Addition class
/// </Summary>
Class operationadd: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera + numberb;
Return result;
}
}
/// <Summary>
/// Subtraction class
/// </Summary>
Class operationsub: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera-numberb;
Return result;
}
}
/// <Summary>
/// Multiplication class
/// </Summary>
Class operationmul: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera * numberb;
Return result;
}
}
/// <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;
}
}
/// <Summary>
/// Computing Factory
/// </Summary>
Class operationfactory
{
Public static operation createoperate (string operate)
{
Operate = "operator." + operate;
Operation operation;
Object OBJ = activator. createinstance (type. GetType (operate ));
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) {// operationadd operationsub operationmul operationdiv operation; 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 );}}}}