static void Main(string[] args) { Employee empJohn = new PermanentEmployee(1, "John"); Console.WriteLine($"Employee:\n {empJohn.ToString()} \nBonus: {empJohn.CalculateBonus(100000).ToString()}"); Employee empManu = new TemporaryEmployee(2, "Manu"); Console.WriteLine($"Employee:\n {empManu.ToString()} \nBonus: {empManu.CalculateBonus(100000).ToString()}"); Console.ReadLine(); }
// any new functionality should be implemented by adding new classes, attributes and methods, // instead of changing the current ones or existing ones. // Bertrand Meyer is generally credited for having originated the term open/closed principle, // and This Principle is considered by Bob Martin as "the most important principle of object-oriented design". // The simplest way to apply OCP is to implement the new functionality on new derived (sub) classes that inherit the original class implementation. // Another way is to allow client to access the original class with an abstract interface. // So, at any given point of time when there is a requirement change instead of touching the existing functionality it’s always suggested to create new classes and leave the original implementation untouched. static void Main(string[] args) { Employee empJohn = new PermanentEmployee(1, "John"); Employee empJason = new TemporaryEmployee(2, "Jason"); Console.WriteLine(string.Format("Employee {0} Bonus: {1}", empJohn.ToString(), empJohn.CalculateBonus(100000).ToString())); Console.WriteLine(string.Format("Employee {0} Bonus: {1}", empJason.ToString(), empJason.CalculateBonus(150000).ToString())); Console.ReadLine(); }
static void Main(string[] args) { var empJohn = new PermanentEmployee { Id = 1, Name = "John" }; var empChris = new PermanentEmployee { Id = 2, Name = "Chris" }; Console.WriteLine($"Employee {empJohn}, Bonus{empJohn.CalculateBonus(10000)}"); Console.WriteLine($"Employee {empChris}, Bonus{empChris.CalculateBonus(150000)}"); Console.ReadLine(); }
// OpenClosed Principle states that software entities such as class/funtion/modules should be // open for extension but closed for modification // Which means any new funtionality should be implemented by adding new classes, attributes and methods instead of changing // current existing ones // Implementation guidelines // 1. Implement new funtionality on derived classes that inherit original implementation. // 2. Allow client to access the original class with an abstract interface. static void Main(string[] args) { // 1. Initial stage //Stage1Employee emp1 = new Stage1Employee(1, "Anuj"); //Console.WriteLine("Employee : " + emp1.Name + " Bonus is : " + emp1.CalculateBonus(20000)); // 2. Calculate bonus for Permananent employee and contract employee differs // Not following open closed principle // We have changed the method for modification and for new req we'll continue doing so //Stage2Employee emp1 = new Stage2Employee(1, "Anuj", "Permanent"); //Stage2Employee emp2 = new Stage2Employee(1, "Vivek", "Contract"); //Console.WriteLine("Employee : " + emp1.Name + " Bonus is : " + emp1.CalculateBonus(20000)); //Console.WriteLine("Employee : " + emp2.Name + " Bonus is : " + emp2.CalculateBonus(20000)); // 3. Employee emp1 = new ContractEmployee(1, "Ajay"); Employee emp2 = new PermanentEmployee(2, "Anuj"); Console.WriteLine(emp1.ToString() + emp1.CalculateBonus(20000)); Console.WriteLine(emp2.ToString() + emp2.CalculateBonus(20000)); }