[英]What design pattern do I use when I need to call a helper function before I create an object?
I have a class Motor
, which I use to provide me with various motor specifications.我有一个类
Motor
,我用它来为我提供各种电机规格。
I currently have the following methods as part of the Motor
class:我目前有以下方法作为
Motor
类的一部分:
public function getIdFromModel($model)
{
$sql = "SELECT id FROM ... WHERE model = ...";
return process($sql);
}
public function getIdByFrame($size)
{
$sql = "SELECT id FROM ... WHERE size = ...";
return process($sql);
}
The above are supplementary functions used to get motor's id
from database, when some other non- id
parameter is known.以上是用于从数据库中获取电机
id
补充函数,当一些其他非id
参数已知时。 id
is then used to create the actual motor.然后使用
id
来创建实际的电机。
Before I could do:在我能做之前:
$model = "ABC";
$motor = new Motor();
//retrieve ID
$id = $motor->getIdFromModel($model)
$motor-loadSpecs($id);
$motor = new Motor($id);
My problem shows up after I have changed the structure of my class to require knowledge of id
at Motor
creation time.在我更改了类的结构以在创建
Motor
时需要id
知识后,我的问题出现了。 Now I need to do:现在我需要做的是:
$model = "ABC";
//retrieve ID
$id = $???->getIdFromModel($model)
$motor = new Motor($id);
Question题
What structure/class/pattern/function do I use in place of ???
我用什么结构/类/模式/函数来代替
???
above?以上?
I know that I can create a class just for the above two functions, but I feel there is a better answer/pattern for this and I wanted to ask.我知道我可以为上述两个函数创建一个类,但我觉得有一个更好的答案/模式,我想问一下。 I don't see a meaning for creating a class just for something-to-id methods.
我没有看到仅仅为某个东西到 id 方法创建一个类的意义。 I'd like to have some meaning.
我想有点意思。
You would normally use a Factory Patter for this.您通常会为此使用工厂模式。 It could be a stand-alone function or a static class method depending on how you want to implement it.
它可以是独立函数或静态类方法,具体取决于您希望如何实现它。
http://www.oodesign.com/factory-pattern.html http://www.oodesign.com/factory-pattern.html
I'd make them static functions, member of Motor.我会让它们成为静态函数,Motor 的成员。
public static function getIdFromModel($model)
{
..
}
Then you can call the function, without the need of an instance.然后您可以调用该函数,而无需实例。
$id = Motor::getIdFromModel($model)
Factory Method + supporting methods工厂方法+支持方法
Usage:用法:
$id = 17;
$model = "ABC";
$frame = "T123";
$factory = new MotorFactory();
//instantiation via various pieces of data
$motor = $factory->fromId($id);
$motor = $factory->fromModel($model);
$motor = $factory->fromFrame($frame);
Factory Method:工厂方法:
class MotorFactory
{
public function fromId($id)
{
return new Motor($id);
}
public function fromModel($model)
{
$id = $this->getIdFromModel($model);
return $this->fromId($id);
}
public function fromFrame($frame)
{
$id = $this->getIdFromFrame($frame);
return $this->fromId($id);
}
//supporting methods
private function getIdFromModel($model)
{
$sql = "SELECT id FROM ... WHERE model = ...";
return process($sql);
}
private function getIdFromFrame($size)
{
$sql = "SELECT id FROM ... WHERE size = ...";
return process($sql);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.