PHP - 如何在多个子类之间共享设置并实例化这些共享设置一次?
假设我有 3 个 PHP 类:ShedBuilder、HouseBuilder 和 TableBuilder 以及以下代码:
$worker_1 = new ShedBuilder(); // I only know how to build a shed
$worker_2 = new HouseBuilder(); // I only know how to build a house
$worker_3 = new TableBuilder(); // I only know how to build a table
$worker_1->build();
$worker_2->build();
$worker_3->build();
我希望worker_1、worker_2 和worker_3 类各自共享相同的工作工具集,例如,我希望所有 3 个工作人员都使用可用的工具,在本例中是锤子、螺丝刀、钉子等。但是worker_1将建造一个棚子,worker_2将建造一座房子,worker_3将建造一张桌子。
设置一次工具集然后让 3 名工人中的每一位都知道如何访问他们工作所需的工具的最佳方法是什么?
我考虑过让棚屋/桌子/房屋建造者从父类 Builder 扩展,但这意味着当我实例化每个工具集时,我必须给它们每个工具集:
$toolset = new Toolset();
$toolset->addTool('hammer');
$toolset->addTool('screwdriver');
$toolset->addTool('nails');
$worker_1 = new ShedBuilder($toolset);
$worker_2 = new HouseBuilder($toolset);
$worker_3 = new TableBuilder($toolset);
我宁愿实例化一次工具,然后让所有 3 个工人/classes 了解工具集并分别构建它们的每个项目。
实现我在 PHP 中尝试执行的操作的最佳方法是什么?
Let's say I have 3 PHP classes: ShedBuilder, HouseBuilder and TableBuilder and the following code:
$worker_1 = new ShedBuilder(); // I only know how to build a shed
$worker_2 = new HouseBuilder(); // I only know how to build a house
$worker_3 = new TableBuilder(); // I only know how to build a table
$worker_1->build();
$worker_2->build();
$worker_3->build();
I want worker_1, worker_2, and worker_3 classes to each share the same toolset for the job, for example, I want all 3 workers to use the tools available, in this case a hammer, screwdriver, nails, etc. But worker_1 will build a shed, worker_2 will build a house, worker_3 builds a table.
What's the best way to set the toolset once and then have each of the 3 workers know how to access the tools they need for the job?
I thought about having Shed/Table/House builder each extend from a parent class, Builder, but that means that when I instantiate each I have to give them each the toolset:
$toolset = new Toolset();
$toolset->addTool('hammer');
$toolset->addTool('screwdriver');
$toolset->addTool('nails');
$worker_1 = new ShedBuilder($toolset);
$worker_2 = new HouseBuilder($toolset);
$worker_3 = new TableBuilder($toolset);
I'd rather instantiate the tools once and then have all 3 workers/classes know about the toolset and build each of their items, respectively.
What's the best way to implement what I'm trying to do in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经拥有的实例化
Toolset
并向其构造函数中的每个工作器类提供$toolset
称为 依赖注入,,它实际上是大多数时候处理此任务的首选方式。可以通过创建一个单例
Toolset
对象来完成它,但是当依赖注入以整洁且易于测试的方式处理它时,这是不必要的。What you have already, instantiating a
Toolset
and providing$toolset
to each of the worker classes in their constructors, is called Dependency Injection, and it is actually the preferred way of handling this task most of the time.It is possible to accomplish it by creating a singleton
Toolset
object, however that is unnecessary when dependency injection handles it in a tidy and easily testable way.我认为对这 3 个构建器类使用父类,并在父类中使用工具集的静态变量即可。 检查此页面以了解静态变量的使用情况。
I think using a parent class for those 3 builder classes, and in the parent class using static variable for toolset will do. Check this page for usage of static variable.