首页  >  文章  >  后端开发  >  如何在 Python 中选择正确的设计模式(附示例)

如何在 Python 中选择正确的设计模式(附示例)

Linda Hamilton
Linda Hamilton原创
2024-10-24 06:12:02344浏览

Comment choisir le bon design pattern en Python, avec des exemples

设计模式是软件开发中常见问题的经过验证的解决方案。它们为解决设计问题提供了可重用的模板,从而提高了代码的可维护性和灵活性。

但是有这么多可用的设计模式,您如何知道针对给定问题在 Python 中实现哪一种?在本文中,我们将探讨选择正确设计模式的步骤,并提供每个示例,以帮助您理解和有效应用它们。

1. 理解问题

选择设计模式的第一步是清楚地了解您要解决的问题。问自己以下问题:

预期的行为是什么?
系统有哪些限制?
可能的延伸或变化点有哪些?

2.设计模式分类

设计模式一般分为三类:

创造:关注对象的创建。
结构:关注对象的组成。
行为:关注对象之间的交互。
确定与您的问题匹配的类别有助于缩小相关模式的数量。

3.选择合适的设计模式

了解问题及其类别后,查看该类别中的设计模式以找到最适合您情况的设计模式。考虑以下几点:

灵活性:该模式是否提供必要的灵活性?
复杂性:这不会带来不必要的复杂性吗?
可扩展性:它是否使未来的扩展更容易?

  1. Python 中的设计模式示例 辛格尔顿 什么时候使用它? 当您需要确保某个类只有一个实例并提供对该实例的全局访问点时。

Python 示例:
`class SingletonMeta(类型):
_instance = {}

def __call__(cls, *args, **kwargs):
    if cls not in cls._instance:
        cls._instance[cls] = super().__call__(*args, **kwargs)
    return cls._instance[cls]

类 Logger(metaclass=SingletonMeta):
def log(自我, 消息):
print(f"[LOG]: {消息}")

使用

logger1 = Logger()
logger2 = Logger()

print(logger1 is logger2) # 输出:True

logger1.log(“运行中的单例模式。”)
`
为什么它有效?
SingletonMeta 是一个控制 Logger 实例创建的元类。如果实例已存在,则返回该实例,确保只有一个实例。

工厂
什么时候使用它?
当您有一个包含多个子类的父类,并且根据输入数据,您需要返回其中一个子类。

Python 示例:
`类形状:
def 绘制(自身):
通过

类圆形(形状):
def 绘制(自身):
print("画一个圆。")

类正方形(形状):
def 绘制(自身):
print("画一个正方形。")

def shape_factory(shape_type):
如果 shape_type == "圆":
返回圆()
elif shape_type == "方形":
返回 Square()
其他:
raise ValueError("未知的形状类型。")

使用

shape = shape_factory("圆")
shape.draw() # 输出:画一个圆。
`
为什么它有效?
工厂封装了对象创建逻辑,允许在不暴露底层逻辑的情况下创建实例。

观察
什么时候使用它?
当你有一个对象(主体)需要在状态发生变化时通知多个其他对象(观察者)。

Python 示例:
`课程主题:
def init(self):
self._observers = []

def __call__(cls, *args, **kwargs):
    if cls not in cls._instance:
        cls._instance[cls] = super().__call__(*args, **kwargs)
    return cls._instance[cls]

类观察者:
def 更新(自我,消息):
通过

类 EmailObserver(观察者):
def 更新(自我,消息):
print(f"邮件通知:{message}")

类 SMSObserver(观察者):
def 更新(自我,消息):
print(f"短信通知:{message}")

使用

主题=主题()
subject.attach(EmailObserver())
subject.attach(SMSObserver())

subject.notify("观察者模式已实现。")
`
为什么它有效?
主体维护观察者列表并通知他们变化,从而允许解耦通信。
策略
什么时候使用它?
当您有多种算法来执行一项任务并且您想要动态地交换它们时。

