码迷,mamicode.com
首页 > 其他好文 > 详细

《软件架构与设计模式》关于 状态模式 的一个小例子

时间:2016-01-01 14:54:06      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

作业题目

技术分享

1、画UMl图

 技术分享

2、写代码

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 状态
{
class Program
{
static void Main(string[] args)
{
Club account = new Club("张三", 0);
Console.WriteLine("开户成功,姓名:" + account.Owner + ",初始金额:" + account.Balance);
Console.WriteLine("------------------------");
account.Deposit(50);
Console.WriteLine("------------------------");
account.Deposit(100);
Console.WriteLine("------------------------");
Console.ReadLine();

}
// AccountState类


public abstract class AccountState
{
protected Club Account;
//存款
public abstract void Deposit(double amount);
//消费
public abstract void Withdraw(double amount);
//检查账户状态
public abstract void StateChange();

}
  

//VistorState类


public class VistorState : AccountState
{
public VistorState(Club account)
{
Account = account;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
Console.WriteLine("向名为"+Account.Owner+"的账户中存款"+amount+"元,存款后账户余额为"+Account.Balance+"元");
StateChange();
}
public override void Withdraw(double amount)
{
double newBalance = Account.Balance - amount;
if (newBalance > 0)
{
Account.Balance = newBalance;
Console.WriteLine("向名为" + Account.Owner + "的账户中消费" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
else
{
Console.WriteLine("你的账户余额不足,无法消费,请先存款");
}
StateChange();
}
public override void StateChange()
{
if (Account.Balance > 100 && Account.Balance <= 1000)
{
Account.State = new MenberState(Account);
}
else if (Account.Balance > 1000)
{
Account.State = new VIPState(Account);
}
}
}
  //MenberState类


public class MenberState : AccountState
{
public MenberState(Club account)
{
Account = account;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
Console.WriteLine("向名为" + Account.Owner + "的账户中存款" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
public override void Withdraw(double amount)
{
double newBalance = Account.Balance - amount;
if (newBalance > 0)
{
Account.Balance = newBalance;
Console.WriteLine("向名为" + Account.Owner + "的账户中消费" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
else
{
Console.WriteLine("你的账户余额不足,无法消费,请先存款");
}
StateChange();
}
public override void StateChange()
{
if (Account.Balance > 0&& Account.Balance <= 100)
{
Account.State = new VistorState(Account);
}
else if (Account.Balance > 1000)
{
Account.State = new VIPState(Account);
}
}
}
  //VIPState类


public class VIPState : AccountState
{
public VIPState(Club account)
{
Account = account;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
Console.WriteLine("向名为" + Account.Owner + "的账户中存款" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
public override void Withdraw(double amount)
{
double newBalance = Account.Balance - amount;
if (newBalance > 0)
{
Account.Balance = newBalance;
Console.WriteLine("向名为" + Account.Owner + "的账户中消费" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
else
{
Console.WriteLine("你的账户余额不足,无法消费,请先存款");
}
StateChange();
}
public override void StateChange()
{
if (Account.Balance > 0 && Account.Balance <= 100)
{
Account.State = new VistorState(Account);
}
else if (Account.Balance >100 && Account.Balance<=1000)
{
Account.State = new MenberState(Account);
}
}
}
 

//Club类


public class Club
{
public AccountState State { get; set; }
public double Balance { get; set; }
public string Owner { get; set; }
public Club(string owner, double initialAmount)
{
Owner = owner;
Balance = initialAmount;
State = new VistorState(this);
}

public void SetBalance(double amount)
{
Balance = amount;
}
public void Deposit(double amount)
{
Console.WriteLine("现要存款" + amount + "元");
State.Deposit(amount);
Console.WriteLine("账户状态变为" + State);
}
public void Withdraw(double amount)
{
Console.WriteLine("现要消费" + amount + "元");
State.Withdraw(amount);
Console.WriteLine("账户状态变为" + State);

}

}

 

}
}

效果

技术分享

《软件架构与设计模式》关于 状态模式 的一个小例子

标签:

原文地址:http://www.cnblogs.com/houyanjie/p/5093041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!