构建者是否应该在交付产品后重置其构建环境

发布于 2024-12-18 16:30:27 字数 878 浏览 1 评论 0原文

我正在实现一个构建器,其中通过调用 Builder::getProduct() 来检索可交付成果。导演要求各个部分构建 Builder::buildPartA()Builder::buildPartB() 等,以便完整构建产品。
我的问题是,一旦构建器通过调用 Builder::getProduct() 交付产品,它是否应该重置其环境 (Builder::partA = NULL;, < code>Builder::partB = NULL;) 以便准备好构建另一个产品? (具有相同或不同的配置?)

我问这个问题是因为我使用的是 PHP,其中对象默认通过引用传递(不,我不想克隆它们,因为它们的字段之一是一个资源)。然而,即使您从与语言无关的角度思考,构建器是否应该重置其构建环境?如果您的答案是“这取决于具体情况”,哪些用例可以证明重置环境(以及其他方式)是合理的?

为了提供代码示例,这里是我的 Builder::gerProcessor() ,它显示了我所说的“重置环境”的含义

/**
 * @see IBuilder::getProessor()
 */
public function getProcessor()
{
    if($this->_processor == NULL) {
        throw new LogicException('Processor not yet built!');
    } else {
        $retval = $this->_processor;
        $this->_product = NULL, $this->_processor = NULL;
    }
    return $retval;
}

I am implementing a builder where in the deliverable is retrieved by calling Builder::getProduct() . The director asks various parts to build Builder::buildPartA() , Builder::buildPartB() etc. in order to completely build the product.
My question is, once the product is delivered by the Builder by calling Builder::getProduct(), should it reset its environment (Builder::partA = NULL;, Builder::partB = NULL;) so that it is ready to build another product? (with same or different configuration?)

I ask this as I am using PHP wherein the objects are by default passed by reference (nope, I don't want to clone them, as one of their field is a Resource) . However, even if you think from a language agnostic point of view, should the Builder reset its build environment ? If your answer is 'it depends on the case' what use cases would justify reseting the environment (and other way round) ?

For the sake of providing code sample here's my Builder::gerProcessor() which shows what I mean by reseting the environment

/**
 * @see IBuilder::getProessor()
 */
public function getProcessor()
{
    if($this->_processor == NULL) {
        throw new LogicException('Processor not yet built!');
    } else {
        $retval = $this->_processor;
        $this->_product = NULL, $this->_processor = NULL;
    }
    return $retval;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2024-12-25 16:30:28

getProcessor() 中重置状态并不明显,如果您想这样做,该方法应该在其名称中反映这一点,例如 getProcessorAndReset()。一个更简洁的解决方案是为构建器提供一个单独的 reset() 方法。

一般来说,您的 getProcessor() 不应重置其内部状态,因为方法不应神奇地改变行为,但应可靠地执行相同的操作。 getProcessor() 是一个查询,该查询应在每次调用时返回相同的配置处理器。它不应该改变状态。重置状态是一个命令。您想要分离命令和查询方法

Resetting the state in getProcessor() is non-obvious and if you want to do that the method should reflect that in it's name, e.g. getProcessorAndReset(). A cleaner solution would be to just give the builder a separate reset() method.

In general, your getProcessor() should not reset it's internal state because methods should not magically change behavior but reliably do the same. getProcessor() is a query and that query should return the same configured Processor on each call. It should not change state. Resetting the state is a command. You want to separate command and query methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文