Python 示例:
`导入类型

类文本处理器:
def init(自身,格式化程序):
self.formatter = types.MethodType(formatter, self)

def attach(self, observer):
    self._observers.append(observer)

def notify(self, message):
    for observer in self._observers:
        observer.update(message)

def uppercase_formatter(self, text):
return text.upper()

def lowercase_formatter(self, text):
return text.lower()

使用

处理器 = TextProcessor(uppercase_formatter)
print(processor.process("Hello World")) # 输出:HELLO WORLD

processor.formatter = types.MethodType(lowercase_formatter, 处理器)
print(processor.process("Hello World")) # 输出:hello world
`
为什么它有效?
策略模式允许您通过为格式分配新函数来动态更改对象使用的算法。

装饰器
什么时候使用它?
当您想要动态地向对象添加新功能而不更改其结构时。

Python 示例:
`def bold_decorator(func):
def 包装器():
return "" func() ""
返回包装

def italic_decorator(func):
def 包装器():
return "" func() ""
返回包装

@bold_decorator
@italic_decorator
def say_hello():
返回“你好”

使用

print(say_hello()) # 输出:你好
`

它为什么有效?
装饰器允许您包装函数以添加功能,例如在此处进行格式化,而无需修改原始函数。

适应
什么时候使用它?
当您需要使用现有的类但其接口不符合您的需求时。

Python 示例:
`class EuropeanSocketInterface:
定义电压(自身):通过
def live(self): 通过
def 中立(自我):通过

类EuropeanSocket(EuropeanSocketInterface):
定义电压(自身):
返回230

def __call__(cls, *args, **kwargs):
    if cls not in cls._instance:
        cls._instance[cls] = super().__call__(*args, **kwargs)
    return cls._instance[cls]

USASocketInterface 类:
定义电压(自身):通过
def live(self): 通过
def 中立(自我):通过

类适配器(USASocketInterface):
def init(self, european_socket):
self.european_socket = european_socket

def attach(self, observer):
    self._observers.append(observer)

def notify(self, message):
    for observer in self._observers:
        observer.update(message)

使用

euro_socket = EuropeanSocket()
适配器 = 适配器(euro_socket)
print(f"电压:{adapter.Voltage()}V") # 输出:电压:110V
`
适配器将一个类的接口转换为客户端期望的另一个接口,从而允许不兼容的接口之间兼容。

命令
什么时候使用它?
当您想将请求封装为对象时,允许您使用不同的请求、队列或日志记录来配置客户端。

Python 示例:
`类命令:
def 执行(自我):
通过

类 LightOnCommand(命令):
def init(self, light):
self.light = 光

def process(self, text):
    return self.formatter(text)

类 LightOffCommand(命令):
def init(self, light):
self.light = 光

def live(self):
    return 1

def neutral(self):
    return -1

类光:
def open_on(自身):
print("灯亮了")

def voltage(self):
    return 110

def live(self):
    return self.european_socket.live()

def neutral(self):
    return self.european_socket.neutral()

远程控制类:
def 提交(自我,命令):
命令.execute()

使用

光 = Light()
on_command = LightOnCommand(灯光)
off_command = LightOffCommand(光)

远程 = RemoteControl()
remote.submit(on_command) # 输出:灯亮
remote.submit(off_command) # 输出:灯关闭
`
为什么它有效?
命令模式将操作转换为对象,允许配置、排队或取消操作。

5. 结论

在 Python 中选择正确的设计模式需要清楚地了解要解决的问题和可用的模式。通过对问题进行分类并分析每种模式的优点,您可以选择提供最有效解决方案的一种。

请记住,设计模式是改进代码的工具,而不是需要遵循的严格规则。明智地使用它们来编写干净、可维护且可扩展的 Python 代码。

6. 额外资源

书籍:
设计模式:可重用面向对象软件的元素 Erich Gamma 等人
埃里克·弗里曼 (Eric Freeman) 和伊丽莎白·罗布森 (Elisabeth Robson) 的 Head First 设计模式。
网站:
重构大师
深入研究设计模式
感谢您的阅读!欢迎在评论中分享您使用 Python 设计模式的经验。

以上是如何在 Python 中选择正确的设计模式(附示例)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn