来看一下状态模式的角色组成吧:
1) 使用环境(Context)角色:客户程序是通过它来满足自己的需求。它定义了客户程序需要的接口;并且维护一个具体状态角色的实例,这个实例来决定当前的状态。
2) 状态(State)角色:定义一个接口以封装与使用环境角色的一个特定状态相关的行为。
3) 具体状态(Concrete State)角色:实现状态角色定义的接口
下面是GOF在《设计模式》中给出的状态模式的适用情况:
1) 一个对象的行为取决于它的状态, 并且它必须在运行时刻根据状态改变它的行为。
2) 一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态
using
System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace StatePatten
... {
class Program
...{
static void Main(string[] args)
...{
TcpConnection tcpcon = new TcpConnection();
tcpcon.Open();
tcpcon.Acknowledge();
tcpcon.Open();
tcpcon.Close();
tcpcon.Acknowledge();
tcpcon.Close();
}
}
public interface ITcpState
...{
void Open();
void Close();
void Acknowledge();
}
public class TcpConnection
...{
private ITcpState state;
public ITcpState State
...{
set ...{ state = value; }
}
public void Open()
...{
this.state = new TcpEstablished();
this.state.Open();
this.state = new TcpListen();
}
public void Close()
...{
this.state.Close();
this.state = new TcpClosed();
}
public void Acknowledge()
...{
this.state.Acknowledge();
}
}
public class TcpEstablished : ITcpState
...{
public void Open()
...{
Console.WriteLine("Have opened, can not open again!");
}
public void Close()
...{
Console.WriteLine("Closing");
}
public void Acknowledge()
...{
Console.WriteLine("TcpEstablished!");
}
}
public class TcpListen : ITcpState
...{
public void Open()
...{
Console.WriteLine("Have opened, can not open again!");
}
public void Close()
...{
Console.WriteLine("Closing");
}
public void Acknowledge()
...{
Console.WriteLine("Tcp Listen!");
}
}
public class TcpClosed : ITcpState
...{
public void Open()
...{
Console.WriteLine("Opening");
}
public void Close()
...{
Console.WriteLine("Have closed, can't close again!");
}
public void Acknowledge()
...{
Console.WriteLine("Tcp Closed!");
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace StatePatten
... {
class Program
...{
static void Main(string[] args)
...{
TcpConnection tcpcon = new TcpConnection();
tcpcon.Open();
tcpcon.Acknowledge();
tcpcon.Open();
tcpcon.Close();
tcpcon.Acknowledge();
tcpcon.Close();
}
}
public interface ITcpState
...{
void Open();
void Close();
void Acknowledge();
}
public class TcpConnection
...{
private ITcpState state;
public ITcpState State
...{
set ...{ state = value; }
}
public void Open()
...{
this.state = new TcpEstablished();
this.state.Open();
this.state = new TcpListen();
}
public void Close()
...{
this.state.Close();
this.state = new TcpClosed();
}
public void Acknowledge()
...{
this.state.Acknowledge();
}
}
public class TcpEstablished : ITcpState
...{
public void Open()
...{
Console.WriteLine("Have opened, can not open again!");
}
public void Close()
...{
Console.WriteLine("Closing");
}
public void Acknowledge()
...{
Console.WriteLine("TcpEstablished!");
}
}
public class TcpListen : ITcpState
...{
public void Open()
...{
Console.WriteLine("Have opened, can not open again!");
}
public void Close()
...{
Console.WriteLine("Closing");
}
public void Acknowledge()
...{
Console.WriteLine("Tcp Listen!");
}
}
public class TcpClosed : ITcpState
...{
public void Open()
...{
Console.WriteLine("Opening");
}
public void Close()
...{
Console.WriteLine("Have closed, can't close again!");
}
public void Acknowledge()
...{
Console.WriteLine("Tcp Closed!");
}
}
}