Unity+C#状态模式——案例1(图文详解+源码)

Unity+C#状态模式——案例1(图文详解+源码)_第1张图片

高清4K无水印原图,点赞评论免费找我来领啦!!!

Unity+C#状态模式——案例1(图文详解+源码)

  • 前言
  • 一、什么是状态模式
  • 二、案例的思维导图
  • 三、案例的源码
  • 总结
  • 版权声明


前言

几天前无意间翻到了毛星云大佬的文章,其中提到了状态模式,我就顺藤摸瓜的找到了github的库,这篇文章是对状态模式的第一个案例的理解和整理。


一、什么是状态模式

设计模式的一种,定义是:当一个对象内在状态改变时允许其改变行为,这个对象看起来像改变了其类。
感觉不太好理解,还是让我们来看案例吧。

二、案例的思维导图

案例主要是与银行账户,存取钱,相关
自己整理一份思维导图,大概就是整个案例的关系
Unity+C#状态模式——案例1(图文详解+源码)_第2张图片

三、案例的源码

在上源码之前,可以先看下整个的结构还是相当清晰的
Unity+C#状态模式——案例1(图文详解+源码)_第3张图片
下面是源码,在大佬的基础上添加了更详细的注释,便于以后复习

//-------------------------------------------------------------------------------------
//	StateExample1.cs
//-------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;

//This real-world code demonstrates the State pattern which allows an Account to behave differently depending on its balance. 
//The difference in behavior is delegated to State objects called RedState, SilverState and GoldState. 
//These states represent overdrawn accounts, starter accounts, and accounts in good standing.

namespace StateExample1
{
    public class StateExample1 : MonoBehaviour
    {
        void Start()
        {
            // Open a new account
            Account account = new Account("Jim Johnson");

            // Apply financial transactions
            account.Deposit(500.0);
            account.Deposit(300.0);
            account.Deposit(550.0);
            account.PayInterest();
            account.Withdraw(2000.00);
            account.Withdraw(1100.00);
        }
    }

    /// 
    /// The 'State' abstract class,状态  的抽象类
    /// 
    abstract class State
    {
        /// 
        /// 账户
        /// 
        protected Account account;
        /// 
        /// 余款
        /// 
        protected double balance;
        /// 
        /// 利息
        /// 
        protected double interest;
        /// 
        /// 下限
        /// 
        protected double lowerLimit;
        /// 
        /// 上限
        /// 
        protected double upperLimit;

        // Properties
        public Account Account
        {
            get { return account; }
            set { account = value; }
        }

        public double Balance
        {
            get { return balance; }
            set { balance = value; }
        }
        
        /// 
        /// 存钱
        /// 
        /// 数量
        public abstract void Deposit(double amount);
        /// 
        /// 取钱
        /// 
        /// 数量
        public abstract void Withdraw(double amount);
        /// 
        /// 支付利息
        /// 
        public abstract void PayInterest();
    }


    /// 
    /// A 'ConcreteState' class 透支状态:一个具体状态类
    /// 
    /// Red indicates that account is overdrawn 
    /// 
    /// 
    class RedState : State
    {
        private double _serviceFee;

        // Constructor构造函数
        public RedState(State state)
        {
            this.balance = state.Balance;
            this.account = state.Account;
            Initialize();
        }

        private void Initialize()
        {
            // Should come from a datasource
            interest = 0.0;
            lowerLimit = -100.0;
            upperLimit = 0.0;
            _serviceFee = 15.00;
        }

        public override void Deposit(double amount)
        {
            balance += amount;
            StateChangeCheck();
        }

        public override void Withdraw(double amount)
        {
            amount = amount - _serviceFee;
            Debug.Log("No funds available for withdrawal!");
        }

        public override void PayInterest()
        {
            // No interest is paid
        }

        private void StateChangeCheck()
        {
            if (balance > upperLimit)
            {
                account.State = new SilverState(this);
            }
        }
    }

    /// 
    /// A 'ConcreteState' class  没有利息的状态:一个具体的状态类
    /// 
    /// Silver indicates a non-interest bearing state
    /// 
    /// 
    class SilverState : State
    {
        // Overloaded constructors
        /// 
        /// 没有利息的构造函数(状态)
        /// 
        /// 
        public SilverState(State state) :
          this(state.Balance, state.Account)
        {
        }
        /// 
        /// 没有利息的构造函数(余额,账户)
        /// 
        /// 
        /// 
        public SilverState(double balance, Account account)
        {
            this.balance = balance;
            this.account = account;
            Initialize();
        }

