说到设计模式,其实无非就是对编程进行分装,多态,继承的综合运用,从而衍生到了那么多的设计模式,面向对象技术,就是为了让我们的程序更加的灵活,维护性更好,复用性更高。
比如说我国的四大发明,其中有一样就和我们的面向对象技术有异曲同工之妙——活字印刷术,他不在像人们的过程思维了,比如要印一篇《兰亭集序》,那么就刻一个《兰亭集序》的版,错一个字,整版废除,印完一般,整版再次废除,无意中加了太多的工作量。而用到了面向对象的活字印刷术,我把需要的字刻出来,要印什么文章,我就把模子找出来,不用的我收起来,没有的字我再刻一个,简单明了,以后再用,这样的话变实现了灵活性,复用性,可扩展性。这也正式为什么他可以和无中生有的纸,火药,指南针并列为四大发明。
今天我就为大家介绍几个运用面向对象技术的几个设计模式:简单工厂模式,工厂方法模式,抽象工厂模式。
//具体的算之前,做一个运算的类
public class Operation
{
private double numberA = 0;
private double numberB = 0;
public double NumberA
{
get
{
return numberA;
}
set
{
numberA = value;
}
}
public double NumberB
{
get
{
return numberB;
}
set
{
numberB = value;
}
}
public virtual double GetResult()
{
double result = 0;
return result;
}
}
//定义加法类,继承了运算类
class OperationPlus : Operation
{
public override double GetResult() //重写基类的方法
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
//定义减法类,继承了运算类
class OperationMinus : Operation
{
public override double GetResult()//重写基类方法
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
//定义了乘法类,继承了运算类
class OperationMul : Operation
{
public override double GetResult()//重写基类方法
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
//定义了除法类,继承了运算类
class OperationDiv : Operation
{
public override double GetResult()//重写基类方法
{
double result = 0;
//判断分母是否为零
if (NumberB == 0)
throw new Exception("除数不能为零");
result = NumberA / NumberB;
return result;
}
}
public class OperationFactory { public static Operation createOperate(string operate) { Operation oper = null; switch (operate) { case "+": oper = new OperationPlus(); break; case "-": oper = new OperationMinus(); break; case "*": oper = new OperationMul(); break ; case "/": oper = new OperationDiv(); break; } return oper; }
static void Main(string[] args)
{
Operation oper;
oper = OperationFactory.createOperate("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
}
运用这个程序,如果我们要加一个算法,我们就可以再加一个算法类,然后再加一个case就可以了。
//运算类
class Operation
{
private double _NumberA = 0;
private double _NumberB = 0;
public double NumberA
{
get { return _NumberA; }
set { _NumberA = value; }
}
public double NumberB
{
get { return _NumberB;}
set { _NumberB = value; }
}
public virtual double GetResult()
{
double result = 0;
return result;
}
}
算法
//继成运算类的加法运算 class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } //继承运算类的减法运算 class OPerationSub : Operation { public override double GetResult() { double result=0; result = NumberA - NumberB; return result; } } //继承运算类的乘法运算 class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } //继承运算类除法运算 class OperationDiv : Operation { public override double GetResult() { double result = 0; //判断分母是否为零 if (NumberB == 0) throw new Exception("除数不能为0"); result = NumberA / NumberB; return result; } }工厂方法实现,先创建一个接口
interface IFactory { Operation CreateOperation(); }
//加法工厂 class AddFactory:IFactory { public Operation CreateOperation() { return new OperationAdd(); } } //减法工厂 class SubFactory : IFactory { public Operation CreateOperation() { return new OPerationSub(); } } //乘法工厂 class MulFactory : IFactory { public Operation CreateOperation() { return new OperationMul(); } } //除法工厂 class DivFactory : IFactory { public Operation CreateOperation() { return new OperationDiv(); } }客户端代码
IFactory operFactory = new AddFactory();
Operation oper = operFactory.CreateOperation(); //实例化算法
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
{
void Insert(User user);
User GetUser(int id);
}
interface IFactory
{
IUser CreateUser();
IDepartment CreateDepartment();
}
interface IDepartment
{
void Insert(Department department);
Department GetDepartment(int id);
}
//数据库访问
class User
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
class Department
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
private string deptName;
public string DeptName
{
get { return deptName; }
set { deptName = value; }
}
}
class SqlserverDepartment:IDepartment //用于访问SqlServer的department
{
public void Insert(Department department)
{
Console.WriteLine("在department表中添加一条记录");
}
public Department GetDepartment(int id)
{
Console.WriteLine("在sqlserver中根据ID查找department表中的一条记录");
return null;
}
}
//用于访问Access的department
class AccessDepartment : IDepartment
{
public void Insert(Department department)
{
Console.WriteLine("在department表中添加一条记录");
}
public Department GetDepartment(int id)
{
Console.WriteLine("在Access中根据ID查找department表中的一条记录");
return null;
}
}
//实例化sqlserverUser
class SqlServerFactory:IFactory
{
public IUser CreateUser()
{
return new SqlserverUser();
}
public IDepartment CreateDepartment()
{
return new SqlserverDepartment();
}
}
//实例化AccessUser
class AccessFactory : IFactory
{
public IUser CreateUser()
{
return new AccessUser();
}
public IDepartment CreateDepartment()
{
return new AccessDepartment();
}
}
客户端代码
User user = new User(); Department department = new Department(); IFactory factory = new SqlServerFactory(); IUser iu = factory.CreateUser(); iu.Insert(user); iu.GetUser(1); IDepartment id = factory.CreateDepartment(); id.Insert(department); id.GetDepartment(2); Console.Read();结果如下