现在的位置: 首页 > 综合 > 正文

c# 状态模式 小试牛刀

2013年10月26日 ⁄ 综合 ⁄ 共 5699字 ⁄ 字号 评论关闭

状态模式:  环境类 + 抽象状态类  +具体状态类

环境类

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

namespace StateDesignModelTraining
{
    public class Player
    {
        private Level currentLevel;
        public Level CurrentLevel
        {
            get { return currentLevel; }
            set { currentLevel = value; }
        }

        private int score;
        public int Score
        {
            get { return score; }
            set
            {
                score = value;
                score = Math.Max(score,0);
            }
        }

        private string name;
       

        public Player(string name)
        {
            this.name = name;
            this.CurrentLevel = new PrimaryLevel(this);
            this.Score = 0;
            Console.WriteLine("玩家:{0},积分:{1},等级:{2}", this.name, this.score, this.CurrentLevel.GetType().Name);
        }

        public void Win(int _score)
        {
            this.CurrentLevel.AddScore(_score);
            Console.WriteLine("胜利一场获得积分{0},现有积分{1},玩家等级为{2}",_score, this.Score, this.CurrentLevel.GetType().Name);
            this.CurrentLevel.Display();
            Console.WriteLine("------------------------------------------");
        }

        public void Fail(int _score)
        {
            this.CurrentLevel.RedouceScore(_score);
            Console.WriteLine("失败一场扣除积分{0},现有积分{1},玩家等级为{2}", _score, this.Score, this.CurrentLevel.GetType().Name);
            this.CurrentLevel.Display();
            Console.WriteLine("------------------------------------------");
        }

        public void Play()
        {
            Console.WriteLine("游戏基本功能");
        }

        public void DoubleScore()
        {
            Console.WriteLine("积分加倍功能");
        }

        public void ChangeCards()
        {
            Console.WriteLine("换牌功能");
        }

        public void PeekCards()
        {
            Console.WriteLine("偷看他人牌的功能");
        }

    }
}

抽象状态类

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

namespace StateDesignModelTraining
{
    public abstract class Level
    {
        protected Player Player;
        public Level(Player _player)
        {
            this.Player = _player;
        }
        public abstract void StartCheck();

        public void AddScore(int _score)
        {
            this.Player.Score += _score;
            this.StartCheck();
        }

        public  void RedouceScore(int _score)
        {
            this.Player.Score -= _score;
            this.StartCheck();
        }

        public abstract void Display();
    }
}

具体状态类

namespace StateDesignModelTraining
{
    public class PrimaryLevel:Level
    {
        public PrimaryLevel(Player _player)
            :base(_player)
        {
        
        }

        public override void StartCheck()
        {
            if(this.Player.Score > 300)
            {
                this.Player.CurrentLevel = new FinalLevel(this.Player);
            }
            else if (this.Player.Score > 200)
            {
                this.Player.CurrentLevel = new ProfessionalLevel(this.Player);
            }
            else if (this.Player.Score > 100)
            {
                this.Player.CurrentLevel = new SecondaryLevel(this.Player);
            }
        }

        public override void Display()
        {
            this.Player.Play();
        }
    }
}

namespace StateDesignModelTraining
{
    public class SecondaryLevel:Level
    {
        public SecondaryLevel(Player _player)
            :base(_player)
        {
        
        }

        public override void StartCheck()
        {
            if (this.Player.Score > 300)
            {
                this.Player.CurrentLevel = new FinalLevel(this.Player);
            }
            else if (this.Player.Score > 200)
            {
                this.Player.CurrentLevel = new ProfessionalLevel(this.Player);
            }
            else if (this.Player.Score <= 100)
            {
                this.Player.CurrentLevel = new PrimaryLevel(this.Player);
            }
        }

        public override void Display()
        {
            this.Player.Play();
            this.Player.DoubleScore();
        }
    }
}

namespace StateDesignModelTraining
{
    public class ProfessionalLevel:Level
    {
        public ProfessionalLevel(Player _player)
            :base(_player)
        {
        
        }

        public override void StartCheck()
        {
            if (this.Player.Score > 300)
            {
                this.Player.CurrentLevel = new FinalLevel(this.Player);
            }
            else if (this.Player.Score <= 200 && this.Player.Score > 100)
            {
                this.Player.CurrentLevel = new SecondaryLevel(this.Player);
            }
            else if (this.Player.Score <= 100)
            {
                this.Player.CurrentLevel = new PrimaryLevel(this.Player);
            }
        }

        public override void Display()
        {
            this.Player.Play();
            this.Player.DoubleScore();
            this.Player.ChangeCards();
        }
    }
}

namespace StateDesignModelTraining
{
    public class FinalLevel:Level
    {
        public FinalLevel(Player _player)
            :base(_player)
        {
        
        }

        public override void StartCheck()
        {
            if (this.Player.Score > 200 && this.Player.Score <= 300)
            {
                this.Player.CurrentLevel = new ProfessionalLevel(this.Player);
            }
            else if (this.Player.Score > 100)
            {
                this.Player.CurrentLevel = new SecondaryLevel(this.Player);
            }
            else if (this.Player.Score <= 100)
            {
                this.Player.CurrentLevel = new PrimaryLevel(this.Player);
            }
        }

        public override void Display()
        {
            this.Player.Play();
            this.Player.DoubleScore();
            this.Player.ChangeCards();
            this.Player.PeekCards();
        }
    }
}

客户端

{
    class Program
    {
        static void Main(string[] args)
        {
            Player _player = new Player("东方不败");
            _player.Win(200);
            _player.Fail(100);
            _player.Win(300);
            _player.Fail(150);
        }
    }
}

抱歉!评论已关闭.