HP OOP构建器模式使用
我对在实践中使用PHP构建器模式感到困惑。 在许多文档中,他们建议使用这样的构建器。
require 'Pizza.php';
require 'PizzaBuiler.php';
$piza_builder=(new PizzaBuilder('medium'))
->cheeze(true)
->bacon(true)
->build();
$pizza=new Pizza($piza_builder);
披萨类使用pizzabuilder作为构造函数参数和Init类属性。
为什么不直接从构建器实例化对象???这是不好的(反原本)。
require 'Pizza.php';
require 'PizzaBuiler.php';
$piza= Pizza::getBuilder("medium")
->cheeze(true)
->bacon(true)
->build();
两个失败者之间的唯一区别是修改构建器类中的build()函数以返回新的披萨对象,而不是返回构建器实例。
您能建议我使用什么干净的建筑商使用吗???
I'm confused about using PHP Builder pattern in practise.
In many documentation they propose using the Builder like this.
require 'Pizza.php';
require 'PizzaBuiler.php';
$piza_builder=(new PizzaBuilder('medium'))
->cheeze(true)
->bacon(true)
->build();
$pizza=new Pizza($piza_builder);
Pizza class use PizzaBuilder as constructor parameter and init class properties from it.
Why not instantiate object directly from Builder ??? is this bad (Anti-Pattern).
require 'Pizza.php';
require 'PizzaBuiler.php';
$piza= Pizza::getBuilder("medium")
->cheeze(true)
->bacon(true)
->build();
The only difference between two implemnettations is to modify build() function in Builder class to return new Pizza Object instead of of returning Builder instance.
can you advice me what clean builder to use ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为构建器模式有一些变体,因为它是设计模式而不是规范。
构建器模式旨在解决复杂物体构建的问题。只要它执行打算做的事情,就可以了。
一定。考虑
pizza
是意大利菜肴的概念表示。Pizzabuilder
是实用程序来构建pizza
。pizza
足够好,可以在没有pizzabuilder
的情况下独立存在。我认为在产品构造函数中接受构建器并不是必需的。此外,由于单一责任原则,我不想在
pizza
中包括静态pizzabuilder
。如上所述,披萨足够好,可以独自存在。pizza
与pizzabuilder
无关。它是pizzabuilder
需要pizza
的知识才能构建pizza
。我将留下任何pizza
施工留在pizzabuilder
中,并将其从pizza
将其解除。我会以这种方式构造代码。
更新21-APR-2022以解决评论
我认为构造函数不是围绕该课程构建的广告公用事业的工具。这取决于开发人员/用户如何了解该框架,并以他们为实现其目的进行编程。
如果
pizzabuilder
是独立的,:
如果
pizzabuilder
是静态成员pizza
:我们必须考虑
pizzabuilder
和getBuilder()
表示pizza
。他们是否在帮助pizza
实现目标?如果pizza
是意大利菜肴的概念表示(当然,这只是我的解释。您可能有自己的解释,因为您最了解您的应用程序),是getBuilder()
与其他pizza
构造相干?将来,如果
pizza
需要更改,那可能是什么原因?也许它有更多的成分可添加。也许Pizzabuilder
不再适合业务需求。有一天,如果我们需要一种新算法来精确控制水和面粉的比率。我们使用复杂的算法开发了一种新的Precisepizzabuilder
。然后,我们用pizzabuilder
替换precisepizzabuilder
。更改pizza
的原因有两个。这可能暗示我们将两件不同的东西造成。毕竟,我认为没有直接的对与错。在某种程度上,我的回答涉及我对您的应用程序的理解。我可能会提供偏见的意见,因为我讨厌披萨(不是真的,我喜欢它)。我可能会理解您的应用程序错误。但是,最重要的是,您为每个模块提供了一个目的,并在整个应用程序寿命中保留它。预见,将来哪一部分将会改变,以及您今天可以做些什么,以使您的未来生活更轻松。然后,您将知道需要做出什么决定。这是OOP,坚实的原理和设计模式。使代码灵活且可维护。
I think there are some variants for the builder pattern since it is a design pattern not a specification.
Builder pattern aims to solve the problem of complex object construction. As long as it does what it intends to do, it is ok.
For sure. Consider
Pizza
is a conceptual representation of an Italian dish.PizzaBuilder
is a utility helps to buildPizza
. APizza
is good enough to exist on its own without aPizzaBuilder
. I think it is not a must to accept a Builder in Product constructor.Moreover, I prefer not to include a static
PizzaBuilder
inPizza
because of the Single-responsibility principle. As stated above, a pizza is good enough to exist on its own.Pizza
has nothing to do with aPizzaBuilder
. It isPizzaBuilder
requires the knowledge ofPizza
to buildPizza
. I will leave any logic ofPizza
construction stay in thePizzaBuilder
and decouple it fromPizza
.I would structure the code this way.
Update on 21-Apr-2022 to address comments
I think constructor is not a tool for advertising utilities built around that class. It is depends on how developer/user are knowledgeable about the framework, and base on that they program their application to achieve their purpose.
If
PizzaBuilder
is standalone:If
PizzaBuilder
is a static member ofPizza
:We have to think about what do
PizzaBuilder
andgetBuilder()
mean toPizza
. Are they helpingPizza
to achieve something? IfPizza
is a conceptual representation of an Italian dish (of course this is only my interpretation. You may have your own since you know your application best), isgetBuilder()
coherent to the rest ofPizza
construct?In the future, if
Pizza
need a change, what would be the possible reason? Maybe it has more ingredients to add. Maybe thePizzaBuilder
does not fit the business needs anymore. One day, if we need a new algorithm to precisely control the water and flour ratio. We develop a newPrecisePizzaBuilder
with a complex algorithm. Then we replacePrecisePizzaBuilder
withPizzaBuilder
. There would be two reasons to changePizza
. It might be a hint that we couple two different things.After all, I think there is no straight right or wrong. My answer, to some extent, involve my own comprehension to your application. I may provide bias opinion because I hate pizza (not really, I love it). I may understand your application wrong. Nevertheless, most important thing is that you give each module a purpose and preserve it throughout the entire application lifespan. Foresee which part will be changed in the future and what you can do today to make your future life easier. Then you will know what decision you need to make. This is OOP, SOLID principle, and design pattern all about. To make code flexible and maintainable.