策略模式:灵活切换算法的设计之道

策略模式的定义与核心思想

策略模式是一种行为设计模式,允许在运行时选择算法的行为。它将算法封装成独立的类,使得它们可以互相替换,而不影响客户端代码。策略模式的核心在于将算法的使用与实现分离,提升代码的灵活性和可维护性。

策略模式的结构

策略模式通常包含以下角色:

  • Context(上下文):维护一个对策略对象的引用,负责将客户端请求委托给具体策略。
  • Strategy(策略接口):定义所有支持的算法或行为的公共接口。
  • ConcreteStrategy(具体策略):实现策略接口,提供具体的算法实现。
// 策略接口
public interface PaymentStrategy {
    void pay(int amount);
}

// 具体策略:信用卡支付
public class CreditCardPayment implements PaymentStrategy {
    private String cardNumber;
    private String cvv;

    public CreditCardPayment(String cardNumber, String cvv) {
        this.cardNumber = cardNumber;
        this.cvv = cvv;
    }

    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " via Credit Card");
    }
}

// 具体策略:支付宝支付
public class AlipayPayment implements PaymentStrategy {
    private String email;

    public AlipayPayment(String email) {
        this.email = email;
    }

    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " via Alipay");
    }
}

// 上下文类
public class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void checkout(int amount) {
        paymentStrategy.pay(amount);
    }
}

策略模式的优点

  • 开闭原则:新增策略无需修改现有代码,只需扩展新的策略类。
  • 消除条件分支:避免使用冗长的if-elseswitch-case结构来选择算法。
  • 可复用性:策略类可以在多个上下文中复用,减少代码冗余。

策略模式的应用场景

  • 当需要动态切换算法或行为时,例如支付方式、排序算法、压缩策略等。
  • 当多个类仅在算法行为上有所不同,策略模式可以避免代码重复。
  • 当希望隐藏算法实现细节,仅暴露接口给客户端。

策略模式与状态模式的区别

策略模式和状态模式在结构上相似,但意图不同:

  • 策略模式:客户端主动选择策略,策略之间通常无依赖关系。
  • 状态模式:状态转换由上下文或状态类自身控制,状态之间可能存在关联。

实际案例:Java 中的 Comparator

Java 的Collections.sort()方法允许传入自定义的Comparator,这正是策略模式的典型应用:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

// 按字母顺序排序(策略1)
Collections.sort(names, (a, b) -> a.compareTo(b));

// 按字符串长度排序(策略2)
Collections.sort(names, (a, b) -> a.length() - b.length());

总结

策略模式通过将算法封装为独立对象,提供了一种灵活的方式来管理行为变化。它不仅提升了代码的可扩展性,还降低了耦合度,是面向对象设计中常用的模式之一。

