策略模式实现用户充值
一、概念
①什么是策略模式?
Strategy:它定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化不会影响到使用算法的用户。
②如何使用策略模式?
将每个算法都封装起来、定义一个策略类并且在策略类中定义一个所有算法的公共接口、定义一个context类用来维护对strategy对象的引用。
③为什么要使用策略模式?
策略模式可以解决多重条件语句的问题,易于维护。
类图
实战、策略模式实现用户充值
类图
代码实现
Context类
public class Context { RechargeStrategy strategy; //通过构造方法传入具体的收费策略 public Context (RechargeStrategy strategy) { this.strategy = strategy; } //根据收费策略返回不同的结果 public decimal ContextInterface(decimal money ) { return strategy.Recharge(money); } }
RechargeStrategy类
abstract public class RechargeStrategy { //充值金额抽象类 public abstract decimal Recharge(decimal money); }
StrategyReturn类
/// <summary> /// 满赠类 /// </summary> public class StrategyReturn:RechargeStrategy { private decimal moneyCondition; private decimal moneyReturn; /// <summary> /// 初始化时输入的赠送条件和赠送值 /// </summary> /// <param name="moneyCondition">赠送条件</param> /// <param name="moneyReturn">赠送值</param> public StrategyReturn(decimal moneyCondition,decimal moneyReturn) { this.moneyCondition = moneyCondition; this.moneyReturn = moneyReturn; } /// <summary> /// 计算返利数值 /// </summary> /// <param name="money">充值金额</param> /// <returns></returns> public override decimal Recharge(decimal money) { decimal result = money; //若大于赠送条件则加上赠送金额 if (money>=moneyCondition) result = money +Math.Floor(money / moneyCondition) * moneyReturn; return result; } }
业务逻辑实现层
public class RechargeBLL { //实例化WCF代理服务对象 DateBaseServiecClient client = new DateBaseServiecClient(); /// <summary> /// 充值 /// </summary> /// <param name="recharge">基本信息实体</param> /// <param name="value">满赠条件</param> /// <returns></returns> public CustomerModel Recharge(string id, decimal charge,string giveInfo) { //声明一个RechargeModel对象 RechargeModel rechargeModel = new RechargeModel(); CustomerModel customer = client.SelectCustomerExist(id); PriceModel priceInfo = client.SelectPrice(); //策略模式声明一个Context对象 Strategy.Context context = null; //根据用户选择的满赠条件,进行相应的策略 switch (giveInfo ) { case "50送20": //通过构造方法传入具体的收费策略 context = new Strategy.Context(new Strategy.StrategyReturn(50, 20)); break; case "100送50": context = new Strategy.Context(new Strategy.StrategyReturn(100, 50)); break; } //根据不同的收费策略计算不同的结果 decimal money = context.ContextInterface(charge ); //通过总金额-充值金额的出赠送金额 decimal giveMoney = money - charge; //金额汇总 decimal allMoney = customer.balance + money; //修改用户表余额 client.UpdateBalance(id , allMoney); //赋值赠送金额 rechargeModel.giveMoney = giveMoney; } else { if (charge.ToString() == "") { throw new Exception("请输入充值金额"); } // 赠送金额为零 rechargeModel.giveMoney = 0; //总金额 decimal allMoney = charge + customer.balance; //修改用户表余额 client.UpdateBalance(id , allMoney); } //赋值当前操作员卡号 rechargeModel.managerID = StaticManager.managerID; //rechargeModel.managerID = recharge.managerID; //赋值充值金额 rechargeModel.charge = charge; //赋值当前充值卡号 rechargeModel.userID = id ; //赋值未结账状态 rechargeModel.checkStatus = false; //赋值当前充值日期和时间 rechargeModel.time = DateTime.Now; //向充值表中插入一条充值记录 client.InsertRecharge(rechargeModel); CustomerModel customerInfo = new CustomerModel(); //返回用户表信息 customerInfo = client.SelectCustomerExist(id ); return customerInfo; }