模板模式与策略模式区别
时间: 2024-06-13 12:09:18 浏览: 18
模板模式和策略模式是两种常见的设计模式,它们的区别如下:
1.定义不同:模板模式定义了一个算法的骨架,将一些步骤延迟到子类中实现;策略模式定义了一系列算法,将它们封装起来,并且可以相互替换。
2.实现方式不同:模板模式使用继承来实现算法的复用;策略模式使用组合来实现算法的复用。
3.关注点不同:模板模式关注的是算法的整体流程,强调的是一致性和复用性;策略模式关注的是算法的具体实现,强调的是灵活性和可替换性。
下面是一个模板模式的例子:
```python
from abc import ABC, abstractmethod
class AbstractClass(ABC):
def template_method(self):
self.base_operation1()
self.required_operation1()
self.base_operation2()
self.hook1()
self.required_operation2()
self.base_operation3()
self.hook2()
def base_operation1(self):
print("AbstractClass says: I am doing the bulk of the work")
def base_operation2(self):
print("AbstractClass says: But I let subclasses override some operations")
def base_operation3(self):
print("AbstractClass says: But I am doing the bulk of the remaining work")
@abstractmethod
def required_operation1(self):
pass
@abstractmethod
def required_operation2(self):
pass
def hook1(self):
pass
def hook2(self):
pass
class ConcreteClass1(AbstractClass):
def required_operation1(self):
print("ConcreteClass1 says: Implemented Operation1")
def required_operation2(self):
print("ConcreteClass1 says: Implemented Operation2")
class ConcreteClass2(AbstractClass):
def required_operation1(self):
print("ConcreteClass2 says: Implemented Operation1")
def required_operation2(self):
print("ConcreteClass2 says: Implemented Operation2")
def hook1(self):
print("ConcreteClass2 says: Overridden Hook1")
```
这里定义了一个抽象类AbstractClass,其中包含了一个模板方法template_method和一些基本操作base_operation1、base_operation2、base_operation3,以及一些钩子hook1、hook2。ConcreteClass1和ConcreteClass2是AbstractClass的具体子类,它们实现了抽象方法required_operation1和required_operation2,并且可以选择性地覆盖钩子方法hook1。
下面是一个策略模式的例子:
```python
class Context:
def __init__(self, strategy):
self._strategy = strategy
def do_some_business_logic(self):
result = self._strategy.do_algorithm(['a', 'b', 'c', 'd', 'e'])
print(','.join(result))
class Strategy:
def do_algorithm(self, data):
pass
class ConcreteStrategyA(Strategy):
def do_algorithm(self, data):
return sorted(data)
class ConcreteStrategyB(Strategy):
def do_algorithm(self, data):
return reversed(sorted(data))
context = Context(ConcreteStrategyA())
context.do_some_business_logic()
context = Context(ConcreteStrategyB())
context.do_some_business_logic()
```
这里定义了一个Context类,它接受一个Strategy对象作为参数,并且在do_some_business_logic方法中调用Strategy对象的do_algorithm方法。ConcreteStrategyA和ConcreteStrategyB是Strategy的具体子类,它们实现了do_algorithm方法,分别对数据进行排序和反转排序。
--相关问题--:
1. 什么是模板方法模式?
2. 什么是策略模式?
3. 模板模式
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)