惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs

博客园 - 点点滴滴

C#操作IIS的代码 恢复误删数据(SQL Server 2000)--Log Explorer 如何让ClickOnce进行手动更新(含代码) - 点点滴滴 - 博客园 BackgroundWorker 组件 获取VS.NET 自带的数据库连接对话框的数据库连接 搜索一个局域网中所有的SQL Server服务器 Application.DoEvent() 在C#使用XML注释 用IDisposable接口释放.NET资源 正确的重载operator 很好的debug有理由不用吗 C#调用API访问其它进程 抽象 虚方法 接口 的区别 ASP.NET AJAX 路线图 ASP.NET AJAX 概述 安装ASP.NET AJAX Visitor Template Method Strategy
State
点点滴滴 · 2006-10-29 · via 博客园 - 点点滴滴

using System;

namespace DoFactory.GangOfFour.State.RealWorld
{
  
  // MainApp test application

class MainApp
  {
    static void Main()
    {
      // 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);

// Wait for user
      Console.Read();
    }
  }

// "State"

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();
  }

// "ConcreteState"

// Account is overdrawn

class RedState : State
  {
    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;
      Console.WriteLine("No funds available for withdrawal!");
    }

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

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

// "ConcreteState"

// Silver is 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);
      }
    }
  }

// "ConcreteState"

// 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);
      }
    }
  }

// "Context"

class Account
  {
    private State state;
    private string owner;

// Constructor
    public Account(string owner)
    {
      // New accounts are 'Silver' by default
      this.owner = owner;
      state = new SilverState(0.0, this);
    }

// 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);
      Console.WriteLine("Deposited {0:C} --- ", amount);
      Console.WriteLine(" Balance = {0:C}", this.Balance);
      Console.WriteLine(" Status = {0}\n" ,
        this.State.GetType().Name);
      Console.WriteLine("");
    }

public void Withdraw(double amount)
    {
      state.Withdraw(amount);
      Console.WriteLine("Withdrew {0:C} --- ", amount);
      Console.WriteLine(" Balance = {0:C}", this.Balance);
      Console.WriteLine(" Status = {0}\n" ,
        this.State.GetType().Name);
    }

public void PayInterest()
    {
      state.PayInterest();
      Console.WriteLine("Interest Paid --- ");
      Console.WriteLine(" Balance = {0:C}", this.Balance);
      Console.WriteLine(" Status = {0}\n" ,
        this.State.GetType().Name);
    }
  }
}