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...");
                }
            }
        }
    }
  • 相关阅读:
    MTputty设置字体 MTputty菜单栏隐藏显示
    Ubuntu安装SSH服务
    LeetCode--Text Justification
    海量数据处理--hash和bit-map
    海量数据处理--bloom filter
    SSH原理简介(转)
    Ubuntu12.04 安装android集成环境(xserver被卸载)
    内存对齐(转)
    Ubuntu12.04 安装(无法将 grub-efi 软件包安装到/target/中,如果没有 GRUB 启动引导期,所安装的系统无法启动)
    算法题--等概率产生0和1(有扩展)
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993796.html
Copyright © 2011-2022 走看看