策略模式+工厂模式

  • Post author:
  • Post category:其他




策略模式

策略模式(Strategy):它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。



场景

商场不同时间段活动。正常,打折,满减,等等



CashSuper

package com.healer.strategy;

import com.healer.domain.Goods;

import java.util.List;

/**

  • @author tianzx


    /

    public interface CashSuper{


    /

    *

    *收款

    • @return
    • @param goodsList 商品列表

      */

      int acceptCash(List goodsList);

      }



CashNormal

package com.healer.strategy;

import com.healer.domain.Goods;

import java.util.List;

/**正常

  • @author tianzx

    */

    public class CashNormal implements CashSuper {


    private String strategyName;

    public CashNormal(String strategyName) {


    this.strategyName = strategyName;

    }

    @Override

    public int acceptCash(List goodsList) {


    System.out.println(“今天活动:”+strategyName);

    int sum=0;

    /

    打印商品

    /

    goodsList.forEach(goods->System.out.println(goods.getGoodsName()+”—-“+goods.getGoodsNumber()+”—-“+goods.getGoodsPrice()));

    for (Goods goods:goodsList){


    sum+=goods.getGoodsNumber()*goods.getGoodsPrice();

    }

     return sum;
    

    }

    }



CashRebate

package com.healer.strategy;

import com.healer.domain.Goods;

import java.util.List;

/**打八折

  • @author tianzx

    */

    public class CashRebate implements CashSuper {


    private String strategyName;

    private int discount;

    public CashRebate(String strategyName, int discount) {


    this.strategyName = strategyName;

    this.discount = discount;

    }

    @Override

    public int acceptCash(List goodsList) {


    System.out.println(“今天活动:”+strategyName);

    int sum = 0;

    /

    打印商品

    /

    goodsList.forEach(goods -> System.out.println(goods.getGoodsName() + “—-” + goods.getGoodsNumber() + “—-” + goods.getGoodsPrice()));

    for (Goods goods : goodsList) {


    sum += goods.getGoodsNumber() * goods.getGoodsPrice();

    }

    return sum*discount/10;

    }

    }



CashReturn

package com.healer.strategy;

import com.healer.domain.Goods;

import java.util.List;

/**满多少减多少

  • @author tianzx

    */

    public class CashReturn implements CashSuper {


    private String strategyName;

    private int full;

    private int cashReturn;

    public CashReturn(String strategyName,int full, int cashReturn) {


    this.strategyName=strategyName;

    this.full = full;

    this.cashReturn = cashReturn;

    }

    @Override

    public int acceptCash(List goodsList) {


    System.out.println(“今天活动:”+strategyName);

    int sum = 0;

    /

    打印商品

    /

    goodsList.forEach(goods -> System.out.println(goods.getGoodsName() + “—-” + goods.getGoodsNumber() + “—-” + goods.getGoodsPrice()));

    for (Goods goods : goodsList) {


    sum += goods.getGoodsNumber() * goods.getGoodsPrice();

    }

    if (sum>full){


    sum=sum-cashReturn;

    }

    return sum;

    }

    }



CashContext

package com.healer.strategy;

import com.healer.domain.Goods;

import java.util.List;

/**收银逻辑

  • @author tianzx
  • @Date

    */

    public class CashContext {


    private CashSuper cashSuper;

    private List goodsList;

    public CashContext(CashSuper cashSuper,List goodsList){


    this.cashSuper=cashSuper;

    this.goodsList=goodsList;

    }

    public int getAcceptResult() {


    return cashSuper.acceptCash(goodsList);

    }

}

package com.healer.factory;



CashFactory

import com.healer.domain.Goods;

import com.healer.strategy.CashContext;

import com.healer.strategy.CashNormal;

import com.healer.strategy.CashRebate;

import com.healer.strategy.CashReturn;

import java.util.List;

/**策略工厂

  • @author hspcadmin

    */

    public class CashFactory {


    public static void cashContext(String strategyName, List goodsList){


    switch (strategyName){


    case “无”:new CashContext(new CashNormal(strategyName),goodsList).getAcceptResult();

    break;

    case “满10000元减800元”:new CashContext(new CashReturn(strategyName,10000,800),goodsList).getAcceptResult();

    break;

    case “打八折”:new CashContext(new CashRebate(strategyName,8),goodsList).getAcceptResult();

    break;

    default:

    System.out.println(“今日没有此活动”);

    }

    }

    }



App



测试类

package com.healer;

import com.healer.domain.Goods;

import com.healer.factory.CashFactory;

import java.util.ArrayList;

import java.util.List;

/**

  • Hello world!
  • @author hspcadmin

    */

    public class App {


    public static void main(String[] args) {


    System.out.println(“Hello World!”);

    Goods goods1=new Goods(1,“电脑”,2,20000);

    Goods goods2=new Goods(2,“手机”,2,4000);

    Goods goods3=new Goods(3,“平板”,2,2000);

    Goods goods4=new Goods(4,“显示器”,2,1000);

    Goods goods5=new Goods(5,“主机”,2,6000);

    Goods goods6=new Goods(6,“耳机”,2,1000);

    List goodsList=new ArrayList<>();

    goodsList.add(goods1);

    goodsList.add(goods2);

    goodsList.add(goods3);

    goodsList.add(goods4);

    goodsList.add(goods5);

    goodsList.add(goods6);

    CashFactory.cashContext(“无”,goodsList);

    CashFactory.cashContext(“满10000元减800元”,goodsList);

    CashFactory.cashContext(“打八折”,goodsList);

    CashFactory.cashContext(“打七折”,goodsList);

    }

    }



版权声明:本文为qq_36436040原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。