基于C#的设计模式学习之状态模式

        状态模式是一种对象行为型设计模式,对有若干对象的状态,将每一种状态封装成一个对象,把负载的判读逻辑提取到不同的对象中。以线程的转换为例。如下所示:

 

namespace Designmode.State
{
    /// <summary>
    /// 线程状态抽象类
    /// </summary>
    public abstract class ThreadState
    {
        /// <summary>
        /// 状态名称
        /// </summary>
        public string Name { get; set; }
    }
}

 

namespace Designmode.State
{
    /// <summary>
    /// 新建状态
    /// </summary>
    public class NewState:ThreadState
    {
        /// <summary>
        /// 构造一个新建状态实例
        /// </summary>
        public NewState()
        {
            this.Name = "New";
        }
        /// <summary>
        /// 开始运行
        /// </summary>
        /// <param name="hj">想成上下文</param>
        public void Start(ThreadContext hj)
        {
            hj.SetState(new RunnableState());
            Console.WriteLine($"State: {this.Name} to {hj.GetState().Name}");
        }
    }
}
namespace Designmode.State
{
    /// <summary>
    /// 可运行状态类
    /// </summary>
    public class RunnableState:ThreadState
    {
        /// <summary>
        /// 可运行的状态
        /// </summary>
        public RunnableState()
        {
            this.Name = "Runable";
        }
        /// <summary>
        /// 获取CPU
        /// </summary>
        /// <param name="hj">线程上下文</param>
        public void GetCPU(ThreadContext hj)
        {
            hj.SetState(new RunningState());
            Console.WriteLine($"State: {this.Name} to {hj.GetState().Name}");
        }
    }
}
namespace Designmode.State
{
    /// <summary>
    /// 运行状态类
    /// </summary>
    public class RunningState:ThreadState
    {
        /// <summary>
        /// 构造一个运行状态类
        /// </summary>
        public RunningState()
        {
            this.Name = "Running";
        }
        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="hj">线程上下文</param>
        public void Suspend(ThreadContext hj)
        {
            hj.SetState(new BlockedState());
            Console.WriteLine($"State: {this.Name} to {hj.GetState().Name}");
        }
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="hj">线程上下文</param>
        public void Stop(ThreadContext hj)
        {
            hj.SetState(new DeadState());
            Console.WriteLine($"State: {this.Name} to {hj.GetState().Name}");
        }
    }
}
namespace Designmode.State
{
    /// <summary>
    /// 线程阻塞状态
    /// </summary>
    public class BlockedState:ThreadState
    {
        /// <summary>
        /// 构造一个阻塞状态实例
        /// </summary>
        public BlockedState()
        {
            this.Name = "Blocked";
        }
        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="hj">线程上下文</param>
        public void Resume(ThreadContext hj)
        { 

        }
    }
}
namespace Designmode.State
{
    /// <summary>
    /// 线程死亡状态
    /// </summary>
    public class DeadState:ThreadState
    {
        /// <summary>
        /// 构造一个死亡状态实例
        /// </summary>
        public DeadState()
        {
            this.Name = "Dead";
        }
        /// <summary>
        /// 线程死亡
        /// </summary>
        public void Dead()
        {

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值