1、(word完整版)03-工厂模式实验青 岛 理 工 大 学课程实验报告课程名称软件设计与体系结构班级实验日期2016。05.06姓名学号实验成绩实验名称工厂模式实验实验目的及要求(1)熟悉工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)的核心思想。(2)掌握利用反射技术改进工厂模式的编程方法;实验环境Win7, VS2010实验内容1。 利用简单工厂模式实现四则运算程序(1)实现抽象的运算类(2)实现加减乘除运算子类(3)实现运算工厂2。 利用反射技术改写工厂类(1)创建配置文件,存储目前使用的运算符;(2)使用反射技术根据配置文件的内容创建运算对象.算法描述及实验步骤1、创建控制台程序2
2、、编写使用简单工厂模式实现四则运算程序3、运行调试程序4、创建配置文件存储运算符5、使用反射技术根据配置文件的内容创建运算对象6、运行调试程序调试过程及实验结果使用反射技术前使用反射技术后总结通过这次实验,我掌握了使用简单工厂模式编写四则运算程序,并能够通过反射技术改写工厂类。此次实验较为简单,主要考察面向对象部分,只在C部分由于自己长时间未用有些淡忘,一些地方通过上网查资料并对比老师的代码才将程序写得完整。附录客户端using System;using System.Collections。Generic;using System。Linq;using System。Text;namespa
3、ce ConsoleApplication2 class Program static void Main(string args) try Console.Write(”请输入第一个数:”); string strNumberOne = Console。ReadLine(); Console.Write(请输入运算符:”); string strOperate = Console。ReadLine(); Console。Write(”请输入第二个数:”); string strNumberTwo = Console。ReadLine(); Operation operate = Operat
4、ionFactory。GetOperation(strOperate); operate.NumberOne = Convert。ToDouble(strNumberOne); operate。NumberTwo = Convert。ToDouble(strNumberTwo); double result = operate。getResult(); Console.WriteLine(strNumberOne + strOperate + strNumberTwo + = + result); Console。ReadLine(); catch (Exception ex) Console
5、。WriteLine(”您输入的数据有错误!” + ex。ToString(); Console.ReadLine(); 抽象运算类using System;using System。Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2 class Operation private double numberOne=0; private double numberTwo=0; public double NumberOne get return numberOne; set
6、numberOne = value; public double NumberTwo get return numberTwo; set numberTwo = value; public virtual double getResult() return 0; 加法类using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2 class OperationAdd:Operation public override double g
7、etResult() double result = 0; result = NumberOne + NumberTwo; return result; 减法类using System;using System.Collections。Generic;using System。Linq;using System.Text;namespace ConsoleApplication2 class OperationSub:Operation public override double getResult() double result=0; result = NumberOne - Number
8、Two; return result; 乘法类using System;using System。Collections.Generic;using System。Linq;using System.Text;namespace ConsoleApplication2 class OperationMul:Operation public override double getResult() double result=0; result = NumberOne NumberTwo; return result; 除法类using System;using System.Collection
9、s.Generic;using System。Linq;using System。Text;namespace ConsoleApplication2 class OperationDiv:Operation public override double getResult() double result=0; if (NumberTwo = 0。0) throw (new Exception(除数不能为0!)); result = NumberOne / NumberTwo; return result; 工厂类using System;using System.Collections.Ge
10、neric;using System。Linq;using System。Text;namespace ConsoleApplication2 class OperationFactory public static Operation GetOperation(String str) Operation OpA = null; if (str.Equals(+”) OpA = new OperationAdd(); else if (str.Equals(”) OpA = new OperationSub(); else if (str。Equals(*)) OpA = new Operat
11、ionMul(); else if (str。Equals(”/”) OpA = new OperationDiv(); else throw (new Exception(”输入的运算符有错误!”)); return OpA; 使用反射技术后的客户端using System;using System.Collections.Generic;using System。Linq;using System.Text;using System.Configuration;using System。Reflection;namespace ConsoleApplication2 class Progr
12、am static void Main(string args) try Console。Write(请输入第一个数:”); string strNumberOne = Console。ReadLine(); Console.Write(”请输入第二个数:”); string strNumberTwo = Console.ReadLine(); Operation operation; String operationstring = ConfigurationManager.AppSettingsSub”; operation = (Operation)Assembly.Load(”Cons
13、oleAppliation2”)。CreateInstance(operationstring); operation。NumberOne = Convert。ToDouble(strNumberOne); operation。NumberTwo = Convert。ToDouble(strNumberTwo); double result = operation。getResult(); Console。Write(”使用反射的方法进行减运算); Console。WriteLine(); Console.WriteLine(strNumberOne + ”-” + strNumberTwo
14、+ = + result); Console。ReadLine(); catch (Exception ex) Console。WriteLine(您输入的数据有错误! + ex。ToString(); Console。ReadLine(); 配置文件?xml version=”1。0” encoding=utf8 ?configuration add key=Add value=”ConsoleApplication2。OperationAdd”/ add key=”Sub value=ConsoleApplication2.OperationSub”/ add key=Mul value=ConsoleApplication2.OperationMul”/ /configuration7