[设计模式]行为型模式-策略模式
  25xeEEK55E62 2024年02月19日 54 0

前言

策略模式定义了一系列算法,并将每个算法封装起来,使它们可以互相替换,且算法的变换不会影响使用算法的客户。

在项目开发中,我们经常要根据不同的场景,采取不同的措施,也就是不同的策略。假设我们需要对a、b这两个整数进行计算,根据条件的不同,需要执行不同的计算方式。我们可以把所有的操作都封装在同一个函数中,然后根据if ... else ...的形式来调用不同的计算方式,这种方式称为硬编码。

在实际应用中,随着功能和体验的不断增长,我们需要经常添加/修改策略,进而需要不断修改已有代码,这不仅会让这个函数越来越难以维护,还会因为修改带来一些Bug。因此,为了解耦,我们需要使用策略模式,定义一些独立的类来封装不同的算法,每一个类封装一个具体的算法。

示例代码

策略模式的重点在于策略的设定,以及普通类Operator和策略CalStrategy的对接。通过更换实现同一接口的不同策略类。降低了Operator的维护成本,解耦算法实现。

Go

strategy.go

package strategy  
  
// CalStrategy 是一个策略类  
type CalStrategy interface {  
    do(int, int) int  
}  
  
// Add 为加法策略  
type Add struct{}  
  
func (*Add) do(a, b int) int {  
    return a + b  
}  
  
// Reduce 为减法策略  
type Reduce struct{}  
  
func (*Reduce) do(a, b int) int {  
    return a - b  
}  
  
// Operator 是具体的策略执行者  
type Operator struct {  
    strategy CalStrategy  
}  
  
// 设置策略  
func (o *Operator) setStrategy(strategy CalStrategy) {  
    o.strategy = strategy  
}  
  
// 调用策略中的方法  
func (o *Operator) calc(a, b int) int {  
    return o.strategy.do(a, b)  
}

单元测试

package strategy  
  
import "testing"  
  
func TestStrategy(t *testing.T) {  
    operator := Operator{}  
    operator.setStrategy(&Add{})  
    if operator.calc(1, 2) != 3 {  
       t.Fatal("Add strategy error")  
    }  
  
    operator.setStrategy(&Reduce{})  
    if operator.calc(2, 1) != 1 {  
       t.Fatal("Reduce strategy error")  
    }  
}

Python

from abc import ABC, abstractmethod


class CalStrategy(ABC):
    """策略类
    """
    @abstractmethod
    def do(self, a: int, b: int) -> int:
        pass


class Add(CalStrategy):
    """加法策略
    """
    def do(self, a: int, b: int) -> int:
        return a + b


class Reduce(CalStrategy):
    """减法策略
    """
    def do(self, a: int, b: int) -> int:
        return a - b


class Operator:
    """策略执行者
    """
    def __init__(self):
        self.strategy = None

    def set_strategy(self, strategy: CalStrategy):
        """设置策略
        """
        self.strategy = strategy

    def calc(self, a: int, b: int) -> int:
        """调用策略中的方法
        """
        return self.strategy.do(a, b)


if __name__ == "__main__":
    operator = Operator()
    operator.set_strategy(Add())
    print(operator.calc(1, 2))

    operator.set_strategy(Reduce())
    print(operator.calc(4, 3))

参考

  • 孔令飞 - 企业级Go项目开发实战
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年02月19日 0

暂无评论

推荐阅读
  ZkZ4n0nvAySz   2024年04月14日   43   0   0 设计模式
  bWqO7ATbLQET   2024年03月08日   74   0   0 设计模式
  ZkZ4n0nvAySz   2024年04月16日   47   0   0 设计模式
  ZkZ4n0nvAySz   2024年04月15日   66   0   0 设计模式
  bVJlYTdzny4o   2024年04月30日   52   0   0 设计模式
  ZkZ4n0nvAySz   2024年04月13日   68   0   0 设计模式
  Thv3NVnh1dDs   2024年02月19日   139   0   0 设计模式
25xeEEK55E62