跳转到内容

状态

25% developed
来自维基教科书,开放世界中的开放书籍

单例模式 计算机科学设计模式
状态
策略模式

允许对象在内部状态改变时改变其行为。对象将看起来改变了它的类。

#include <iostream>
using namespace std;
class Machine
{
  class State *current;
  public:
    Machine();
    void setCurrent(State *s)
    {
        current = s;
    }
    void on();
    void off();
};

class State
{
  public:
    virtual void on(Machine *m)
    {
        cout << "   already ON\n";
    }
    virtual void off(Machine *m)
    {
        cout << "   already OFF\n";
    }
};

void Machine::on()
{
  current->on(this);
}

void Machine::off()
{
  current->off(this);
}

class ON: public State
{
  public:
    ON()
    {
        cout << "   ON-ctor ";
    };
    ~ON()
    {
        cout << "   dtor-ON\n";
    };
    void off(Machine *m);
};

class OFF: public State
{
  public:
    OFF()
    {
        cout << "   OFF-ctor ";
    };
    ~OFF()
    {
        cout << "   dtor-OFF\n";
    };
    void on(Machine *m)
    {
        cout << "   going from OFF to ON";
        m->setCurrent(new ON());
        delete this;
    }
};

void ON::off(Machine *m)
{
  cout << "   going from ON to OFF";
  m->setCurrent(new OFF());
  delete this;
}

Machine::Machine()
{
  current = new OFF();
  cout << '\n';
}

int main()
{
  void(Machine:: *ptrs[])() = 
  {
    Machine::off, Machine::on
  };
  Machine fsm;
  int num;
  while (1)
  {
    cout << "Enter 0/1: ";
    cin >> num;
    (fsm. *ptrs[num])();
  }
}
C# 实现
using System;

  class MainApp
  {
    static void Main()
    {
      // Setup context in a state
      Context c = new Context(new ConcreteStateA());

      // Issue requests, which toggles state
      c.Request();
      c.Request();
      c.Request();
      c.Request();

      // Wait for user
      Console.Read();
    }
  }

  // "State"
  abstract class State
  {
    public abstract void Handle(Context context);
  }

  // "ConcreteStateA"
  class ConcreteStateA : State
  {
    public override void Handle(Context context)
    {
      context.State = new ConcreteStateB();
    }
  }

  // "ConcreteStateB"
  class ConcreteStateB : State
  {
    public override void Handle(Context context)
    {
      context.State = new ConcreteStateA();
    }
  }

  // "Context"
  class Context
  {
    private State state;

    // Constructor
    public Context(State state)
    {
      this.State = state;
    }

    // Property
    public State State
    {
      get{ return state; }
      set
      {
        state = value;
        Console.WriteLine("State: " +
          state.GetType().Name);
      }
    }

    public void Request()
    {
      state.Handle(this);
    }
  }
Java 实现

状态接口和两个实现。状态的方法引用了上下文对象,并能够改变其状态。

interface Statelike {

    /**
     * Writer method for the state name.
     * @param STATE_CONTEXT
     * @param NAME
     */
    void writeName(final StateContext STATE_CONTEXT, final String NAME);
   
}
class StateA implements Statelike {
    /* (non-Javadoc)
     * @see state.Statelike#writeName(state.StateContext, java.lang.String)
     */
    @Override
    public void writeName(final StateContext STATE_CONTEXT, final String NAME) {
        System.out.println(NAME.toLowerCase());
        STATE_CONTEXT.setState(new StateB());
    }

}
class StateB implements Statelike {
    /** State counter */
    private int count = 0;

    /* (non-Javadoc)
     * @see state.Statelike#writeName(state.StateContext, java.lang.String)
     */
    @Override
    public void writeName(final StateContext STATE_CONTEXT, final String NAME) {
        System.out.println(NAME.toUpperCase());
        // Change state after StateB's writeName() gets invoked twice
        ++count;
        if (count > 1) {
            STATE_CONTEXT.setState(new StateA());
        }
    }
}

上下文类有一个状态变量,它在一个初始状态实例化,在本例中是 StateA。在它的方法中,它使用状态对象的对应方法。

public class StateContext {
    private Statelike myState;
        /**
         * Standard constructor
         */
    public StateContext() {
        setState(new StateA());
    }

