Calculator Code Constructed in "simple factory mode" After Optimization-C # "reflection" Technology

Source: Internet
Author: User

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 );}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.