State pattern
State – defines a common interface for all states, so that they are interchangeable.
ConcreteState – defines a concrete definition of the state
Example
At the beginning we implement context class which will be represented employee.
//context public class Employee { public Position _developer { get; set; } public string _name { get; set; } public Employee(string name) { _name = name; _developer = new JuniorDeveloper(); } // calculation of salaries public void CalculateSalaray(int overtime) { _developer.CalculateSalaray(this, overtime); } // promotion public void Promote() { _developer.Promote(this); } }
Our employee will be able to : calculation of salaries and can advance. OK but… what mean Position in this class.
So, position is state, which can be an interface or an abstract class. The status will vary depending on – in which state is our object.
//state public interface Position { void CalculateSalaray(Employee dev, int overtime); void Promote(Employee dev); }
Let’s implement our states. Our states will pass in : Junior developer => Senior developer => Expert Developer =>can not be promoted above.
_developer = new JuniorDeveloper();
//ConcreteState public class JuniorDeveloper : Position { int salary = 7000; public void CalculateSalaray(Employee dev, int overtime) { var result = 50 * overtime; var summarySalary = salary + result; Console.WriteLine(dev._name + "- current salary : " + summarySalary + " with " + overtime + " overtime"); } public void Promote(Employee dev) { dev._developer = new SeniorDeveloper(); Console.WriteLine(dev._name + " promoted to senior"); } }
//ConcreteState public class SeniorDeveloper : Position { int salary = 15000; public void CalculateSalaray(Employee dev, int overtime) { var result = 150 * overtime; var summarySalary = salary + result; Console.WriteLine(dev._name + "- current salary : " + summarySalary + " with " + overtime + " overtime"); } public void Promote(Employee dev) { dev._developer = new ExperDeveloper(); Console.WriteLine(dev._name + " promoted to expert"); } }
Expert earns 90000 and he gets 600 for overtime and he can’t advance.
//ConcreteState public class ExperDeveloper : Position { int salary = 90000; public void CalculateSalaray(Employee dev, int overtime) { var result = 600 * overtime; var summarySalary = salary + result; Console.WriteLine(dev._name + "- current salary : " + summarySalary + " with " + overtime + " overtime"); } public void Promote(Employee dev) { Console.WriteLine(dev._name + " can't advance"); } }
Let’s create objects of two workers Tom and Jessica.Both begin from the position Junior developer. Tom has 20 hours of overtime and Jessica has 12 hours of overtime per month.
static void Main(string[] args) { Employee john = new Employee("Tom"); Employee katie = new Employee("Jessica"); john.CalculateSalaray(20); katie.CalculateSalaray(12); Console.ReadKey(); Console.WriteLine("\nJessica promotion"); katie.Promote(); john.CalculateSalaray(20); katie.CalculateSalaray(12); Console.ReadKey(); Console.WriteLine("\nTom promotion"); john.Promote(); john.CalculateSalaray(20); katie.CalculateSalaray(12); Console.ReadKey(); Console.WriteLine("\nTom promotion"); john.Promote(); john.CalculateSalaray(20); katie.CalculateSalaray(12); Console.ReadKey(); Console.WriteLine("\nTom promotion"); john.Promote(); john.CalculateSalaray(20); katie.CalculateSalaray(12); Console.ReadKey(); }
Effect:
Summary
State Pattern : we want to find a replacement for a conditional statement if / switch.
Yes, You are right 😉 Corrected.