Strategy Pattern
策略模式在运行时动态地装配算法行为到对象中。
我们可以定义算法,封装它们,动态地切换它们。
实现
1 | type Operator interface{ |
定义具体的对象
1 | type Addition struct{} |
使用
1 | func main() { |
注意
- 策略模式可以让更换对象的内脏,而装饰者模式可以更换对象的外表。
策略模式在运行时动态地装配算法行为到对象中。
我们可以定义算法,封装它们,动态地切换它们。
1 | type Operator interface{ |
定义具体的对象
1 | type Addition struct{} |
使用
1 | func main() { |