        /**
         * Setter method for the state.
         * Normally only called by classes implementing the State interface.
         * @param NEW_STATE
         */
    public void setState(final Statelike NEW_STATE) {
        myState = NEW_STATE;
    }

        /**
         * Writer method
         * @param NAME
         */
    public void writeName(final String NAME) {
        myState.writeName(this, NAME);
    }
}

下面的测试也显示了用法

public class TestClientState {
    public static void main(String[] args) {
        final StateContext SC = new StateContext();

        SC.writeName("Monday");
        SC.writeName("Tuesday");
        SC.writeName("Wednesday");
        SC.writeName("Thursday");
        SC.writeName("Friday");
        SC.writeName("Saturday");
        SC.writeName("Sunday");
    }
}

根据上面的代码,TestClientState 的 main() 的输出应该是

monday
TUESDAY
WEDNESDAY
thursday
FRIDAY
SATURDAY
sunday
Perl 实现
use strict;
use warnings;

package State;
# base state, shared functionality
use base qw{Class::Accessor};
# scan the dial to the next station
sub scan {
	my $self = shift;
	printf "Scanning... Station is %s %s\n",
		$self->{stations}[$self->{pos}], $self->{name};
	$self->{pos}++;
	if ($self->{pos} >= @{$self->{stations}}) {
		$self->{pos} = 0
	}
}

package AmState;
our @ISA = qw(State);
AmState->mk_accessors(qw(radio pos name));
use Scalar::Util 'weaken';
sub new {
	my ($class, $radio) = @_;
	my $self;
	@$self{qw(stations pos name radio)} = 
		([1250,1380,1510], 0, 'AM', $radio);
	# make circular reference weak
	weaken $self->{radio};
	bless $self, $class;
}
sub toggle_amfm {
	my $self = shift;
	print "Switching to FM\n";
	$self->radio->state( $self->radio->fmstate );
}

package FmState;
our @ISA = qw(State);
FmState->mk_accessors(qw(radio pos name));
use Scalar::Util 'weaken';
sub new {
	my ($class, $radio) = @_;
	my $self;
	@$self{qw(stations pos name radio)} = 
		([81.3,89.3,103.9], 0, 'FM', $radio);
	# make circular reference weak
	weaken $self->{radio};
	bless $self, $class;
}
sub toggle_amfm {
	my $self = shift;
	print "Switching to AM\n";
	$self->radio->state( $self->radio->amstate );
}

package Radio;
# this is a radio, it has a scan button and a am/fm toggle
use base qw{Class::Accessor};
Radio->mk_accessors(qw(amstate fmstate state));
sub new {
	my $class = shift;
	my $self  = {};
	bless $self, $class;

	@$self{ 'amstate', 'fmstate' }
		= ( AmState->new($self), FmState->new($self), );
	$self->state( $self->amstate );
	$self;
}
sub toggle_amfm {
	shift->state->toggle_amfm;
}
sub scan {
	shift->state->scan;
}

package main;
# test out our radio
sub main {
	my $radio = Radio->new;
	my @actions = (
		('scan')x2,
		('toggle_amfm'),
		('scan')x2
	)x2;

	for my $action (@actions) {
		$radio->$action;
	}
	exit;
		
}

main();
Python 实现
import itertools

"""Implementation of the state pattern"""
class State(object):
    """Base state. This is to share functionality"""

    def scan(self):
        """Scan the dial to the next station"""
        print "Scanning... Station is", self.stations.next(), self.name

class AmState(State):
    def __init__(self, radio):
        self.radio = radio
        self.stations = itertools.cycle(["1250", "1380", "1510"])
        self.name = "AM"

    def toggle_amfm(self):
        print "Switching to FM"
        self.radio.state = self.radio.fmstate

class FmState(State):
    def __init__(self, radio):
        self.radio = radio
        self.stations = itertools.cycle(["81.3", "89.1", "103.9"])
        self.name = "FM"

    def toggle_amfm(self):
        print "Switching to AM"
        self.radio.state = self.radio.amstate

