Java 策略模式(Strategy)

创建一个能够根据所传递的参数对象的不同而具有不同行为的方法

  • 要执行的算法固定不变,封装到一个类(Context)中
  • 策略就是传递进去的参数对象,它包含执行代码

  • 策略接口

/**
 * 策略接口
 */
public interface IStrategy {
    String name();

    /**
     * 具体逻辑(算法)
     * @param str
     * @return
     */
    String Arithmetic(String str);
}
  • 具体实现
public class Downcase implements IStrategy {
    public String name() {
        return getClass().getSimpleName();
    }

    public String Arithmetic(String str) {
        return str.toLowerCase();
    }
}

public class UpCase implements IStrategy {
    public String name() {
        return getClass().getSimpleName();
    }

    public String Arithmetic(String str) {
        return str.toUpperCase();
    }
}

public class Splitter implements IStrategy {
    public String name() {
        return getClass().getSimpleName();
    }

    public String Arithmetic(String str) {
        return Arrays.toString(str.split(" "));
    }
}
  • 封装逻辑(算法)
/**
 * 策略模式通过组合的方式实现具体算法
 * 其要执行的算法不变,封装到一个类(Context)中
 */
public class Context {

    private IStrategy mStrategy;

    /**
     * 将抽象接口的实现传递给组合对象
     * @param strategy
     */
    public Context(IStrategy strategy){
        this.mStrategy = strategy;
    }

    /**
     * 封装逻辑(算法)
     * @param s
     */
    public void doAction(String s){
        System.out.println(mStrategy.name());
        System.out.println(this.mStrategy.Arithmetic(s));
    }
}
  • 测试
    public static String s="Disagreement with beliefs is by definition incorrect";

    public static void main(String[] args){
        IStrategy is = new UpCase();
        Context c = new Context(is);
        c.doAction(s);

        IStrategy isd = new Downcase();
        Context c2 = new Context(isd);
        c2.doAction(s);

        IStrategy iss = new Splitter();
        Context c3 = new Context(iss);
        c3.doAction(s);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值