您好,欢迎访问三七文档
1青岛理工大学课程实验报告课程名称软件设计与体系结构班级实验日期2016.05.06姓名学号实验成绩实验名称工厂模式实验实验目的及要求(1)熟悉工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)的核心思想。(2)掌握利用反射技术改进工厂模式的编程方法;实验环境Win7,VS2010实验内容1.利用简单工厂模式实现四则运算程序(1)实现抽象的运算类(2)实现加减乘除运算子类(3)实现运算工厂2.利用反射技术改写工厂类(1)创建配置文件,存储目前使用的运算符;(2)使用反射技术根据配置文件的内容创建运算对象。算法描述及实验步骤1、创建控制台程序2、编写使用简单工厂模式实现四则运算程序3、运行调试程序4、创建配置文件存储运算符5、使用反射技术根据配置文件的内容创建运算对象6、运行调试程序调试过程及实验结果使用反射技术前使用反射技术后总结通过这次实验,我掌握了使用简单工厂模式编写四则运算程序,并能够通过反射技术改写工厂类。此次实验较为简单,主要考察面向对象部分,只在C#部分由于自己长时间未用有些2淡忘,一些地方通过上网查资料并对比老师的代码才将程序写得完整。附录客户端usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication2{classProgram{staticvoidMain(string[]args){try{Console.Write(请输入第一个数:);stringstrNumberOne=Console.ReadLine();Console.Write(请输入运算符:);stringstrOperate=Console.ReadLine();Console.Write(请输入第二个数:);stringstrNumberTwo=Console.ReadLine();Operationoperate=OperationFactory.GetOperation(strOperate);operate.NumberOne=Convert.ToDouble(strNumberOne);operate.NumberTwo=Convert.ToDouble(strNumberTwo);doubleresult=operate.getResult();Console.WriteLine(strNumberOne+strOperate+strNumberTwo+=+result);Console.ReadLine();}catch(Exceptionex){Console.WriteLine(您输入的数据有错误!+ex.ToString());Console.ReadLine();}}}}抽象运算类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;3namespaceConsoleApplication2{classOperation{privatedoublenumberOne=0;privatedoublenumberTwo=0;publicdoubleNumberOne{get{returnnumberOne;}set{numberOne=value;}}publicdoubleNumberTwo{get{returnnumberTwo;}set{numberTwo=value;}}publicvirtualdoublegetResult(){return0;}}}加法类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication2{classOperationAdd:Operation{publicoverridedoublegetResult(){doubleresult=0;result=NumberOne+NumberTwo;returnresult;}}}4减法类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication2{classOperationSub:Operation{publicoverridedoublegetResult(){doubleresult=0;result=NumberOne-NumberTwo;returnresult;}}}乘法类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication2{classOperationMul:Operation{publicoverridedoublegetResult(){doubleresult=0;result=NumberOne*NumberTwo;returnresult;}}}除法类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;5namespaceConsoleApplication2{classOperationDiv:Operation{publicoverridedoublegetResult(){doubleresult=0;if(NumberTwo==0.0){throw(newException(除数不能为0!));}result=NumberOne/NumberTwo;returnresult;}}}工厂类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication2{classOperationFactory{publicstaticOperationGetOperation(Stringstr){OperationOpA=null;if(str.Equals(+)){OpA=newOperationAdd();}elseif(str.Equals(-)){OpA=newOperationSub();}elseif(str.Equals(*)){OpA=newOperationMul();}elseif(str.Equals(/)){6OpA=newOperationDiv();}else{throw(newException(输入的运算符有错误!));}returnOpA;}}}使用反射技术后的客户端usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Configuration;usingSystem.Reflection;namespaceConsoleApplication2{classProgram{staticvoidMain(string[]args){try{Console.Write(请输入第一个数:);stringstrNumberOne=Console.ReadLine();Console.Write(请输入第二个数:);stringstrNumberTwo=Console.ReadLine();Operationoperation;Stringoperationstring=ConfigurationManager.AppSettings[Sub];operation=(Operation)Assembly.Load(ConsoleAppliation2).CreateInstance(operationstring);operation.NumberOne=Convert.ToDouble(strNumberOne);operation.NumberTwo=Convert.ToDouble(strNumberTwo);doubleresult=operation.getResult();Console.Write(使用反射的方法进行减运算);Console.WriteLine();Console.WriteLine(strNumberOne+-+strNumberTwo+=+result);Console.ReadLine();}7catch(Exceptionex){Console.WriteLine(您输入的数据有错误!+ex.ToString());Console.ReadLine();}}}}配置文件?xmlversion=1.0encoding=utf-8?configurationappSettingsaddkey=Addvalue=ConsoleApplication2.OperationAdd/addkey=Subvalue=ConsoleApplication2.OperationSub/addkey=Mulvalue=ConsoleApplication2.OperationMul/addkey=Divvalue=ConsoleApplication2.OperationDiv//appSettings/configuration
本文标题:03-工厂模式实验
链接地址:https://www.777doc.com/doc-6686202 .html