        private void Initialize()
        {
            // Should come from a datasource
            interest = 0.0;
            lowerLimit = 0.0;
            upperLimit = 1000.0;
        }

        public override void Deposit(double amount)
        {
            balance += amount;
            StateChangeCheck();
        }

        public override void Withdraw(double amount)
        {
            balance -= amount;
            StateChangeCheck();
        }

        public override void PayInterest()
        {
            balance += interest * balance;
            StateChangeCheck();
        }
        /// 
        /// 检查当前状态的方法
        /// 
        private void StateChangeCheck()
        {
            if (balance < lowerLimit)
            {
                account.State = new RedState(this);
            }
            else if (balance > upperLimit)
            {
                account.State = new GoldState(this);
            }
        }
    }

    /// 
    /// A 'ConcreteState' class  有利息的状态:一个具体的状态类
    /// 
    /// Gold indicates an interest bearing state
    /// 
    /// 
    class GoldState : State
    {
        // Overloaded constructors
        /// 
        /// 构造函数,调用有两个参数的构造函数
        /// 
        /// 
        public GoldState(State state)
          : this(state.Balance, state.Account)
        {
        }

        public GoldState(double balance, Account account)
        {
            this.balance = balance;
            this.account = account;
            Initialize();
        }

        private void Initialize()
        {
            // Should come from a database
            interest = 0.05;
            lowerLimit = 1000.0;
            upperLimit = 10000000.0;
        }

        public override void Deposit(double amount)
        {
            balance += amount;
            StateChangeCheck();
        }

        public override void Withdraw(double amount)
        {
            balance -= amount;
            StateChangeCheck();
        }

        public override void PayInterest()
        {
            balance += interest * balance;
            StateChangeCheck();
        }

        private void StateChangeCheck()
        {
            if (balance < 0.0)
            {
                account.State = new RedState(this);
            }
            else if (balance < lowerLimit)
            {
                account.State = new SilverState(this);
            }
        }
    }

    /// 
    /// The 'Context' class,账户  类
    /// 
    class Account
    {
        private State _state;
        private string _owner;

        // Constructor
        /// 
        /// 账户类的构造函数
        /// 
        /// 
        public Account(string owner)
        {
            // New accounts are 'Silver' by default
            this._owner = owner;
            this._state = new SilverState(0.0, this);
        }

        //to fix the private field "_owner' is assigned but its value is never used warning
        public string GetOwner()
        {
            return _owner;
        }

        // Properties
        public double Balance
        {
            get { return _state.Balance; }
        }

        public State State
        {
            get { return _state; }
            set { _state = value; }
        }
        /// 
        /// 账户类的 存钱方法
        /// 
        /// 
        public void Deposit(double amount)
        {
            _state.Deposit(amount);
            Debug.Log("Deposited存钱 " + amount + "---");
            Debug.Log(" Balance余额 = " + this.Balance);
            Debug.Log(" Status账户状态 = " + this.State.GetType().Name);
            Debug.Log("");
        }
        /// 
        /// 账户类的 取钱方法
        /// 
        /// 
        public void Withdraw(double amount)
        {
            _state.Withdraw(amount);
            Debug.Log("Withdraw取钱 " + amount + "---");
            Debug.Log(" Balance = " + this.Balance);
            Debug.Log(" Status = " + this.State.GetType().Name + "\n");
        }
        /// 
        /// 账户类的 支付利息方法
        /// 
        public void PayInterest()
        {
            _state.PayInterest();
            Debug.Log("Interest Paid支付利息 --- ");
            Debug.Log(" Balance = " + this.Balance);
            Debug.Log(" Status = " + this.State.GetType().Name);

        }
    }
}


总结

欢迎大佬多多来给萌新指正,欢迎大家来共同探讨。
如果各位看官觉得文章有点点帮助,跪求各位给点个“一键三连”,谢啦~

声明:本博文章若非特殊注明皆为原创原文链接
https://blog.csdn.net/Wrinkle2017/article/details/124155068
————————————————————————————————

版权声明

版权声明:本博客为非营利性个人原创
所刊登的所有作品的著作权均为本人所拥有
本人保留所有法定权利,违者必究!
对于需要复制、转载、链接和传播博客文章或内容的
请及时和本博主进行联系
对于经本博主明确授权和许可使用文章及内容的
使用时请注明文章或内容出处并注明网址
转载请附上原文出处链接及本声明

你可能感兴趣的:(Unity3d,C#,unity手游开发,游戏开发,c#,设计模式,状态模式,毛星云)