UML图

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YJY
{
    class Account
    {
        public AccountState State { get; set; }
        public string Owner { get; set; }
        public double Balance { get; set; }
        public Account(string owner, double initialAmount)
        {
            Owner = owner;
            Balance = initialAmount;
            State = new 访客(this);
        }
        public void SetBalance(double amount)
        {
            Balance = amount;
        }
        public void Deposit(double amount)
        {
            Console.WriteLine("存款 {0}.", amount);
            State.Deposit(amount);
            Console.WriteLine("客户状态变更为 {0}", State);
        }
        public void Consume(double amount)
        {
            Console.WriteLine("消费 {0}.", amount);
            State.Consume(amount);
            Console.WriteLine("客户状态变更为 {0}", State);
        }
    }
    abstract class AccountState
    {
        protected Account Account;
        public abstract void Deposit(double amount);
        public abstract void Consume(double amount);
        public abstract void Check();
    }
    class 访客 : AccountState
    {
        public 访客(Account account)
        {
            Account = account;
        }
        public override void Deposit(double amount)
        {
            Account.Balance += amount;
            Console.WriteLine("向" + Account.Owner + "账户中存入" + amount + ",账户余额为" + Account.Balance);
            Check();
        }
        public override void Consume(double amount)
        {
            double newBalance = Account.Balance - amount;
            Account.Balance -= amount;
            Console.WriteLine("在" + Account.Owner + "账户中消费" + amount + ",账户余额为" + Account.Balance);
            Check();
        }
        public override void Check()
        {
            if (Account.Balance > 100 && Account.Balance < 1000)
            {
                Account.State = new 会员(Account);
            }
            else if (Account.Balance >= 1000)
            {
                Account.State = new 贵宾(Account);
            }
        }
    }
    class 会员 : AccountState
    {
        public 会员(Account account)
        {
            Account = account;
        }
        public override void Deposit(double amount)
        {
            Account.Balance += amount;
            Console.WriteLine("向" + Account.Owner + "账户中存入" + amount + ",账户余额为" + Account.Balance);
            Check();
        }
        public override void Consume(double amount)
        {
            double newBalance = Account.Balance - amount;
            Account.Balance -= amount;
            Console.WriteLine("在" + Account.Owner + "账户中消费" + amount + ",账户余额为" + Account.Balance);
            Check();
        }
        public override void Check()
        {
            if (Account.Balance > 0 && Account.Balance <= 100)
            {
                Account.State = new 访客(Account);
            }
            else if (Account.Balance >= 1000)
            {
                Account.State = new 贵宾(Account);
            }
        }
    }
    class 贵宾 : AccountState
    {
        public 贵宾(Account account)
        {
            Account = account;
        }
        public override void Deposit(double amount)
        {
            Account.Balance += amount;
            Console.WriteLine("向"+Account.Owner+"账户中存入"+amount+",账户余额为"+Account.Balance);
            Check();
        }
        public override void Consume(double amount)
        {
            double newBalance = Account.Balance - amount;
            Account.Balance -= amount;
            Console.WriteLine("在" + Account.Owner + "账户中消费" + amount + ",账户余额为" + Account.Balance);
            Check();
        }
        public override void Check()
        {
            if (Account.Balance > 100 && Account.Balance < 1000)
            {
                Account.State = new 会员(Account);
            }
            else if (Account.Balance > 0 && Account.Balance <= 100)
            {
                Account.State = new 访客(Account);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Account account = new Account("YJY", 0);
            Console.WriteLine("用户名:"+account.Owner);
            Console.WriteLine("初始金额:"+account.Balance);
            Console.WriteLine("------------------");
            account.Deposit(50);
            Console.WriteLine("-------------------");
            account.Deposit(200);
            Console.WriteLine("-----------------------------------------");
            account.Deposit(800);
            Console.WriteLine("-----------------------------------------");
            account.Consume(100);
            Console.WriteLine("---------------------");
            account.Consume(900);
            Console.WriteLine("-----------------------------------------");
        }
    }
}
运行结果

原文:http://www.cnblogs.com/Yang-jy/p/5093583.html