Saturday, November 13, 2010

..........State Pattern

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

static void Main()
{
CEmployee objCEmployee = new CManager();

objCEmployee = objCEmployee.Action();
objCEmployee = objCEmployee.Action();
objCEmployee = objCEmployee.Action();

// Wait for user
Console.ReadKey();
}


///
/// The 'CEmployee' abstract class
///

abstract class CEmployee
{
public abstract CEmployee Action();
}

class CManager: CEmployee
{
public override CEmployee Action()
{
Console.WriteLine(“This is Manager’s Work.”);
return new CDeveloper();
}
}

class CDeveloper : CEmployee
{
public override CEmployee Action()
{
Console.WriteLine(“This is Developer’s Work.”);
return new CManager();
}
}

No comments:

Followers

Link