Php 对于这个建筑估算项目,我应该考虑什么样的设计模式?

Php 对于这个建筑估算项目,我应该考虑什么样的设计模式?,php,codeigniter,design-patterns,Php,Codeigniter,Design Patterns,我应该说我是一个业余程序员。当我从行医中得到休息时,我会练习(主要是)PHP,并试图随着时间的推移在我的知识基础上继续发展,同时希望能为人们解决一些问题 最近,我一直在为我的家庭装修业务申请。这个项目是建立在codeigniter和mongodb之上的,虽然它可以工作,但我知道它并不漂亮。我花了一些时间阅读有关设计模式的书籍,我很难决定哪一种模式适合这个项目的“估算器”部分 下面是它的工作原理: 用户输入一系列特定于确定价格方法的测量值。有时,这些测量是特定于一种产品类型的,例如水槽。其他时候,

我应该说我是一个业余程序员。当我从行医中得到休息时,我会练习(主要是)PHP,并试图随着时间的推移在我的知识基础上继续发展,同时希望能为人们解决一些问题

最近,我一直在为我的家庭装修业务申请。这个项目是建立在codeigniter和mongodb之上的,虽然它可以工作,但我知道它并不漂亮。我花了一些时间阅读有关设计模式的书籍,我很难决定哪一种模式适合这个项目的“估算器”部分

下面是它的工作原理:

用户输入一系列特定于确定价格方法的测量值。有时,这些测量是特定于一种产品类型的,例如水槽。其他时候,测量与正在进行的工作比产品更密切相关,例如,用乙烯基和铝覆盖外部拱腹

所有这些都意味着每种特定的估算方法都需要不同类型的测量。继续上面的两个例子

要估计排水沟,我们必须知道,类型(小,大,屏幕)和线性英尺。重要的是要知道每种类型都有自己的价格

为了估算拱腹,我们必须只知道线性英尺

估算公式相当简单-数量x价格=估算-但数量可以是#件、#英尺或#平方英尺

我不知道我是否把这件事弄得比它需要的更复杂,但我想把我的技能提高到一个新的水平,学习设计好的、面向对象的代码。该项目的下一步是进行这些测量,并制定工作所需的材料清单——由于有大量可用的产品,这项工作要复杂得多


我应该考虑什么样的设计模式?到目前为止,我主要关注的是
抽象工厂
Builder
应该在我的列表中吗?还有什么?

您是否可以创建一个“Estimator”接口,让它需要一个名为Estimate()的方法,然后用一组类扩展该接口,每个类完成不同的估计。它们都有一个构造函数,该构造函数接受输入参数(估算所需的所有信息),并且它们都将实现估算函数,该函数将输出总成本

现在我知道你们理解了这个模式,因为你们列出了和它非常相似的模式。我只是想告诉你我是如何编码的,这样你就可以看到它是如何工作的。有时候,即使这听起来是个坏主意,结果却非常有效,你无法想象它

比如:

interface Estimator
{
    public function Estimate ();
    public function BuildInfo ();
}

class Estimate_gutter implements Estimator
{
    private $totalLength, $gutterType;
    function __construct ($totalLength, $gutterType)
    {
        $this->totalLength = $totalLength;
        $this->gutterType = $gutterType;
    }

    function Estimate ()
    {
        return $this->totalLength * .45; // 45 cents a foot, consider a binary math library here though
    }

    function BuildInfo ()
    {
        return "Gutter Type: {$this->gutterType}, Length: {$this->totalLength}"; //to keep different classes from having to output different values, thus breaking the idea of abstraction we are trying to achieve; we'll just output a string that will be printed to the job list.
    }
}
class Estimate_sidewall implements Estimator
{
    private $width, $height, $type;
    function __construct ($drywallType, $width, $height)
    {
        $this->width = $width;
        $this->height = $height;
        $this->type = $drywallType;
    }

    function Estimate ()
    {
        return $this->width * $this->height * 1.12; // 1.12 a square foot, again consider a binary math library
    }

    function BuildInfo ()
    {
        return "SideWall type '{$this->type}' {$this->width}ft x {$this->height}ft = ". ($this->width * $this->height) ."sqft.";
    }
}

$gutter = new Estimate_gutter(100, "Type to gutter"); //pass this to the view

$gutter->Estimate(); //this would be run in the view, or be assigned in the controller to a variable for the view.  This must exist, because it is implementing the Estimator class.  The reason for this is so that you don't need to know what class type it is in the view, you just need to call the same function, no matter what.

/*
    This would print out all the information needed to retrieve
    the needed product.  It is important this function always returns
    the *exact* same data type every time for every class, otherwise
    the abstraction will break. (because if you output an array for one,
    and a string for another, how do you know when to deal with an array,
    and when to deal with a string? You'd need to know what type of estimate
    you're making, and you're attempting to eliminate needing to know that
    using this design pattern).
*/
echo $gutter->BuildInfo ();

虽然我很想看到这个问题的其他模式和解决方案,但这并不是唯一的,但一个好的解决方案可能很难制定。

这是一个很好的例子,@mazzzzz。谢谢我认为它将在项目的这一部分工作得很好。后续问题:项目的另一部分将建立一个“材料清单”,告诉工作人员完成工作所需的材料。例如,特定作业所需的乙烯基拱腹量来自多个不同的测量值(它们在估算中显示为单独的测量值)。我的问题是……如果我在
接口
中定义了一个类,我以后是否可以使用该类中的函数向
生成器
提供信息?没有足够的信息让我对后续问题做出真正的响应(我不清楚您试图实现的目标)。如果愿意,您可以在接口中创建另一个函数(可能称为BuilderInfo或其他什么),然后在每个类中实现该函数,让它们生成信息(平方英尺、长度、特定名称等)。我将更新我的示例,以包括这一点。