资源描述
(word完整版)03-工厂模式实验
青 岛 理 工 大 学
课程实验报告
课程名称
软件设计与体系结构
班级
实验日期
2016。05.06
姓名
学号
实验成绩
实验名称
工厂模式实验
实验目的
及要求
(1)熟悉工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)的核心思想。
(2)掌握利用反射技术改进工厂模式的编程方法;
实验环境
Win7, VS2010
实验内容
1。 利用简单工厂模式实现四则运算程序
(1)实现抽象的运算类
(2)实现加减乘除运算子类
(3)实现运算工厂
2。 利用反射技术改写工厂类
(1)创建配置文件,存储目前使用的运算符;
(2)使用反射技术根据配置文件的内容创建运算对象.
算法描述及实验步骤
1、创建控制台程序
2、编写使用简单工厂模式实现四则运算程序
3、运行调试程序
4、创建配置文件存储运算符
5、使用反射技术根据配置文件的内容创建运算对象
6、运行调试程序
调试过程及实验结果
使用反射技术前
使用反射技术后
总结
通过这次实验,我掌握了使用简单工厂模式编写四则运算程序,并能够通过反射技术改写工厂类。此次实验较为简单,主要考察面向对象部分,只在C#部分由于自己长时间未用有些淡忘,一些地方通过上网查资料并对比老师的代码才将程序写得完整。
附
录
客户端
using System;
using System.Collections。Generic;
using System。Linq;
using System。Text;
namespace 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 = OperationFactory。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。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 { 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 getResult()
{
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 - NumberTwo;
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.Collections.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.Generic;
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 OperationMul();
}
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 Program
{
static void Main(string[] args)
{
try
{
Console。Write("请输入第一个数:”);
string strNumberOne = Console。ReadLine();
Console.Write(”请输入第二个数:”);
string strNumberTwo = Console.ReadLine();
Operation operation;
String operationstring = ConfigurationManager.AppSettings["Sub”];
operation = (Operation)Assembly.Load(”ConsoleAppliation2”)。CreateInstance(operationstring);
operation。NumberOne = Convert。ToDouble(strNumberOne);
operation。NumberTwo = Convert。ToDouble(strNumberTwo);
double result = operation。getResult();
Console。Write(”使用反射的方法进行减运算");
Console。WriteLine();
Console.WriteLine(strNumberOne + ”-” + strNumberTwo + " = " + result);
Console。ReadLine();
}
catch (Exception ex)
{
Console。WriteLine("您输入的数据有错误!" + ex。ToString());
Console。ReadLine();
}
}
}
}
配置文件
〈?xml version=”1。0” encoding="utf—8" ?>
〈configuration〉
<appSettings>
〈add key="Add" value=”ConsoleApplication2。OperationAdd”/>
〈add key=”Sub" value="ConsoleApplication2.OperationSub”/〉
〈add key="Mul" value="ConsoleApplication2.OperationMul”/〉
<add key="Div” value="ConsoleApplication2.OperationDiv"/〉
〈/appSettings>
〈/configuration〉
7
展开阅读全文