📄 strategy.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignDll
{
/*
* 策略模式(Strategy)操作
* 它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户
*/
//以下样例有采用策略模式,加上简单工厂模式来处理。
/// <summary>
/// 定义一个抽象类
/// </summary>
public abstract class Strategy
{
private double _price = 0;
private int _count = 0;
/// <summary>
/// 单价
/// </summary>
public double Price
{
get { return _price; }
set { _price = value; }
}
/// <summary>
/// 数量
/// </summary>
public int Count
{
get { return _count; }
set { _count = value; }
}
public abstract void AlgorithmInterface();
//计算方法的结果相当提供一个接口
public abstract double GetResultInterface(double dPrice , int iCount);
}
/// <summary>
/// 具体算法/功能
/// </summary>
public class ConcreateStragegyA : Strategy
{
public override void AlgorithmInterface()
{
//实现A的算法
//throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 实现正常收费功能 单价*数量
/// </summary>
/// <returns></returns>
public override double GetResultInterface(double dPrice , int iCount)
{
return dPrice * iCount;
//throw new Exception("The method or operation is not implemented.");
}
}
/// <summary>
/// 具体算法/功能
/// </summary>
public class ConcreateStragegyB : Strategy
{
public override void AlgorithmInterface()
{
//实现B的算法
//throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 打打9折优惠
/// </summary>
/// <returns></returns>
public override double GetResultInterface(double dPrice, int iCount)
{
return dPrice * iCount * 0.9;
//throw new Exception("The method or operation is not implemented.");
}
}
/// <summary>
/// 具体算法功能
/// </summary>
public class ConcreateStragegyC : Strategy
{
public override void AlgorithmInterface()
{
//实现C的算法
//throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 打7.5折优惠
/// </summary>
/// <returns></returns>
public override double GetResultInterface(double dPrice, int iCount)
{
return dPrice * iCount * 0.75;
//throw new Exception("The method or operation is not implemented.");
}
}
/// <summary>
/// 具体算法/功能
/// </summary>
public class ConcreateStragegyD : Strategy
{
public override void AlgorithmInterface()
{
throw new Exception("The method or operation is not implemented.");
}
public override double GetResultInterface(double dPrice, int iCount)
{
return dPrice * iCount * 0.5;
//throw new Exception("The method or operation is not implemented.");
}
}
/// <summary>
/// 用来管控操作
/// </summary>
public class Context
{
Strategy strategy;
//定义构造函数
public Context(int itype)
{
switch(itype)
{
case 0:
this.strategy = new ConcreateStragegyA();
break;
case 1:
this.strategy = new ConcreateStragegyB();
break;
case 2:
this.strategy = new ConcreateStragegyC();
break;
case 3:
this.strategy = new ConcreateStragegyD();
break;
}
}
/// <summary>
/// 结合上下文的接口
/// </summary>
public void ContextInterface()
{
strategy.AlgorithmInterface();
}
public double ContextResultInterface(double dPrice, int iCount)
{
if (strategy != null)
return strategy.GetResultInterface(dPrice, iCount);
else
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -