设计模式-策略模式

本文介绍了策略模式的概念及其在缓存淘汰策略中的应用。通过创建不同的淘汰策略接口实现,如淘汰最后、淘汰最不经常使用和淘汰最近最少使用的策略,实现了动态切换缓存淘汰算法。同时,使用枚举类型和工厂方法简化了策略的选择和实例化,提高了代码的可读性和扩展性。
摘要由CSDN通过智能技术生成

设计模式-策略模式

本文我们简单说下设计模式中的策略模式。

策略模式概述

在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。

在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

策略模式优缺点

优点

  1. 算法可以自由切换。
  2. 避免使用多重条件判断。
  3. 扩展性良好。

缺点

  1. 策略类会增多。
  2. 所有策略类都需要对外暴露。

应用场景

  1. 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  2. 一个系统需要动态地在几种算法中选择一种。
  3. 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。

策略模式结构

在这里插入图片描述

代码实现

talk is cheap show me the code。废话少说,直接上代码。

package com.itender.design.strategy;

/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 21:30
 * @Description: 淘汰策略
 */
public interface EvictionStrategy {
    /**
     * 淘汰方法
     */
    void evictionCache();
}

package com.itender.design.strategy;


import lombok.extern.slf4j.Slf4j;

/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 21:33
 * @Description: 淘汰最后
 */
public class LastEvictionStrategy implements EvictionStrategy {

    @Override
    public void evictionCache() {
        System.out.println("淘汰最后一名!");
    }
}
package com.itender.design.strategy;


/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 21:33
 * @Description: 淘汰最不经常使用的
 */
public class LfuEvictionStrategy implements EvictionStrategy {

    @Override
    public void evictionCache() {
        System.out.println("淘汰最不经常使用的!");
    }
}
package com.itender.design.strategy;


/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 21:33
 * @Description: 淘汰最近最少使用的
 */
public class LruEvictionStrategy implements EvictionStrategy {

    @Override
    public void evictionCache() {
        System.out.println("淘汰最近最少使用的!");
    }
}
package com.itender.design.strategy;

/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 21:47
 * @Description: 淘汰类型枚举
 */
public enum EvictionType {
    LAST("last","淘汰最后一名"),
    LRU("lru","淘汰最近最少使用"),
    LFU("lfu", "淘汰最不经常使用的");

    EvictionType(String code, String name) {
        this.code = code;
        this.name = name;
    }

    private String code;

    private String name;

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public EvictionType evictionType(String code) {
        if (LAST.code.equals(code)) {
            return LAST;
        }else if (LRU.code.equals(code)) {
            return LRU;
        }else if (LFU.code.equals(code)) {
            return LFU;
        }
        return null;
    }
}
package com.itender.design.strategy;

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

import static com.itender.design.strategy.EvictionType.*;

/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 21:46
 * @Description: 策略工厂
 */
public class StrategyFactory {
    public static Map<EvictionType, EvictionStrategy> evictionMap = new HashMap<>();

    static {
        evictionMap.put(LAST, new LastEvictionStrategy());
        evictionMap.put(LRU, new LruEvictionStrategy());
        evictionMap.put(LFU, new LfuEvictionStrategy());
    }

    public static EvictionStrategy getEvictionStrategy(EvictionType evictionType) {
        return evictionMap.get(evictionType);
    }
}

测试代码

package com.itender.design.strategy;

/**
 * @Author: ITender
 * @CreateTime: 2022-02-19 22:00
 * @Description: 策略模式测试类
 */
public class StrategyTest {
    public static void main(String[] args) {
        EvictionStrategy lastStrategy = StrategyFactory.getEvictionStrategy(EvictionType.LAST);
        lastStrategy.evictionCache();

        EvictionStrategy lruStrategy = StrategyFactory.getEvictionStrategy(EvictionType.LRU);
        lruStrategy.evictionCache();

        EvictionStrategy lfuStrategy = StrategyFactory.getEvictionStrategy(EvictionType.LFU);
        lfuStrategy.evictionCache();
    }
}

输出

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值