Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 应用中的管线/阶段设计模式_Php_Algorithm_Oop_Laravel_Design Patterns - Fatal编程技术网

Php 应用中的管线/阶段设计模式

Php 应用中的管线/阶段设计模式,php,algorithm,oop,laravel,design-patterns,Php,Algorithm,Oop,Laravel,Design Patterns,在我的申请中,潜在客户必须经历一些阶段 例如: New In Progress Won Lost 这只是11个阶段中的4个阶段。在每个阶段,都会有一些必须执行的操作 例如,潜在客户从新客户转移到进行中客户,我需要更新四个表中的一组特定值 我可以在这里使用什么样的理想设计模式,以便将来如果出现任何新的阶段,可以很容易地在代码中容纳它 我在考虑使用工厂模式 编辑: 我是这样做的: interface StageInterface { public function changeStage(

在我的申请中,潜在客户必须经历一些阶段

例如:

New
In Progress
Won
Lost
这只是11个阶段中的4个阶段。在每个阶段,都会有一些必须执行的操作

例如,潜在客户从新客户转移到进行中客户,我需要更新四个表中的一组特定值

我可以在这里使用什么样的理想设计模式,以便将来如果出现任何新的阶段,可以很容易地在代码中容纳它

我在考虑使用工厂模式

编辑: 我是这样做的:

interface StageInterface
{
    public function changeStage($previous_stage, $next_stage, $ticket_details);
}

abstract class ParentStage implements StageInterface
{
    public function changeStage($previous_stage, $next_stage, $ticket_details)
    {
        $ticket_details->stage_id = $next_stage;
        $ticket_details->save();

        return $this;
    }
}

Class InProgress extends ParentStage
{
}

class TicketStagesFactory
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function getObject()
    {
        switch ($this->data) {

            case Stage::IN_PROGRESS:
                return new \App\Http\Controllers\Stages\InProgress();
                break;
     }
}
$factory = new TicketStagesFactory($next_stage);
$test = $factory->getObject()->changeStage($previous_stage, $next_stage, $ticket_details);
$lead = new Lead();

//will handle the first stage
$lead->handle();

//something causes the stage to change
$lead->setStage( new InProgress( $lead ) );

//will handle the InProgess stage logic
$lead->handleStage();
然后我就这样使用工厂:

interface StageInterface
{
    public function changeStage($previous_stage, $next_stage, $ticket_details);
}

abstract class ParentStage implements StageInterface
{
    public function changeStage($previous_stage, $next_stage, $ticket_details)
    {
        $ticket_details->stage_id = $next_stage;
        $ticket_details->save();

        return $this;
    }
}

Class InProgress extends ParentStage
{
}

class TicketStagesFactory
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function getObject()
    {
        switch ($this->data) {

            case Stage::IN_PROGRESS:
                return new \App\Http\Controllers\Stages\InProgress();
                break;
     }
}
$factory = new TicketStagesFactory($next_stage);
$test = $factory->getObject()->changeStage($previous_stage, $next_stage, $ticket_details);
$lead = new Lead();

//will handle the first stage
$lead->handle();

//something causes the stage to change
$lead->setStage( new InProgress( $lead ) );

//will handle the InProgess stage logic
$lead->handleStage();

这是正确的方法吗?

这实际上更像是一个工作流。 状态机的工作原理与此类似: 对于一个状态,事件将导致它转换到另一个状态。 工作流的工作原理更像您所说的: 对于一个状态,必须执行操作,这些操作的结果决定下一个状态


我想为php寻找一个工作流引擎,使您的工作具有可维护性。

这取决于您的程序流程,但想法可能是:

//ABSTRACT CLASS FOR A STAGE
abstract class LeadStage
{
    protected $lead;

    //INJECT YOUR LEAD OBJECT
    public function __construct( Lead $lead )
    {
        $this->lead = $lead;
    }

    //handle the stage logic on the $lead object
    public abstract function handle();
}

//CONCRETE CLASS FOR A STAGE
class InProgress extends LeadStage
{
    //HANDLE THE CONCRETE STAGE LOGIC ON THE LEAD OBJECT
    public function handle()
    {
        //do some logic on $this->lead;
    }
}

//LEAD OBJECT
class Lead
{
    protected $leadStage;

    public function construct(  )
    {
        //here build your first LeadStage object: you could also use a factory
        //pattern to create the LeadStage instance
        $this->leadStage = //...
    }

    public function setStage( LeadStage $stage )
    {
        $this->leadStage = $stage;    
    }

    public function handleStage()
    {
        $this->leadStage->handle();
    }
}
并使用如下类:

interface StageInterface
{
    public function changeStage($previous_stage, $next_stage, $ticket_details);
}

abstract class ParentStage implements StageInterface
{
    public function changeStage($previous_stage, $next_stage, $ticket_details)
    {
        $ticket_details->stage_id = $next_stage;
        $ticket_details->save();

        return $this;
    }
}

Class InProgress extends ParentStage
{
}

class TicketStagesFactory
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function getObject()
    {
        switch ($this->data) {

            case Stage::IN_PROGRESS:
                return new \App\Http\Controllers\Stages\InProgress();
                break;
     }
}
$factory = new TicketStagesFactory($next_stage);
$test = $factory->getObject()->changeStage($previous_stage, $next_stage, $ticket_details);
$lead = new Lead();

//will handle the first stage
$lead->handle();

//something causes the stage to change
$lead->setStage( new InProgress( $lead ) );

//will handle the InProgess stage logic
$lead->handleStage();
因此,为每个阶段使用单个类,将把处理阶段逻辑的责任委托给特定类的对象


当您需要添加状态时,您只需创建一个新的
LeadStage
implementation

我已经添加了我的实现,您能看看它是否正确吗?@phantomphoenix:changeStage方法的签名与您的界面和I实现不同。应该是一样的。考虑将<代码>票证对象作为一个依赖项传递给阶段对象,正如我在我的示例中所展示的。是的,我改变了。可能错过了。但是一般来说,实现是否正确?@phantomphoenix:通常你不能说一个模式是否“正确”,这取决于上下文和它的使用方式;如果您觉得它适合您的体系结构,那么就使用它