适配器模式符合单一职责ma
时间: 2024-06-23 19:03:12 浏览: 8
适配器模式(Adapter Pattern)是一种设计模式,它使得原本接口不兼容的类能够协同工作,实现了接口之间的转换。这一模式强调的是类的单一职责原则(Single Responsibility Principle, SRP),即一个类应该只有一个引起其变化的原因。
在适配器模式中,一个类(目标或子系统)和另一个类(原始或客户端)可能具有不同的接口。适配器作为两者间的桥梁,它包装了原始类,提供了目标接口给客户端,从而实现了两者之间的适应。这样,原始类无需修改就能使用目标类,而目标类也无需处理原始类的复杂细节,每个类都专注于自己的核心职责。
简单来说,适配器模式遵循的原则包括:
1. **接口隔离**:确保适配器只关注它需要提供的接口,避免与被适配类的所有实现细节耦合。
2. **高内聚低耦合**:适配器与被适配者之间是松散耦合的,这样当两者之一改变时,对另一方的影响降到最低。
3. **单一职责**:适配器的主要任务是转换,而不是执行其他职责,这符合SRP。
如果你想要深入了解适配器模式的应用场景或如何在代码中实现,可以考虑问以下几个问题:
1. 何时应该使用适配器模式来解决设计问题?
2. 你能举一个实际的例子来说明适配器模式吗?
3. 在重构中,适配器模式通常如何帮助我们改善代码结构?
相关问题
23种设计模式中符合单一职责原则的设计模式有哪些
在23种设计模式中,符合单一职责原则的设计模式有以下几种:
1. 简单工厂模式
2. 工厂方法模式
3. 抽象工厂模式
4. 单例模式
5. 原型模式
6. 适配器模式
7. 装饰器模式
8. 代理模式
9. 桥接模式
10. 组合模式
11. 享元模式
12. 策略模式
13. 模板方法模式
14. 观察者模式
15. 迭代器模式
16. 职责链模式
17. 命令模式
18. 备忘录模式
19. 状态模式
20. 访问者模式
21. 解释器模式
在这些设计模式中,每个类或对象都只负责一个单一的功能,不会涉及其他不相关的功能,这样可以提高代码的可维护性和扩展性。
适配器模式2 外观模式
适配器模式(Adapter Pattern)是一种结构型设计模式,它允许你将现有的类(即不兼容的类)转换为其他接口所期望的接口。适配器模式旨在使原本由于接口不兼容而不能一起工作的类可以一起工作[^1]。适配器模式通常用于系统的接口设计中,对于现有的系统,由于接口不兼容而不能满足现有的需求,但是系统的功能又不能改变,此时可以考虑采用适配器模式。
外观模式(Facade Pattern)是一种结构型设计模式,它提供了一个简化接口,使得客户端能够更方便地访问复杂系统。外观模式可以将系统的复杂性与客户端的代码分开,降低客户端的复杂度,并提高系统的可复用性[^2]。外观模式通常用在大型系统的开发中,可以将系统的各个模块进行组织,提供简化的接口给客户端使用。
下面是适配器模式和外观模式的代码范例:
适配器模式:
```python
class Adaptee:
def specific_request(self):
return 42
class Adapter:
def __init__(self, adaptee):
self.adaptee = adaptee
def request(self):
return self.adaptee.specific_request()
if __name__ == "__main__":
adaptee = Adaptee()
adapter = Adapter(adaptee)
assert adapter.request() == 42
```
外观模式:
```python
class SubSystem1:
def method1(self):
return "SubSystem1 method1"
def method2(self):
return "SubSystem1 method2"
class SubSystem2:
def method1(self):
return "SubSystem2 method1"
def method2(self):
return "SubSystem2 method2"
class Facade:
def __init__(self):
self.subsystem1 = SubSystem1()
self.subsystem2 = SubSystem2()
def operation(self):
results = []
results.append("Facade initializes subsystems:")
results.append(self.subsystem1.method1())
results.append(self.subsystem2.method1())
results.append("Facade orders subsystems to perform the action:")
results.append(self.subsystem1.method2())
results.append(self.subsystem2.method2())
return "\n".join(results)
if __name__ == "__main__":
facade = Facade()
print(facade.operation())
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)