详解Python为什么不用设计模式

前言 刚刚看了EuroPython 2017一篇演讲,Why You Don't Need Design Patterns in Python,为什么python不用设计模式。演讲者是STXNEXT的Sebastian Buczynski。
他对设计模式的定义是:

  • 常见问题的通用可复用解决方案
  • 定型的最佳实践
他说设计模式是一种似曾相识(Anology),是一种大纲(Outline),他认为设计模式并不是拿来就能用的。
Singleton 详解Python为什么不用设计模式
文章图片

第一个是Singleton模式,Singleton的精髓就是任何时候,只有一个类的实例。
《设计模式》里面给出的Singleton代码是
声明:
class Singleton {public: static Singleton* Instance(); protected: Singleton(); private: static Singleton* _instance; };

实现:
Singleton* Singleton::_instance = 0;

Sebastian 在 Google 上面找Singleton的Python实现,找到了以下代码:
声明:
class Singleton: _instance = None def __new__(cls, *args, **kwargs):if not cls._instance:cls._instance = super().__new__(cls, *args, **kwargs)return cls._instance

实现:
one_instance = Singleton()another_instance = Singleton()one_instance is another_instance # True

Sebastian指出,照抄C++,当然也可以解决问题,但是在python里面有更好的解决方案。比如,可以用@classmethod。不过,最好的解决方案是直接用module。因为module本身就是唯一的,相当于module就实现了singleton,那么,我们为什么要大费周章,搞一个singleton出来呢?
我回忆了一下,尽管Singleton是最简单的设计模式了,但是,我这么多年一直没用。以前写C#的时候,我用的是静态类,静态类本身就是唯一的,所以我不需要singleton。当然,我看到有人也用C#写了和C++一样的Singleton,但是我觉得解决问题就可以了,没必要为了写设计模式而写设计模式。同样,写VB.net的时候,我直接用的module,也不需要singleton。
结论:当年《设计模式》里面的Singleton模式,是为了只有一个类实例。如果编程语言本身,如python, c#, vb.net,已经提供了这样的能力,就没有必要再用C++的套路了。或者说,设计模式就不需要了。
Facade 详解Python为什么不用设计模式
文章图片

详解Python为什么不用设计模式
文章图片

【详解Python为什么不用设计模式】(以上图片来自参考[1])
Facade的基本概念是,子系统用Facade来屏蔽内部的复杂实现。
这时,我们可以把子系统的python文件统一放在一个文件夹里,然后在这个文件夹里放一个__init__.py文件。
详解Python为什么不用设计模式
文章图片

Command Command模式把请求封装成对象。
Sebastian认为,在python里面,函数就是一等公民,所以没有必要创建对象。
def command(discount_rate):some_obj.notify_users_about_discount()

也可以用functools创建command
import functoolscommand = functools.partial(some_obj.notify_users_about_discount, discount_rate=0.5)command()# equals tosome_obj.notify_users_about_discount(discount_rate=0.5)

Visitor Python里面没有接口,没有方法重载。那么怎么实现Visitor呢?
Sebastian指出,可以用@SingleDispatch。
from functools import singledispatch@singledispatchdef visit(node): type_name = type(node).__name__ raise AttributeError(f'No handler found for {type_name}')

from ast_nodes import Assign, FunctionDef@visit.register(Assign)def visit(node): pass@visit.register(FunctionDef)def visit(node): pass

我们看到,这里的实现,并没有class。
Decorator Decorator可以用来扩展一个对象。
它实现的方法是新建一个类,这个类和原来的类属于同一个接口。然后这个类接受一个原来的类的对象,每个方法都调用原来的类的方法。
如果套用c++的《设计模式》,我们有
class OriginalClass: def get_text(self):pass def get_number(self):passclass Decorator: def __init__(self, decorated_obj):self.decorated_obj = decorated_obj def get_text(self):return f'{self.decorated_obj.get_text()}' def get_number(self):return self.decorated_obj.get_number()

但是,这里可以用python的__getattr__特性来简化实现。
class Decorator: def __init__(self, decorated_obj):self.decorated_obj = decorated_obj def get_text(self):return f'{self.decorated_obj.get_text()}' def __getattr__(self, attr_name):return getattr(self.decorated_obj, attr_name)

总结 Sebastian指出,python非常灵活。和25年前的C++大相径庭。很多地方,都非常容易插入逻辑。过去的设计模式,可能并不适用了。我们应该很好的了解python,并借鉴其他语言,而不是生搬硬套。
我觉得,再好的东西,也要和实际相结合。任何脱离实际的做法,都是多余的,甚至有害的。任何理论,方法的产生,都有当时的历史背景,技术背景。如果不了解背后的机制,不了解背后的精神和目的,而是专注于招式本身,那只能是越来越僵化。看似坚持,实际上是背叛。坚持是说固执的坚持原来的做法,背叛是指背叛了初衷。
参考 [1] Why You Don't Need Design Patterns in Python
[2] Design Patterns – Elements of Reusable Object-Oriented Software
到此这篇关于详解Python为什么不用设计模式的文章就介绍到这了,更多相关Python设计模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读