[设计模式]行为型模式-策略模式

前言

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

在项目开发中,我们经常要根据不同的场景,采取不同的措施,也就是不同的策略。假设我们需要对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项目开发实战
posted @ 2024-02-18 21:20  花酒锄作田  阅读(52)  评论(0编辑  收藏  举报