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
  • 相关阅读:
    spark SQL之 DataFrame和DataSet
    scala之 保留小数
    spark之 避免数据倾斜之 给名字分区(百家姓)
    hive之 'client_protocol' is unset!
    hive之报错:ls:cannot access '/usr/local/spark/lib/spark-assembly-*.jar':No such file or directory
    hive之 Error: Duplicate key name 'PCS_STATS_IDX' (state=42000,code=1061) ----Hive schematool -initSchema
    Maven中需要注意的点
    spark之 Idea操作
    scala之 一句话打印三角形
    scala 之 BaseDao
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519236.html
Copyright © 2011-2022 走看看