Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The State Pattern

Posted in Design Patterns
This post has been read 3504 times

In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the State Pattern. You can read about the other patterns from the following links

  1. Strategy Pattern

You can read about Structural Patterns from here.

You can read about Creational Patterns from here.

State pattern in essence is a dynamic version of the strategy pattern, meaning, when the sate inside an object is changed, it could change its behavior.

    1 namespace PatternsConsole

    2 {

    3     class StatePattern

    4     {

    5         interface IState

    6         {

    7             void Deposit( double amount );

    8             void Withdraw( double amount );

    9 

   10             double Balance { get; set; }

   11             Account Account { get; set; }

   12         }

   13 

   14         // State 1

   15         class InsufficientState : IState

   16         {

   17             public InsufficientState( double balance, Account account )

   18             {

   19                 Balance = balance;

   20                 Account = account;

   21             }

   22 

   23             public void Deposit( double amount )

   24             {

   25                 Balance += amount;

   26                 CheckState();

   27             }

   28 

   29             public void Withdraw( double amount )

   30             {

   31                 Balance -= amount;

   32                 Console.WriteLine( "Insufficient fund" );

   33             }

   34 

   35             private void CheckState()

   36             {

   37                 if ( Balance > 100 )

   38                 {

   39                     Account.State = new NormalState( Balance, Account );

   40                 }

   41                 else if ( Balance > 300 )

   42                 {

   43                     Account.State = new GoldState( Balance, Account );

   44                 }

   45             }

   46 

   47             public double Balance { get; set; }

   48             public Account Account { get; set; }

   49         }

   50 

   51         // State 2

   52         class NormalState : IState

   53         {

   54             public NormalState( double balance, Account account )

   55             {

   56                 Balance = balance;

   57                 Account = account;

   58             }

   59 

   60             public void Deposit( double amount )

   61             {

   62                 Balance += amount;

   63                 CheckState();

   64             }

   65 

   66             public void Withdraw( double amount )

   67             {

   68                 Balance -= amount;

   69                 CheckState();

   70             }

   71 

   72             private void CheckState()

   73             {

   74                 if ( Balance < 100 )

   75                 {

   76                     Account.State = new InsufficientState( Balance, Account );

   77                 }

   78                 else if ( Balance > 300 )

   79                 {

   80                     Account.State = new GoldState( Balance, Account );

   81                 }

   82             }

   83 

   84             public double Balance { get; set; }

   85             public Account Account { get; set; }

   86         }

   87 

   88         // State 3

   89         class GoldState : IState

   90         {

   91             public GoldState( double balance, Account account )

   92             {

   93                 Balance = balance;

   94                 Account = account;

   95             }

   96 

   97             public void Deposit( double amount )

   98             {

   99                 Balance += amount;

  100                 CheckState();

  101             }

  102 

  103             public void Withdraw( double amount )

  104             {

  105                 Balance -= amount;

  106                 CheckState();

  107             }

  108 

  109             private void CheckState()

  110             {

  111                 if ( Balance < 100 )

  112                 {

  113                     Account.State = new InsufficientState( Balance, Account );

  114                 }

  115                 else if ( Balance < 300 )

  116                 {

  117                     Account.State = new NormalState( Balance, Account );

  118                 }

  119             }

  120 

  121             public double Balance { get; set; }

  122             public Account Account { get; set; }

  123         }

  124 

  125         // Context

  126         class Account

  127         {

  128             public IState State { set; get; }

  129 

  130             // Construcstor

  131             public Account()

  132             {

  133                 State = new NormalState( 0, this );

  134             }

  135 

  136             public void Deposit( double amount )

  137             {

  138                 State.Deposit( amount );

  139                 Console.WriteLine( "Deposited {0:C} ", amount );

  140                 Console.WriteLine( " Balance = {0:C}", State.Balance );

  141                 Console.WriteLine( " Status  = {0}", State.GetType().Name );

  142                 Console.WriteLine( "" );

  143             }

  144 

  145             public void Withdraw( double amount )

  146             {

  147                 State.Withdraw( amount );

  148                 Console.WriteLine( "Withdrew {0:C} ", amount );

  149                 Console.WriteLine( " Balance = {0:C}", State.Balance );

  150                 Console.WriteLine( " Status  = {0}\n", State.GetType().Name );

  151             }

  152         }

  153 

  154         static void Main()

  155         {

  156             var account = new Account();

  157 

  158             account.Deposit( 100 );

  159             account.Deposit( 100 );

  160             account.Deposit( 300 );

  161             account.Withdraw( 200 );

  162             account.Withdraw( 200 );

  163             account.Withdraw( 100 );

  164 

  165             // Output:

  166             // Deposited $100.00

  167             // Balance = $100.00

  168             // Status  = NormalState

  169 

  170             // Deposited $100.00

  171             // Balance = $200.00

  172             // Status  = NormalState

  173 

  174             // Deposited $300.00

  175             // Balance = $500.00

  176             // Status  = GoldState

  177 

  178             // Withdrew $200.00

  179             // Balance = $300.00

  180             // Status  = GoldState

  181 

  182             // Withdrew $200.00

  183             // Balance = $100.00

  184             // Status  = NormalState

  185 

  186             // Withdrew $100.00

  187             // Balance = $0.00

  188             // Status  = InsufficientState

  189 

  190             Console.Read();

  191         }

  192     }

  193 }

As you can see in the example, the behavior of our code is changing based on the state which internally is changed based on the amount balance. We can use the state pattern when we have objects that may change their behavior at runtime based on some context or when the objects are becoming complex with many conditional branches.

In my next post I will be writing about the Template Method Pattern




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll