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...");
                }
            }
        }
    }
  • 相关阅读:
    Linux 如何查看当前目录
    Docker快速入手实战笔记
    【ssh】ssh登录出现‘The authenticity of host ‘IP’ can't be established.’的问题
    【AFL(七)】afl-fuzz.c小改——输出文件夹暂存
    【steam】Steam背景美化——长展柜终极指南
    【AFL(六)】AFL源码中的那些头文件
    【AFL(五)】文件变异策略
    【Latex】详细的简易教程——写在论文开始之前
    【Latex】论文写作工具:VScode 2019 + latex workshop
    【AFL(四)】afl-cmin修改:文件夹相关操作鲁棒性
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993796.html
Copyright © 2011-2022 走看看