5G.okacbd121.asia/PoSt/1123_621658.HtM
5G.okacbd122.asia/PoSt/1123_750386.HtM
5G.okacbd123.asia/PoSt/1123_056938.HtM
5G.okacbd124.asia/PoSt/1123_105849.HtM
5G.okacbd125.asia/PoSt/1123_867448.HtM
5G.okacbd126.asia/PoSt/1123_343164.HtM
5G.okacbd127.asia/PoSt/1123_872178.HtM
5G.okacbd128.asia/PoSt/1123_225962.HtM
5G.okacbd129.asia/PoSt/1123_097940.HtM
5G.okacbd130.asia/PoSt/1123_587083.HtM
5G.okacbd121.asia/PoSt/1123_805255.HtM
5G.okacbd122.asia/PoSt/1123_940847.HtM
5G.okacbd123.asia/PoSt/1123_349306.HtM
5G.okacbd124.asia/PoSt/1123_977726.HtM
5G.okacbd125.asia/PoSt/1123_405961.HtM
5G.okacbd126.asia/PoSt/1123_309758.HtM
5G.okacbd127.asia/PoSt/1123_858457.HtM
5G.okacbd128.asia/PoSt/1123_158672.HtM
5G.okacbd129.asia/PoSt/1123_877529.HtM
5G.okacbd130.asia/PoSt/1123_671120.HtM
5G.okacbd131.asia/PoSt/1123_842076.HtM
5G.okacbd132.asia/PoSt/1123_654204.HtM
5G.okacbd133.asia/PoSt/1123_399816.HtM
5G.okacbd134.asia/PoSt/1123_747687.HtM
5G.okacbd135.asia/PoSt/1123_292027.HtM
5G.okacbd136.asia/PoSt/1123_235053.HtM
5G.okacbd137.asia/PoSt/1123_728121.HtM
5G.okacbd138.asia/PoSt/1123_563632.HtM
5G.okacbd139.asia/PoSt/1123_052754.HtM
5G.okacbd140.asia/PoSt/1123_431133.HtM
5G.okacbd131.asia/PoSt/1123_195857.HtM
5G.okacbd132.asia/PoSt/1123_257208.HtM
5G.okacbd133.asia/PoSt/1123_768948.HtM
5G.okacbd134.asia/PoSt/1123_055403.HtM
5G.okacbd135.asia/PoSt/1123_995711.HtM
5G.okacbd136.asia/PoSt/1123_899777.HtM
5G.okacbd137.asia/PoSt/1123_231839.HtM
5G.okacbd138.asia/PoSt/1123_140847.HtM
5G.okacbd139.asia/PoSt/1123_260332.HtM
5G.okacbd140.asia/PoSt/1123_066296.HtM
5G.okacbd131.asia/PoSt/1123_705443.HtM
5G.okacbd132.asia/PoSt/1123_267306.HtM
5G.okacbd133.asia/PoSt/1123_795436.HtM
5G.okacbd134.asia/PoSt/1123_259831.HtM
5G.okacbd135.asia/PoSt/1123_667680.HtM
5G.okacbd136.asia/PoSt/1123_435487.HtM
5G.okacbd137.asia/PoSt/1123_845965.HtM
5G.okacbd138.asia/PoSt/1123_922134.HtM
5G.okacbd139.asia/PoSt/1123_454445.HtM
5G.okacbd140.asia/PoSt/1123_154394.HtM
5G.okacbd131.asia/PoSt/1123_710294.HtM
5G.okacbd132.asia/PoSt/1123_336975.HtM
5G.okacbd133.asia/PoSt/1123_704740.HtM
5G.okacbd134.asia/PoSt/1123_535268.HtM
5G.okacbd135.asia/PoSt/1123_935064.HtM
5G.okacbd136.asia/PoSt/1123_118838.HtM
5G.okacbd137.asia/PoSt/1123_428446.HtM
5G.okacbd138.asia/PoSt/1123_525585.HtM
5G.okacbd139.asia/PoSt/1123_332404.HtM
5G.okacbd140.asia/PoSt/1123_490973.HtM
5G.okacbd131.asia/PoSt/1123_110779.HtM
5G.okacbd132.asia/PoSt/1123_939752.HtM
5G.okacbd133.asia/PoSt/1123_897239.HtM
5G.okacbd134.asia/PoSt/1123_616375.HtM
5G.okacbd135.asia/PoSt/1123_688537.HtM
5G.okacbd136.asia/PoSt/1123_306727.HtM
5G.okacbd137.asia/PoSt/1123_479342.HtM
5G.okacbd138.asia/PoSt/1123_081585.HtM
5G.okacbd139.asia/PoSt/1123_577765.HtM
5G.okacbd140.asia/PoSt/1123_659644.HtM
5G.okacbd131.asia/PoSt/1123_352561.HtM
5G.okacbd132.asia/PoSt/1123_099640.HtM
5G.okacbd133.asia/PoSt/1123_107708.HtM
5G.okacbd134.asia/PoSt/1123_673871.HtM
5G.okacbd135.asia/PoSt/1123_908595.HtM
5G.okacbd136.asia/PoSt/1123_377732.HtM
5G.okacbd137.asia/PoSt/1123_606319.HtM
5G.okacbd138.asia/PoSt/1123_006289.HtM
5G.okacbd139.asia/PoSt/1123_315043.HtM
5G.okacbd140.asia/PoSt/1123_519666.HtM

#牛客AI配图神器#

全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务