设计模式十:策略模式(Strategy Pattern)

目录

0、相关文章:

1、详解

1.1、概念

1.2、使用场景

1.3、UML结构图分析

1.4、实际代码分析

1.4.1、传统写法:

1.4.2、策略模式写法:

1.5、优点

2、在实际开发中的运用


0、相关文章:

Android的设计模式-策略模式

Android 中的那些策略模式(阅读量6376)

1、详解

1.1、概念

定义一系列的算法,把它们一个个封装起来,并且使它们可以互相替换。本模式使得算法可独立于
使用它的客户而变化。

1.2、使用场景

一个类定义了多种行为,并且这些行为在这个类的方法中以多个条件语句的形式出现,那么可以
使用策略模式避免在类中使用大量的条件语句。

1.3、UML结构图分析

可以看到,策略模式中主要有以下几个角色:

  • Strategy 接口,用于定义算法的固定套路
  • ConcreteStrategyA , …..B , 等具体算法实现类
  • Context 外部调用类

 Context 中引用的是 接口,因此当更换具体实现时,Context 不用修改代码,这就是针对接口编程的好处。

1.4、实际代码分析

1.4.1、传统写法:

/**
 * 传统写法,如果再增加一个出租车选项,代码还要再写一份,逻辑判断还要在增加一种判断
 */
public class PriceCalculator {
    private static final int BUS = 1;
    private static final int SUBWAY = 2;

    public static void main(String[] args) {
        PriceCalculator calculator = new PriceCalculator();
        System.out.println("坐公交车20KM价格:" + calculator.calculatePrice(20, BUS));
        System.out.println("坐地铁20KM价格:" + calculator.calculatePrice(20, SUBWAY));
    }

    /**
     * 公交车计价:10公里之内1块钱,超过十公里每加1块可坐5公里
     */
    private int busPrice(int km) {
        //超过十公里的距离
        int extraTotal = km - 10;
        //超过距离是5公里的倍数
        int extraFactor = extraTotal / 5;
        //超过的距离对5公里取余
        int fraction = extraFactor % 5;
        //价格计算
        int price = 1 + extraFactor % 5;
        return fraction > 0 ? ++price : price;
    }

    /**
     * 地铁计价:6公里内3块,6-12公里4块,12-22公里5块,22-32公里6块,其他距离7块
     */
    private int subwayPrice(int km) {
        if (km <= 6) {
            return 3;
        } else if (km > 6 && km < 12) {
            return 4;
        } else if (km > 12 && km < 22) {
            return 5;
        } else if (km > 22 && km < 32) {
            return 6;
        }
        return 7;
    }

    /**
     * 根据不同类型计算
     */
    int calculatePrice(int km, int type) {
        if (type == BUS) {
            return busPrice(km);
        } else if (type == SUBWAY) {
            return subwayPrice(km);
        }
        return 0;
    }

}

1.4.2、策略模式写法:

public interface AbstractStrategy {
    //按距离来计算价格
    int calculatePrice(int km);
}

/**
 * 公交车计价:10公里之内1块钱,超过十公里每加1块可坐5公里
 */
public class BusStrategy implements AbstractStrategy {
    @Override
    public int calculatePrice(int km) {
        //超过十公里的距离
        int extraTotal = km - 10;
        //超过距离是5公里的倍数
        int extraFactor = extraTotal / 5;
        //超过的距离对5公里取余
        int fraction = extraFactor % 5;
        //价格计算
        int price = 1 + extraFactor % 5;
        return fraction > 0 ? ++price : price;
    }
}

/**
 * 地铁计价:6公里内3块,6-12公里4块,12-22公里5块,22-32公里6块,其他距离7块
 */
public class SubwayStrategy implements AbstractStrategy {
    @Override
    public int calculatePrice(int km) {
        if (km <= 6) {
            return 3;
        } else if (km > 6 && km < 12) {
            return 4;
        } else if (km > 12 && km < 22) {
            return 5;
        } else if (km > 22 && km < 32) {
            return 6;
        }
        return 7;
    }
}

public class TaxiStrategy implements AbstractStrategy {
    @Override
    public int calculatePrice(int km) {
        return km * 2;
    }
}

public class Context {
    private AbstractStrategy strategy;

    public void setStrategy(AbstractStrategy strategy) {
        this.strategy = strategy;
    }

    public int calculatePrice(int km) {
        return strategy.calculatePrice(km);
    }

    public static void main(String[] args) {
        Context calculator = new Context();
//        calculator.setStrategy(new BusStrategy());
//        calculator.setStrategy(new TaxiStrategy());
        calculator.setStrategy(new SubwayStrategy());
        System.out.println("价格:" + calculator.calculatePrice(20));
    }

}

1.5、优点

  • a、上下文(Context)和具体策略(ConcreteStrategy)是松耦合关系
  • b、策略模式满足“开-闭原则”

2、在实际开发中的运用

  • 1、第三方登录模块可采用策略模式(QQ、微信、微博、Google、Facebook等);
  • 2、人工智能识别路面损害(引用不同的模型文件:PB文件、Tflite文件);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值