简体   繁体   English

在创建对象之前需要调用辅助函数时,我使用什么设计模式?

[英]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.

相关问题 我需要什么正则表达式模式? - What regex pattern do I need for this? 为什么需要使用Kohana Form助手功能? - Why do i need to use Kohana Form helper functions? 在面向对象的 PHP 中使用函数之前是否需要定义函数? - Do I need to define functions before I use them in object-oriented PHP? 我可以使用什么设计模式来模拟PHP中的特征/混合? - What design pattern can I use to emulate traits / mixins in PHP? 测试基础架构应使用哪种设计模式 - What design pattern(s) should I use for a testing infrastructure 我如何在Codeigniter的帮助文件中正确创建函数 - How do i properly create a function in a helper file in codeigniter 当我需要同时从MySQL数据库和本地XML文件中检索数据时,将应用哪种设计模式? - What design pattern would apply when I need to retrieve data from both MySQL DB and local XML files? 当我在对象上调用函数时,为什么在非对象上出现此函数调用错误? - Why do I get this function call error on an non-object when I am calling a function on an object? 如何调用创建图片功能? - how do I call create image function? 如何在PHP中调用以检查字符串是否与模式匹配? - What do I call in PHP to check if a string matches a pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM