Python 中等效的构建器模式 [英] Builder pattern equivalent in Python

查看:23
本文介绍了Python 中等效的构建器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,您可以使用 构建器模式 提供一种更具可读性的方法来实例化具有许多参数的类.在构建器模式中,使用设置命名属性的方法构造一个配置对象,然后使用它来构造另一个对象.

In Java, you can use the builder pattern to provide a more readable means to instantiating a class with many parameters. In the builder pattern, one constructs a configuration object with methods to set named attributes, and then uses it to construct another object.

Python 中的等价物是什么?是模仿相同实现的最佳方式吗?

What is the equivalent in Python? Is the best way to mimic the same implementation?

推荐答案

设计模式通常可以用内置的语言功能替换.

Design patterns can often be replaced with built-in language features.

您说我想要一个更具可读性的手段";实例化一个具有许多参数的类.".在 Java 的情况下:

You say "I wanted to have a more readable "means" to instantiating a class with many parameters.". In Java's case:

[A] 构建器模式的用例是当要构建的对象的构造器必须非常许多参数.在这种情况下,将此类配置参数集中在构建器对象中通常更方便(setMaxTemperature(int t)setMinTemperature(int t)set.. 等),而不是给调用者带来一长串参数以传入类的构造函数..

[A] use case for the builder pattern is when the constructor of the object to be built must take very many parameters. In such cases, it is often more convenient to lump such configuration parameters in a builder object (setMaxTemperature(int t), setMinTemperature(int t), set.. , etc. ) than to burden the caller with a long list of arguments to pass in the class's constructor..

不需要构建器模式

但是Python支持命名参数,所以这不是必须的.你可以定义一个类的构造函数:

Builder pattern not needed

But Python supports named parameters, so this is not necessary. You can just define a class's constructor:

class SomeClass(object):
    def __init__(self, foo="default foo", bar="default bar", baz="default baz"):
        # do something

并使用命名参数调用它:

and call it using named parameters:

s = SomeClass(bar=1, foo=0)

请注意,您可以自由地重新排序和省略参数,就像使用 Java 中的构建器一样,您可以省略或重新排序对构建器对象上的 set 方法的调用.

Note that you can freely reorder and omit arguments, just as with a builder in Java you can omit or reorder calls to the set methods on the builder object.

另外值得一提的是,Python 的动态特性让您可以更自由地构建对象(使用 __new__ 等),这可以替代构建器模式的其他用途.

Also worth stating is that Python's dynamic nature gives you more freedom over construction of objects (using __new__ etc.), which can replace other uses of the builder pattern.

您可以使用 collections.namedtuple 作为您的配置对象.namedtuple() 返回一个表示元组的新类型,其每个参数都有一个给定的名称,而无需编写样板类.您可以以类似于 Java 构建器的方式使用结果类型的对象.(感谢 Paul McGuire 提出这个建议.)

you can use collections.namedtuple as your config object. namedtuple() returns a new type representing a tuple, each of whose parameters has a given name, without having to write a boilerplate class. You can use objects of the resulting type in a similar way to Java builders. (Thanks to Paul McGuire for suggesting this.)

一个相关的模式是 Java 的 StringBuilder,它用于有效地分阶段构造一个(不可变的)String.在 Python 中,这可以替换为 str.join.例如:

A related pattern is Java's StringBuilder, which is used to efficiently construct an (immutable) String in stages. In Python, this can be replaced with str.join. For example:

final StringBuilder sb = new StringBuilder();
for(int i = 0; i < 100; i++)
    sb.append("Hello(" + i + ")");
return sb.toString();

可以替换为

return "".join(f"Hello({i})" for i in range(100))

这篇关于Python 中等效的构建器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