设计模式 - 策略模式

文章介绍了策略模式的概念,它用于封装一系列算法并允许在运行时选择具体算法。通过通用类图展示了如何创建策略接口和两个具体的策略类(加法和减法)。接着,文章提供了一个枚举策略的实现,将加法和减法操作作为枚举常量,简化了策略的选择过程。最后,给出了客户端代码示例,展示了如何在实际应用中使用这些策略。

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

设计模式 - 策略模式

       策略模式 (也叫做政策模式):定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。使用策略模式后,可以由其他模块决定采用何种策略,策略家族对外提供的访问接口就是封装类,简化了操作,同时避免了条件语句判断。平时使用时,一般用工厂方法模式来实现策略类的声明。
 

通用类图

 
在这里插入图片描述 

通用示例

 

/**
 * 策略接口类
 */
public interface IStrategy {
    public int deal(Integer a, Integer b);
}

/**
 * 减法策略
 */
public class SubStrategy implements IStrategy {
    @Override
    public int deal(Integer a, Integer b) {
        return a - b;
    }
}

/**
 * 加法策略
 */
public class AddStrategy implements IStrategy {
    @Override
    public int deal(Integer a, Integer b) {
        return a + b;
    }
}

/**
 * 策略执行者
 */
public class Context {

    private IStrategy strategy;

    public Context(IStrategy strategy) { this.strategy = strategy;}

    public Integer exec(Integer a, Integer b) {
        return strategy == null ? null : strategy.deal(a, b);
    }
}

public class StrategyClient {

    public static void main(String[] args) {
        IStrategy strategy = null;
        String operateType = "add";
        if ("add".equals(operateType)) {
            System.out.println("operate = add");
            strategy = new AddStrategy();
        } else if ("sub".equals(operateType)) {
            System.out.println("operate = sub");
            strategy = new SubStrategy();
        } else {
            System.out.println("operate not verify");
        }
        Context context = new Context(strategy);
        Integer result = context.exec(12, 4);
        System.out.println("result = " + result);
    }
}

/************ 打印结果: *****************/
operate = add
result = 16

 

枚举策略

 

public enum CalculateEnum {

    ADD("+") {
        public int exec(int a, int b) {
            return a + b;
        }
    },
    SUB("-") {
        public int exec(int a, int b) {
            return a - b;
        }
    };

    private String value;

    public String getValue() {
        return this.value;
    }

    CalculateEnum(String value) {
        this.value = value;
    }
    public abstract int exec(int a, int b);

}


public class CalculateClient {
    
    public static void main(String[] args) {
        String operateType = "+";
        if (operateType.equals(CalculateEnum.ADD.getValue())) {
            System.out.println("operateType = " + CalculateEnum.ADD.toString());
            System.out.println("CalculateEnum.ADD.exec = " + CalculateEnum.ADD.exec(12, 13));
        } else if (operateType.equals(CalculateEnum.SUB.getValue())) {
            System.out.println("operateType = " + CalculateEnum.SUB.toString());
            System.out.println("CalculateEnum.SUB.exec = " + CalculateEnum.SUB.exec(12, 13));
        } else {
            System.out.println("operateType not verify");
        }
    }

}

/************ 打印结果: *****************/
operateType = ADD
CalculateEnum.ADD.exec = 25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值