zoukankan      html  css  js  c++  java
  • 用C#改写Head First Design PatternsState(原创)

    状态模式

    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();
            }
        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    mysql数据库(1)
    通过全局异常处理机制实现接口参数校验返回指定返回类型
    http接口安全校验
    java 锁机制介绍
    通过反射获取类的所有属性值拼接成字符串工具类
    Mybatis中出现java.sql.SQLException: 无效的列类型: 1111
    判断两个Long相等
    jwt工具类
    mybatis #{}和${}的区别是什么
    报错解决NoSuchMethod。。。
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519236.html
Copyright © 2011-2022 走看看