\定义一个抽象类Shape,它有两个抽象的方法area()和perimeter(),分别表示求面积和周长.

问题描述:

\定义一个抽象类Shape,它有两个抽象的方法area()和perimeter(),分别表示求面积和周长.

简单工厂模式?
我之前做了个简易计算器的简单工厂模式,效果与你这个是一样的~~~~
你的抽象类Shape里面的方法area()和perimeter(),可以参考的计算器的GetResult()
都是虚方法(使用virtual 关键字),然后在计算面积、周长的子类中分别重写(override关键字)就是了~~~~~~~~~

具体的你看我的简易计算器工程模式 代码:
C# 简单工厂之简易计算器
总共三个类:Operation.csOperationFactory.csProgram.cs
Operation.cs两个操作数,一个运算的虚方法GetResult(),该方法得出运算结果
加减乘除 四个运算类,每个类继承于Operation.cs,每个类都有一个GetResult()方法
,子类的GetResult()用于实现具体的加减乘除运算~~~~~~~~
OperationFactory.cs实例化 加减乘除 四个运算类~~~~~
Program.cs 里面是main()调用运算类~~~~~
Operation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FactoryPattern
{
///
/// 运算类
///
public class Operation
{
#region 运算类,包含两个操作数,一个虚的运算方法
private double _numberA = 0;
///
/// 操作数A
///
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
private double _numberB = 0;
///
/// 操作数B
///
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
#region 虚方法,得到运算结果
public virtual double GetResult()
{
double result = 0;
return result;
}
#endregion
#endregion
}
///
/// 加法类:继承于运算类
///
public class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
///
///减法类:继承于运算类
///
public class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
///
/// 乘法类:继承于运算类
///
public class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
///
/// 除法类:继承于运算类
///
public class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB==0)
{
throw new Exception("除数不能为零!");

}
else
{
result = NumberA / NumberB;
return result;
}

}
}
#region 简单工厂模式结论
/* 若要实现复杂的运算,比如开根号、自然对数、求正余弦等等,
* 只需要:
* (1)增加相应的运算子类
* (2)在工厂的switch子句中添加相应复杂运算的分支
*就OK了
*/
#endregion
}
OperationFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FactoryPattern
{
public class OperationFactory
{
///
/// 根据相应的运算符号,工厂就实例化出合适的运算对象
///
/// 加减乘除等运算符号
///
public static Operation createOperation(string operationType)
{
Operation operation = null;
//分别实例化具体的 加减乘除 运算类~~~~~~~~~
switch (operationType)
{
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/" :
operation = new OperationDiv();
break;
default:
operation = new Operation();
break;
}
return operation;
}
}
}
Program.cs
调用~~~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FactoryPattern
{
class Program
{
static void Main(string[] args)
{
double result = 0;
string operationType = "+";
Operation oper = null;
// 【传入运算符,工厂实例化创造一个相应的运算对象】
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
// 【经过多态,返回父类的方式,实现计算结果】
result = oper.GetResult();
Console.WriteLine(result);
operationType = "-";
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
result = oper.GetResult();
Console.WriteLine(result);
operationType = "*";
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
result = oper.GetResult();
Console.WriteLine(result);
operationType = "/";
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
result = oper.GetResult();
Console.WriteLine(result);
Console.ReadKey();
}
}
}