用C#改写Head First Design Patterns--State(原创)

本文通过一个自动贩卖机的具体案例,详细介绍了状态模式的设计思路与实现方式。该案例涉及多个具体状态类,如投入硬币状态、售罄状态、已售状态等,并通过状态之间的转换展示状态模式的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

状态模式

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public interface State
    {
        //投入25分硬币
         void insertQuarter();
        //退出硬币
         void ejectQuarter();
        //推把手
         void turnCrank();
        //得到糖果
         void dispense();
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public class NoQuarterState : State
    {
        GumballMachine gm;

        public NoQuarterState(GumballMachine g)
        {
            this.gm = g;
        }

        #region State 成员

        void State.insertQuarter()
        {
            System.Console.WriteLine("您刚才投入了25分钱");
            gm.setState(gm.hasQuarterState);
        }

        void State.ejectQuarter()
        {
            System.Console.WriteLine("您没有投币");
        }

        void State.turnCrank()
        {
            System.Console.WriteLine("您没有投币");
        }

        void State.dispense()
        {
            System.Console.WriteLine("您没有投币");
        }

        #endregion
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public class soldOutState :State
    {
        GumballMachine gm;

        public soldOutState(GumballMachine g)
        {
            this.gm = g;
        }

        #region State 成员

        void State.insertQuarter()
        {
            System.Console.WriteLine("已售完,不接受投币");
        }

        void State.ejectQuarter()
        {
            System.Console.WriteLine("已售完,不接受退币");
        }

        void State.turnCrank()
        {
            System.Console.WriteLine("已售完!不可摇杆");
        }

        void State.dispense()
        {
            System.Console.WriteLine("已售完!");
        }

        #endregion
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public class SoldState:State
    {
        GumballMachine gm;

        public SoldState(GumballMachine g)
        {
            this.gm = g;
        }

        #region State 成员

        void State.insertQuarter()
        {
            System.Console.WriteLine("等待出果,不得投币");
        }

        void State.ejectQuarter()
        {
            System.Console.WriteLine("等待出果,不得退币");
        }

        void State.turnCrank()
        {
            System.Console.WriteLine("等待出果,不得摇两次");
        }

        void State.dispense()
        {
            //System.Console.WriteLine("您的糖果,请笑纳");

            //gm.releaseBall();

            if (gm.count > 0)
            {
                gm.setState(gm.noQuarterState);
            }
            else
            {
                gm.setState(gm.soldOutState);
            }
        }

        #endregion
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public class WinnerState:State
    {
        GumballMachine gm;

        public WinnerState(GumballMachine g)
        {
            this.gm = g;
        }

        #region State 成员

        void State.insertQuarter()
        {
            System.Console.WriteLine("等待出果,不得投币");
        }

        void State.ejectQuarter()
        {
            System.Console.WriteLine("等待出果,不得退币");
        }

        void State.turnCrank()
        {
            System.Console.WriteLine("等待出果,不得摇两次");
        }

        void State.dispense()
        {
            System.Console.WriteLine("您赢了一颗果!");
            if (gm.count > 0)
            {
                gm.setState(gm.noQuarterState);
            }
            else
            {
                gm.setState(gm.soldOutState);
            }
        }

        #endregion
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public  class GumballMachine
    {
        public State soldOutState;
        public State noQuarterState;
        public State hasQuarterState;
        public State soldState;
        public State winnerState;

        State state ;
       
        //糖果数量
        public int count = 0;

        public GumballMachine(int num)
        {
            soldOutState = new soldOutState(this);
            noQuarterState = new NoQuarterState(this);
            hasQuarterState = new hasQuarterState(this);
            soldState = new SoldState(this);
            soldState = new WinnerState(this);
            if (num > 0) this.count = num;

            state = noQuarterState;

   
        }

        public void insert()
        {
            state.insertQuarter();
        }
        public void eject()
        {
            state.ejectQuarter();
        }

        public void turn()
        {
            state.turnCrank();
        }


        public void setState(State s)
        {
            this.state = s;
        }

        public void releaseBall()
        {
            if (state == soldState)
            {
                System.Console.WriteLine("箱子提醒您:请查收糖果!");
                state.dispense();
            }
            else
            {
                System.Console.WriteLine("箱子提醒您:摇杆后取果!");
            }

            if (count != 0)
            {
                count--;
            }

        }

    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    public class hasQuarterState : State
    {
        GumballMachine gm;

        public hasQuarterState(GumballMachine g)
        {
            this.gm = g;
        }

        #region State 成员

        void State.insertQuarter()
        {
            System.Console.WriteLine("您已经投了25分钱,不用再投");
        }

        void State.ejectQuarter()
        {
            System.Console.WriteLine("这是您的25分,请收取");
            gm.setState(gm.noQuarterState);
        }

        void State.turnCrank()
        {
            System.Console.WriteLine("请等待糖果");
            gm.setState(gm.soldState);
        }

        void State.dispense()
        {
            System.Console.WriteLine("请等待糖果!");
        }

        #endregion
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    class Program
    {
        static void Main(string[] args)
        {
            GumballMachine g = new GumballMachine(5);

            g.insert();
            g.eject();
            g.turn();
            g.releaseBall();
            g.turn();

            g.insert();
            g.turn();
            g.releaseBall();

            g.insert();
            g.turn();
            g.releaseBall();

            g.insert();
            g.turn();
            g.releaseBall();

            g.insert();
            g.turn();
            g.releaseBall();

            g.insert();
            g.turn();
            g.releaseBall();

            g.insert();
            g.turn();
            g.releaseBall();

            System.Console.ReadLine();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值