设计模式-策略模式

声明: 此文章仅限于记录学习之用 , 受限于自身水平和理解能力 , 因此结论可能是不正确的. 如果您需要学习,建议参考其他文章

介绍

策略模式就是将同一行为的算法分开写, 用于消除if else. 比如 旅行的出游方式,选择骑自行车、坐汽车.

优点

符合单一职责, 缺点就是会类变得很多.

使用场景

策略模式定义了一系列的算法,并将每一个算法封装起来,使他们可以相互替换。

类图

在这里插入图片描述

示例代码

  1. 定义出行接口.
package com.demo;

/**
* @author Administrator
* @version 1.0
* @description:  出行方式
* @date 2023/11/11 19:29
*/
public interface CalculateStrategy {

   int  amount(int distance);
}
  1. 使用打的, 骑自行车, 坐公交车实现具体逻辑 /**
package com.demo;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2023/11/11 19:32
 */
public class ByBus implements CalculateStrategy {
    @Override
    public int amount(int distance) {
        return distance < 10 ? 4 : 6;

    }
}


package com.demo;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2023/11/11 19:32
 */
public class Didi  implements CalculateStrategy{
    @Override
    public int amount(int distance) {
        return distance<3?8:(8+(distance-3)*3);
    }
}

package com.demo;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2023/11/11 19:33
 */
public class BySharedBicycle implements CalculateStrategy {
    @Override
    public int amount(int distance) {
        return 2;
    }
}




  1. 定义使用算法 接口.


package com.demo;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2023/11/11 19:34
 */
public class TrafficFeeCalculator  {
    public int goToTianJinEye(CalculateStrategy strategy,int distance){
        return strategy.amount(distance);
    }
}

  1. 测试
package com.demo;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2023/11/11 19:33
 */
public class Main {

    public static void main(String[] args) {

        TrafficFeeCalculator trafficFeeCalculator = new TrafficFeeCalculator();
        int i = trafficFeeCalculator.goToTianJinEye(new Didi(), 20);
        System.out.println("didi = " + i);

        int bus = trafficFeeCalculator.goToTianJinEye(new ByBus(), 20);
        System.out.println("ByBus = " + bus);

        int bike = trafficFeeCalculator.goToTianJinEye(new BySharedBicycle(), 20);
        System.out.println("bike = " + bike);
    }
}

参考资料

https://www.runoob.com/design-pattern/strategy-pattern.html
https://shusheng007.top/2020/02/16/strategy-pattern/
https://refactoringguru.cn/design-patterns/strategy
<<header first 设计模式>>

学习后记 20240204

近期又看了<<Header first 设计模式>> 发现我以前的理解是片面的. 策略模式本身解决的问题是 行为算法可变的问题. 它是通过定义一个类,将不变的写入超类. 将可变的行为定义成接口并实现. 然后将接口注入到超类中, 由超类的子类做初始化. 如果使用中想要修改子类的行为算法, 直接去通过set来动态改变行为调用. 这可能才是此设计模式的真正意义. 同样,这个算法也教会了我 组合优于继承. 最后还是建议大家,尽量获取源头知识,我们要学习的是作者的解决问题的方式,而不是记UML类图.是理解而不是记忆,这是最重要的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值