设计模式学习笔记一 简单工厂策略模式实现销售策略的变更(一)

来源:互联网 发布:淘宝卖家中心不见了 编辑:程序博客网 时间:2024/05/02 20:20

第一次写博客 抓狂贊一个

需求:

 实现商店收银功能 根据顾客的订单 计算购物金额 而且要根据现实的情况 改变销售策略(如促销时:商品实行打折优惠  购满一定金额返现 )

要求:

 实现需求 而且能够根据现实情况 改变销售策略

解题分析:

销售策略是随着现实的情况改变而改变 如果按普通实现方式 每次需求改变时我都要重新编写实现代码 这就大量代码的重复 后期维护繁琐(商店促销活动是最邪恶的鄙视) 有什么解决方法吗  有木有 有木有   ---------设计模式:简单工厂 和 策略模式 



准备阶段

既然是根据客户的的订单来收费 好 第一步建订单类:

public class OrderItem
{

private int num;//购买商品的数量 

private double price;//购买商品的单价

private double saleVal = 1.00;//折扣默认为1.00


private OrderItemType orderItemType = OrderItemType.Normal;//该订单的类型OrderItemType.Normal为正常订单 OrderItemType.Sale为打折订单 

/*
*无参的构造方法  
*/
public OrderItem()
{

}

/*
* 该方法是给打折类进行调用

*/
public OrderItem(int num,double price,double saleVal,OrderItemType orderItemType)
{
this.num = num;
this.price = price;
this.saleVal = saleVal;
this.orderItemType = orderItemType;
}

/*
* 该方法是给未打折类进行调用

*/
public OrderItem(int num,double price)
{
this.num = num;
this.price = price;
}
public int getNum() {
return num;
}


public void setNum(int num) {
this.num = num;
}


public double getPrice() {
return price;
}


public void setPrice(double price) {
this.price = price;
}


public double getSaleVal() {
return saleVal;
}


public void setSaleVal(double saleVal) {
this.saleVal = saleVal;
}

public OrderItemType getOrderItemType()
{
return orderItemType;
}


public void setOrderItemType(OrderItemType orderItemType) 
{
this.orderItemType = orderItemType;
}

}

    -----------------------------------------------------------------------------------

  /*
 * 该类用来判断订单类型
 * Normal = 正常收费
 * Sale = 打折收费
 */

   public enum OrderItemType
{

Normal,Sale;
}       



       

原创粉丝点击