Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Last active May 13, 2018 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miteshsureja/cccb5aab4e07a049bd93028c76d8a22a to your computer and use it in GitHub Desktop.
Save miteshsureja/cccb5aab4e07a049bd93028c76d8a22a to your computer and use it in GitHub Desktop.
State Design Pattern
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatePattern
{
//state
public abstract class State
{
public BankAccount Account { get; set; }
public double Balance { get; set; }
public abstract void Deposit(double amount);
public abstract void Withdraw(double amount);
}
//concrete state
public class Normal : State
{
public Normal(State state)
{
Balance = state.Balance;
Account = state.Account;
}
public Normal(double balance, BankAccount account)
{
Balance = balance;
Account = account;
}
public override void Deposit(double amount)
{
Balance += amount;
CheckState();
}
public override void Withdraw(double amount)
{
Balance -= amount;
CheckState();
}
void CheckState()
{
if (Balance > 1000)
Account.State = new Classic(this);
}
}
//concrete state
public class Classic : State
{
public Classic(State state)
{
Balance = state.Balance;
Account = state.Account;
}
public Classic(double balance, BankAccount account)
{
Balance = balance;
Account = account;
}
public override void Deposit(double amount)
{
Balance += amount;
CheckState();
}
public override void Withdraw(double amount)
{
Balance -= amount;
CheckState();
}
void CheckState()
{
if (Balance < 1000)
Account.State = new Normal(this);
else if (Balance > 2000)
Account.State = new Platinum(this);
}
}
//concrete state
public class Platinum : State
{
public Platinum(State state)
{
Balance = state.Balance;
Account = state.Account;
}
public Platinum(double balance, BankAccount account)
{
Balance = balance;
Account = account;
}
public override void Deposit(double amount)
{
Balance += amount;
CheckState();
}
public override void Withdraw(double amount)
{
Balance -= amount;
CheckState();
}
void CheckState()
{
if (Balance < 2000)
Account.State = new Classic(this);
else if (Balance < 1000)
Account.State = new Normal(this);
}
}
//context
public class BankAccount
{
public State State { get; set; }
public string Name { get; set; }
public BankAccount(string name)
{
Name = name;
State = new Normal(0, this);
}
public double Balance
{
get { return State.Balance; }
}
public void Deposit(double amount)
{
State.Deposit(amount);
Console.WriteLine("Deposited - {0}", amount);
Console.WriteLine("Balance - {0}", Balance);
Console.WriteLine("Account Status - {0}", State.GetType().Name);
Console.WriteLine(new string('-', 50));
}
public void Withdraw(double amount)
{
State.Withdraw(amount);
Console.WriteLine("Withdrawn - {0}", amount);
Console.WriteLine("Balance - {0}", Balance);
Console.WriteLine("Account Status - {0}", State.GetType().Name);
Console.WriteLine(new string('-', 50));
}
}
class Program
{
//entry point
static void Main(string[] args)
{
BankAccount account = new BankAccount("Mitesh Sureja");
account.Deposit(500);
account.Deposit(600);
account.Deposit(1000);
account.Withdraw(500);
account.Withdraw(1500);
Console.Read();
}
}
}
@miteshsureja
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment