状态设计模式

作者: wudanyang | 来源:发表于2018-02-27 18:14 被阅读9次

什么是状态设计模式

类图如下:


状态设计模式

这个可以理解为对象行为随着状态改变的一个设计模式

如,随着四季变化,树的行为也跟着变化。

随着开关的变化,灯泡的行为也跟着变化。

为什么要使用

在软件设计过程中,对象的状态可能是会发生改变的,当对象的状态改变之后,我们可以使用 if 或者 switch 语句来判断对象的行为,但是如果对象的状态特别多的话,就会发生一个问题,对象的状态增加修改之后会频繁修改原来的类。

使用状态设计模式时,允许一个对象在其内部状态改变时改变它的行为。

类型

行为型模式

参与者

  • Context 上下文类

    • 维护一个具体状态角色ConcreteState的实例,这个实例定义当前状态。
  • IState 抽象状态类

    • 声明了各种不同状态对应的方法。
  • ConcreteState 具体状态类

    • 每一个实现一个与Context的一个状态相关的行为。

例子

只有两种状态的灯泡

代码中 Light 代表 Context 上下文类

<?php

/**
 * 只有两种状态的灯泡,可以开启和关闭
 */

/**
 * 灯
 */
class Light
{
    private $offState;
    private $onState;

    /**
     * @var IState
     */
    private $currentState;

    public function __construct()
    {
        $this->offState = new offState($this);
        $this->onState = new onState($this);
        $this->currentState = $this->offState;
    }

    public function turnOn()
    {
        $this->currentState->turnOn();
    }

    public function turnOff()
    {
        $this->currentState->turnOff();
    }

    public function setState(IState $state)
    {
        $this->currentState = $state;
    }

    public function getOnState()
    {
        return $this->onState;
    }

    public function getOffState()
    {
        return $this->offState;
    }
}

interface IState
{
    public function turnOn();

    public function turnOff();
}

/**
 * 灯的关闭状态
 */
class offState implements IState
{
    private $context;

    public function __construct(Light $light)
    {
        $this->context = $light;
    }

    public function turnOff()
    {
        echo 'light is already **off**, please chose another operation'.PHP_EOL;
    }

    public function turnOn()
    {
        echo 'light on!'.PHP_EOL;
        $this->context->setState($this->context->getOnState());
    }
}

/**
 * 灯的开启状态
 */
class onState implements IState
{
    private $context;

    public function __construct(Light $light)
    {
        $this->context = $light;
    }

    public function turnOff()
    {
        echo 'light off!'.PHP_EOL;
        $this->context->setState($this->context->getOffState());
    }

    public function turnOn()
    {
        echo 'light is already **on**, please chose another operation'.PHP_EOL;
    }
}

class Client
{
    private $light;

    public function __construct()
    {
        $this->light = new Light();
        $this->light->turnOn();
        $this->light->turnOn();
        $this->light->turnOff();
        $this->light->turnOff();
    }
}

$client = new Client();

新增加一种损坏状态

<?php

// 状态模式设计方法

/**
 * 灯
 */
class Light
{
    private $offState;
    private $onState;
    private $brokenState;

    /**
     * @var IState
     */
    private $currentState;

    public function __construct()
    {
        $this->offState = new offState($this);
        $this->onState = new onState($this);
        $this->brokenState = new brokenState();
        $this->currentState = $this->offState;
    }

    public function turnOn()
    {
        $this->currentState->turnOn();
    }

    public function turnOff()
    {
        $this->currentState->turnOff();
    }

    public function brokeIt()
    {
        $this->currentState->turnBroken();
    }

    public function setState(IState $state)
    {
        $this->currentState = $state;
    }

    public function getOnState()
    {
        return $this->onState;
    }

    public function getOffState()
    {
        return $this->offState;
    }

    public function getBrokeState()
    {
        return $this->brokenState;
    }
}

interface IState
{
    public function turnOn();

    public function turnOff();

    public function turnBroken();
}

/**
 * 灯的关闭状态
 */
class offState implements IState
{
    private $context;

    public function __construct(Light $light)
    {
        $this->context = $light;
    }

    public function turnOff()
    {
        echo 'light is already **off**, please chose another operation'.PHP_EOL;
    }

    public function turnOn()
    {
        echo 'light on!'.PHP_EOL;
        $this->context->setState($this->context->getOnState());
    }

    public function turnBroken()
    {
        echo 'light is broke now'.PHP_EOL;
        $this->context->setState($this->context->getBrokeState());
    }
}

/**
 * 灯的开启状态
 */
class onState implements IState
{
    private $context;

    public function __construct(Light $light)
    {
        $this->context = $light;
    }

    public function turnOff()
    {
        echo 'light off!'.PHP_EOL;
        $this->context->setState($this->context->getOffState());
    }

    public function turnOn()
    {
        echo 'light is already **on**, please chose another operation'.PHP_EOL;
    }

    public function turnBroken()
    {
        echo 'light broken now'.PHP_EOL;
        $this->context->setState($this->context->getBrokeState());
    }
}

/**
 * 新增一个损坏状态
 *
 * Class brokenState
 */
class brokenState implements IState
{
    public function turnOn()
    {
        echo 'light is broken, can not open now'.PHP_EOL;
    }

    public function turnOff()
    {
        echo 'light is already off, because it is broken.'.PHP_EOL;
    }

    public function turnBroken()
    {
        echo 'light is already broken.'.PHP_EOL;
    }
}

/**
 * Class Client
 */
class Client
{
    private $light;

    public function __construct()
    {
        $this->light = new Light();
        $this->light->turnOn();
        $this->light->turnOn();
        $this->light->turnOff();
        $this->light->turnOff();
        $this->light->brokeIt();
        $this->light->turnOn();
        $this->light->turnOff();
    }
}

$client = new Client();

light on!
light is already **on**, please chose another operation
light off!
light is already **off**, please chose another operation
light is broke now
light is broken, can not open now
light is already off, because it is broken.

相关文章

  • 设计模式-状态模式

    设计模式-状态模式 设计模式 状态模式的关键是区分事物内部的状态

  • 设计模式-状态设计模式

    1.定义 对于某个操作,由于其状态的不同,表现出的行为会不同(如遥控器进行音量增加的操作,在电视是开机状态下是可以...

  • 设计模式——状态模式

    设计模式——状态模式 在状态模式中,类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。 优点: 减少...

  • 设计模式——状态模式

    前言 设计模式是指导一个程序猿以更好的姿态处理一些问题,而不再像刚学编程的我们,只会使用if-else分支语句,或...

  • 设计模式--状态模式

    基本常识:策略模式与状态模式是双胞胎,在出生时才分开。 假设公司有个糖果机,1当糖果机由糖果,投入25分钱,转动曲...

  • 设计模式——状态模式

    在阎宏博士的《JAVA与模式》一书中开头是这样描述状态(State)模式的:状态模式,又称状态对象模式(Patte...

  • 设计模式《状态模式》

    引言   上一节我们说了策略模式。这一节我们讲讲策略模式的双胞胎弟弟:状态模式,这个模式大家可能不常见,也不常用,...

  • 设计模式——状态模式

    定义 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行...

  • 设计模式 - 状态模式

    模式定义 允许一个对象在其内部状态发生改变时改变它的行为。对象看起来似乎修改了它的类。 状态模式(State Pa...

  • 设计模式 ——— 状态模式

    STATE(状态) ———— 对象行为型模式 意图 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改...

网友评论

    本文标题:状态设计模式

    本文链接:https://www.haomeiwen.com/subject/modczxtx.html