等效于Python的Builder模式 [英] Builder pattern equivalent in Python

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

问题描述

在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中的生成器一样,您可以省略或重新排序调用

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("Hello({})".format(i) for i in range(100))

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

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