浅写策略模式,及map、枚举结合小例子

本文通过三个不同的示例展示了策略模式在Java中的应用。探讨了如何使用策略模式进行行为的封装,以及结合Map和枚举类型实现更优雅的代码结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



import java.util.HashMap;
import java.util.Map;

public class Test1 {

    /**
     * 我在思考 为什么要用策略模式,只是对行为做一些封装,调用 还是需要根据条件来判断选择哪种行为,经过写一段代码后有了一点小小的心得
     * 优点:行为封装 模块化,支持开闭原则,调用很简洁
     * 缺点:需要写一大堆行为实现类,并自行选择那种实现类
     * 如果结合map或者枚举能实现很优雅的code
     * 下面有利用策略模式实现的3个小例子分享给大家
     * main 常用策略模式
     * main1 策略模式与map结合后的
     * main2 策略模式与枚举结合后的
     */

    public static void main1(String[] args) {
        BookMark b1 = new BookMark(new MenberVVip());
        BookMark b2 = new BookMark(new MenberVip());
        BookMark b3 = new BookMark(new MenberNoVip());
        System.out.println("hello i want bug some book");
        System.out.println("hello i am vvip:"+b1.safe(100D,2));
        System.out.println("hello i am vip:"+b2.safe(100D,2));
        System.out.println("hello i am novip:"+b3.safe(100D,2));
    }

    public static void main2(String[] args) {
        BookMarkplus b = new BookMarkplus();
        System.out.println("hello i want bug some bookplus");
        System.out.println("hello i am vvip:"+b.safe("vvip",100D,2));
        System.out.println("hello i am vip:"+b.safe("vip",100D,2));
        System.out.println("hello i am novip:"+b.safe("novip",100D,2));
    }

    public static void main(String[] args) {
        System.out.println("hello i want bug some bookplus");
        System.out.println("hello i am vvip:"+SafeBookEnum.getEnumByType("vvip").safeBook.safe(100D,2));
        System.out.println("hello i am vip:"+SafeBookEnum.getEnumByType("vip").safeBook.safe(100D,2));
        System.out.println("hello i am novip:"+SafeBookEnum.getEnumByType("novip").safeBook.safe(100D,2));
    }
}

class BookMark{
    SafeBook safeBook ;
    BookMark(){}
    BookMark(SafeBook safeBook){
        this.safeBook = safeBook ;
    }
    public Double safe(Double price, Integer count){
        return  this.safeBook.safe(price,count);
    }
}


class BookMarkplus{

    public static Map<String,SafeBook> c = new HashMap<>();
    static {
        c.put("vvip",new MenberVVip());
        c.put("vip",new MenberVip());
        c.put("novip",new MenberNoVip());
    }

    public Double safe( String type, Double price, Integer count){
        return  c.get(type).safe(price,count) ;
    }
}

class BookMarkplusplus{

    public static Map<String,SafeBook> c = new HashMap<>();
    static {
        c.put("vvip",new MenberVVip());
        c.put("vip",new MenberVip());
        c.put("novip",new MenberNoVip());
    }

    public Double safe( String type, Double price, Integer count){
        return  c.get(type).safe(price,count) ;
    }
}


abstract  class SafeBook{
    abstract Double safe(Double price ,Integer count);
}

    class MenberVVip extends  SafeBook {

        @Override
        Double safe(Double price, Integer count) {
            return price*count*0.8;
    }
   }

    class MenberVip extends  SafeBook {

        @Override
        Double safe(Double price, Integer count) {
            return price*count*0.9;
        }
    }
    class MenberNoVip extends  SafeBook {

        @Override
        Double safe(Double price, Integer count) {
            return price*count;
        }
    }

    //枚举类的处理

enum SafeBookEnum{
  VVIP("vvip",new MenberVVip()),
  VIP("vip",new MenberVip()),
  NOVIP("novip",new MenberNoVip()) ;

  public String menberType ;

  public SafeBook  safeBook ;

   SafeBookEnum(String menberType ,SafeBook safeBook){
      this.menberType = menberType ;
      this.safeBook = safeBook ;
  }

    public String getMenberType() {
        return menberType;
    }

    public void setMenberType(String menberType) {
        this.menberType = menberType;
    }

    public SafeBook getSafeBook() {
        return safeBook;
    }

    public void setSafeBook(SafeBook safeBook) {
        this.safeBook = safeBook;
    }

    public static SafeBookEnum getEnumByType(String type){
        for(SafeBookEnum entity : SafeBookEnum.values()){
            if(type.equals(entity.getMenberType())){
                return entity;
            }
        }
        return null;
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值