zoukankan      html  css  js  c++  java
  • CSharp设计模式读书笔记(21):状态模式(学习难度:★★★☆☆,使用频率:★★★☆☆)

    模式角色与结构:

    示例代码:(本示例在具体状态类中实现状态切换,也可以在环境类中实现状态切换。状态模式一定程度上违背开闭原则)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CSharp.DesignPattern.StatePattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                Account account = new Account(0.0);
                account.Deposit(1000); // 1000
                account.Withdraw(2000); // -1000
                account.Deposit(3000); // 2000
                account.Withdraw(4000); // -2000
                account.Withdraw(1000); // -3000
                account.ComputeInterest();
                Console.ReadLine();
            }
        }
    
        // 环境类
        class Account 
        {
            public Account(double balance)
            {
                this._balance = balance;
                this._state = new NormalState(this);
            }
    
            public void Deposit(double amount)
            {
                _state.Deposit(amount);
            }
    
            public void Withdraw(double amount)
            {
                _state.Withdraw(amount);
            }
    
            public void ComputeInterest()
            {
                _state.ComputeInterest();
            }
    
            public double Balance
            {
                get { return _balance; }
                set { _balance = value; }
            }
            internal AccountState State
            {
                get { return _state; }
                set { _state = value; }
            }
    
            private double _balance = 0;
            private AccountState _state;
        }
    
        // 抽象状态类
        abstract class AccountState
        {
            protected Account _account;
    
            public virtual void Deposit(double amount)
            {
                _account.Balance += amount;
                StateCheck();
            }
            public virtual void Withdraw(double amount)
            {
                _account.Balance -= amount;
                StateCheck();
            }
            public abstract void ComputeInterest();
            public abstract void StateCheck();
        }
    
        // 具体状态类
        class NormalState : AccountState
        {
            public NormalState(Account account)
            {
                this._account = account;
            }
    
            public override void ComputeInterest() 
            {
                Console.WriteLine("No interest...");
            }
            // 状态转换
            public override void StateCheck()
            {
                if (_account.Balance > -2000 && _account.Balance <= 0)
                {
                    _account.State = new OverdraftState(this._account);
                    Console.WriteLine("OverdraftState...");
                }
                else if (_account.Balance == -2000)
                {
                    _account.State = new RestrictedState(this._account);
                    Console.WriteLine("RestrictedState...");
                }
                else if (_account.Balance < -2000)
                {
                    Console.WriteLine("Forbidden...");
                }
            }
        }
    
        // 具体状态类
        class OverdraftState : AccountState
        {
            public OverdraftState(Account account)
            {
                this._account = account;
            }
    
            public override void ComputeInterest()
            {
                Console.WriteLine("Compute interest...");
            }
            // 状态转换
            public override void StateCheck()
            {
                if (_account.Balance > 0)
                {
                    _account.State = new NormalState(this._account);
                    Console.WriteLine("NormalState...");
                }
                else if (_account.Balance == -2000)
                {
                    _account.State = new RestrictedState(this._account);
                    Console.WriteLine("RestrictedState...");
                }
                else if (_account.Balance < -2000)
                {
                    Console.WriteLine("Forbidden...");
                }
            }
        }
    
        // 具体状态类
        class RestrictedState : AccountState
        {
            public RestrictedState(Account account)
            {
                this._account = account;
            }
    
            public override void Withdraw(double amount)
            {
                Console.WriteLine("Account is restricted, withdraw failed...");
            }
    
            public override void ComputeInterest()
            {
                Console.WriteLine("Compute interest...");
            }
            // 状态转换
            public override void StateCheck()
            {
                if (_account.Balance > 0)
                {
                    _account.State = new NormalState(this._account);
                    Console.WriteLine("NormalState...");
                }
                else if (_account.Balance > -2000)
                {
                    _account.State = new OverdraftState(this._account);
                    Console.WriteLine("OverdraftState...");
                }
            }
        }
    }
  • 相关阅读:
    测试markdown
    Ubuntu 部署 k8s集群
    HTML 表格 各标签使用的标准顺序(心得)
    javascript event(事件对象)详解
    CSS3选择器归类整理
    PHP开发中session无法获取和保存问题解决方法
    表单脚本
    PHP页面跳转三种实现方法
    PHP中关于时间(戳)、时区、本地时间、UTC时间等的梳理
    JS前端将table导出到excel 兼容谷歌 IE 且保留表格样式
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993796.html
Copyright © 2011-2022 走看看