Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace State mode
{
public interface State
{
void dosomething (person P);
}
public class Mstate:state
{
public void dosomething (person p)
{
if (p.hours = = 7)
{
Console.WriteLine ("Eat breakfast");
}
Else
{
P.state = new Lstate ();
P.dosomething ();
}
}
}
public class Lstate:state
{
public void dosomething (person p)
{
if (p.hours = = 12)
{
Console.WriteLine ("Eat Lunch");
}
Else
{
P.state =new nstate ();
P.dosomething ();
}
}
}
public class Nstate:state
{
public void dosomething (person p)
{
if (p.hours = = 18)
{
Console.WriteLine ("Eat Dinner");
}
Else
{
P.state =new nostate ();
P.dosomething ();
}
}
}
public class Nostate:state
{
public void dosomething (person p)
{
Console.WriteLine ("No Food");
}
}
public class Person
{
Public state State {get; set;}
public int hours {get; set;}
Public person ()
{
State=new mstate ();
}
public void DoSomething ()
{
State.dosomething (this);
State=new mstate ();
}
}
Class Program
{
static void Main (string[] args)
{
Person P=new person ();
P.hours = 7;
P.dosomething ();
P.hours = 12;
P.dosomething ();
p.hours = 18;
P.dosomething ();
P.hours = 8;
P.dosomething ();
P.hours = 7;
P.dosomething ();
Console.readkey ();
}
}
}
State Mode C # (state flow example Eat)