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

Source: Internet
Author: User

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

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.