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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - AlanPaoPao

23)Visitor 22)Template 21)Strategy 19)Observer 18)Memento 17)Mediator 16)Iterator 15)Interpreter 14)Command 13)Chain Of Responsibility 12)Proxy 11)Flyweight 10)Facade 09)Decorator 08)Composite 07)Bridge 06)Adapter 05)Prototype 04)Factory Method
20)State
AlanPaoPao · 2007-09-21 · via 博客园 - AlanPaoPao

    状态模式的目的是: 引入一组表示对象状态的类,把于状态相关的代码逻辑分配到这些类中
    实例代码:

class Account
{
  
private State state;
  
private string owner;
  
public Account(string owner)
  
{
    
this.owner = owner;
    state 
= new SilverState(0.0this);
  }

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

}

abstract class State
{
  
protected Account account;
  
protected double balance;
  
protected double interest;
  
protected double lowerLimit;
  
protected double upperLimit;
  
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();
}

class RedState : State
{
  
double serviceFee;
  
public RedState(State state)
  
{
    
this.balance = state.Balance;
    
this.account = state.Account;
    Initialize();
  }

  
private void Initialize()
  
{
    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()
  
{
  }

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

  }

}

class SilverState : State
{
  
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()
  
{
    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);
    }

  }

}

class GoldState : State
{
  
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()
  
{
    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);
    }

  }

}

class MainApp
{
  
static void Main()
  
{
    Account account 
= new Account("Jim Johnson");
    account.Deposit(
500.0);
    account.Deposit(
300.0);
    account.Deposit(
550.0);
    account.PayInterest();
    account.Withdraw(
2000.00);
    account.Withdraw(
1100.00);
    Console.Read();
  }

}