class Radio(object):
    """A radio.
    It has a scan button, and an AM/FM toggle switch."""

    def __init__(self):
        """We have an AM state and an FM state"""

        self.amstate = AmState(self)
        self.fmstate = FmState(self)
        self.state = self.amstate

    def toggle_amfm(self):
        self.state.toggle_amfm()
    
    def scan(self):
        self.state.scan()

def main():
    ''' Test our radio out '''
    radio = Radio()
    actions = ([radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2) * 2
    for action in actions:
        action()

if __name__ == '__main__':
    main()
[1]

根据上面的 Perl 和 Python 代码,main() 的输出应该是

Scanning... Station is 1250 AM
Scanning... Station is 1380 AM
Switching to FM
Scanning... Station is 81.3 FM
Scanning... Station is 89.1 FM
Scanning... Station is 103.9 FM
Scanning... Station is 81.3 FM
Switching to AM
Scanning... Station is 1510 AM
Scanning... Station is 1250 AM
Ruby 实现

在下面的 Ruby 示例中,收音机可以切换到两个状态 AM 和 FM,并有一个扫描按钮切换到下一个电台。

class State
    def scan
        @pos += 1
        @pos = 0 if @pos == @stations.size
        puts "Scanning… Station is", @stations[@pos], @name
    end
end

class AmState < State
    attr_accessor :radio, :pos, :name
    def initialize radio
        @radio = radio
        @stations = ["1250", "1380", "1510"]
        @pos = 0
        @name = "AM"
    end

    def toggle_amfm
        puts "Switching to FM"
        @radio.state = @radio.fmstate
    end
end

class FmState < State
    attr_accessor :radio, :pos, :name
    def initialize radio
        @radio = radio
        @stations = ["81.3", "89.1", "103.9"]
        @pos = 0
        @name = "FM"
    end

    def toggle_amfm
        puts "Switching to AM"
        @radio.state = @radio.amstate
    end
end

class Radio
    attr_accessor :amstate, :fmstate, :state
    def initialize
        @amstate = AmState.new self
        @fmstate = FmState.new self
        @state = @amstate
    end

    def toggle_amfm
        @state.toggle_amfm
    end

    def scan
        @state.scan
    end
end

# Test Radio
radio = Radio.new
radio.scan 
radio.toggle_amfm # Toggle the state
radio.scan
Hack 实现

状态接口和两个实现。状态的方法引用了上下文对象,并能够改变其状态。

interface IState {
  /**
   * Writer method for the state name.
   */
  public function write(StateContext $context, string $name): void;
}
use namespace HH\Lib\Str;

final class LowerCaseState implements IState {
  public function write(StateContext $context, string $name): void {
    print Str\lowercase($name) . "\n";
    $context->setState(new MultipleUpperCaseState());
  }
}
use namespace HH\Lib\Str;

final class MultipleUpperCaseState implements IState {
  private int $count = 0;

  public function write(StateContext $context, string $name): void {
    print Str\uppercase($name) . "\n";

    ++$this->count;
    if ($this->count > 1) {
      $context->setState(new LowerCaseState());
    }
  }
}

上下文类有一个状态变量,它在一个初始状态实例化,在本例中是 StateA。在它的方法中,它使用状态对象的对应方法。

final class StateContext {
  
  public function __construct(
    private IState $state = new LowerCaseState()
  ) {}

  /**
   * Set the current state.
   * Normally only called by classes implementing the State interface.
   */
  public function setState(IState $state): void {
    $this->state = $state;
  }

  public function write(string $name): void {
    $this->state->write($this, $name);
  }
}

下面的测试也显示了用法

require 'vendor/hh_autoload.hh';

<<__EntryPoint>>
async function main(): Awaitable<void> {
  $context = new StateContext();
  $context->write('Monday');
  $context->write('Tuesday');
  $context->write('Wednesday');
  $context->write('Thursday');
  $context->write('Friday');
  $context->write('Saturday');
  $context->write('Sunday');
}

根据上面的代码,main() 入口点的输出应该是

monday
TUESDAY
WEDNESDAY
thursday
FRIDAY
SATURDAY
sunday

参考资料


Clipboard

待办事项
添加更多插图。


单例模式 计算机科学设计模式
状态
策略模式


您对本页有任何疑问吗?
在此提问


在本图书上创建一个新页面


华夏公益教科书