意圖
允許一個對象在其內部狀態改變時改變它的行為。對象看起來似乎修改了它的類。
場景
我們在制作一個網上書店的網站,用戶在書店買了一定金額的書後可以升級為銀會員、黃金會員,不同等級的會員購買書籍有不同的優惠。你可能會想到可以在User類的BuyBook方法中判斷用戶歷史消費的金額來給用戶不同的折扣,在GetUserLevel方法中根據用戶歷史消費的金額來輸出用戶的等級。帶來的問題有三點:
l 不用等級的用戶給予的優惠比率是經常發生變化的,一旦變化是不是就要修改User類呢?
l 網站在初期可能最高級別的用戶是黃金會員,而隨著用戶消費金額的累計,我們可能要增加鑽石、白金等會員類型,這些會員的折扣又是不同的,發生這樣的變化是不是又要修改User類了呢?
l 拋開變化不說,User類承擔了用戶等級判斷、購買折扣計算等復雜邏輯,復雜的User類代碼的可維護性會不會很好呢?
由此引入State模式,通過將對象和對象的狀態進行分離,把對象狀態的轉化以及由不同狀態產生的行為交給具體的狀態類去做,解決上述問題。
示例代碼
using System;
using System.Collections.Generic;
using System.Text;
namespace StateExample
{
class Program
{
static void Main(string[] args)
{
User user = new User("zhuye");
user.BuyBook(2000);
user.BuyBook(2000);
user.BuyBook(2000);
user.BuyBook(2000);
}
}
class User
{
private UserLevel userLevel ;
public UserLevel UserLevel
{
get { return userLevel ; }
set { userLevel = value; }
}
private string userName;
private double paidMoney;
public double PaidMoney
{
get { return paidMoney; }
}
public User(string userName)
{
this.userName = userName;
this.paidMoney = 0;
this.UserLevel = new NormalUser(this);
}
public void BuyBook(double amount)
{
Console.WriteLine(string.Format("Hello {0}, You have paid ${1}, You Level is {2}.", userName, paidMoney, userLevel .GetType().Name));
double realamount = userLevel .CalcRealAmount(amount);
Console.WriteLine("You only paid $" + realamount + " for this book.");
paidMoney += realamount;
userLevel .StateCheck();
}
}
abstract class UserLevel
{
protected User user;
public UserLevel (User user)
{
this.user = user;
}
public abstract void StateCheck();
public abstract double CalcRealAmount(double amount);
}
class DiamondUser : UserLevel
{
public DiamondUser(User user)
: base(user) { }
public override double CalcRealAmount(double amount)
{
return amount * 0.7;
}
public override void StateCheck()
{
}
}
class GoldUser : UserLevel
{
public GoldUser(User user)
: base(user) { }
public override double CalcRealAmount(double amount)
{
return amount * 0.8;
}
public override void StateCheck()
{
if (user.PaidMoney > 5000)
user.UserLevel = new DiamondUser(user);
}
}
class SilverUser : UserLevel
{
public SilverUser(User user)
: base(user) { }
public override double CalcRealAmount(double amount)
{
return amount * 0.9;
}
public override void StateCheck()
{
if (user.PaidMoney > 2000)
user.UserLevel = new GoldUser(user);
}
}
class NormalUser : UserLevel
{
public NormalUser(User user)
: base(user) { }
public override double CalcRealAmount(double amount)
{
return amount * 0.95;
}
public override void StateCheck()
{
if (user.PaidMoney > 1000)
user.UserLevel = new SilverUser(user);
}
}
}