Home Architecture Understanding Open Closed Principle

Understanding Open Closed Principle

by Dhanik Lal Sahni

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. According to this principle, component should be

  • Open to ExtensionNew behavior can be added in the future.
  • Closed to Modification
    Changes to source code is not required.

Based on above statement, software entities should change behavior without changing code. For this, we have to use abstraction to implement behavior. In .NET we can use Interface and Abstract class to implement abstraction.

Let us see an example of e-commerce application to calculate cart’s total amount.

    public class OrderItem
    {
        public string StockKeepingUnit { get; set; }
        public int Quantity { get; set; }
    }
    public class Cart
    {
        private readonly List _items;
        public string CustomerEmail { get; set; }
        public Cart()
        {
            _items = new List();
        }
        public IEnumerable Items
        {
            get { return _items; }
        }
        public void Add(OrderItem orderItem)
        {
            _items.Add(orderItem);
        }
        public decimal TotalAmount()
        {
            decimal total = 0;
            foreach (OrderItem orderItem in Items)
            {
                if (orderItem.StockKeepingUnit.StartsWith("EACH"))
                {
                    total += orderItem.Quantity * 5m;
                }
                else if (orderItem.StockKeepingUnit.StartsWith("WEIGHT"))
                {
                    total += orderItem.Quantity * 4m/1000;
                }
                else if (orderItem.StockKeepingUnit.StartsWith("SPECIAL"))
                {
                    total += orderItem.Quantity * 4m;
                    int setOfThree = orderItem.Quantity / 3;
                    total -= setOfThree * 0.2m;
                }
            }
            return total;
        }

Above code is calculating amount based on quantity and StockKeepingUnit. This application is perfectly fine and will give valid result. See attached unit tests

Now we got requirement to add one more calculation condition in TotalAmount function. For this we have to change in that function code, which voilates Open Closed Principle. So

  • Addiding new rule will change total calculator function everytime
  • New calculation logic can introduce bug in system
  • We should avoid introducting changes that cascade through multiple modules in our application
  • Writing new classes is less likely to introduce problems, as this new classes is no where referenced

There are three way to achieve Open Closed Principle.

  • Procedural Programming
    Allow client to control behavior specific via a parameter
  • Inheritance/Template Method Pattern
    Child types override behavior of base class
  • Composition/Strategy Pattern
    Client code dependent on abstraction. It provide plugin model.

Here we are going to implement 2nd approach to implement OCP principle. Let us refactor above code. In above code, we are applying rule to to calculate item price. we cane capture out each rule as class. This will also help making those as SRP object. So that if any calculation logic need to be changes for any rule then only particular class will be changed. Other classes will not be changed.

    public interface IPricingRule
    {
        bool IsMatch(OrderItem item);
        decimal CalcultorPrice(OrderItem item);
    }
    public class EachPricingRule : IPricingRule
    {
        public decimal CalcultorPrice(OrderItem item)
        {
            return item.Quantity * 5m;
        }
        public bool IsMatch(OrderItem item)
        {
            return item.StockKeepingUnit.StartsWith("EACH");
        }
    }
    public class PerGramPricingRule:IPricingRule
    {
        public decimal CalcultorPrice(OrderItem item)
        {
            return item.Quantity * 4m/1000;
        }
        public bool IsMatch(OrderItem item)
        {
            return item.StockKeepingUnit.StartsWith("WEIGHT");
        }
    }
    public class SpecialPricingRule : IPricingRule
    {
        public decimal CalcultorPrice(OrderItem item)
        {
            decimal total = 0;
            total += item.Quantity * 4m;
            int setOfThree = item.Quantity / 3;
            return total -= setOfThree * 0.2m;
        }
        public bool IsMatch(OrderItem item)
        {
            return item.StockKeepingUnit.StartsWith("SPECIAL");
        }
    }

PricingCalculator component will calculate order items total.

    public interface IPricingCalculator
    {
        decimal CalCulatePrice(OrderItem item);
    }
    public class PricingCalculator : IPricingCalculator
    {
        public readonly List _pricingRule;
        public PricingCalcultaor()
        {
            _pricingRule = new List();
            _pricingRule.Add(new EachPricingRule());
            _pricingRule.Add(new PerGramPricingRule());
            _pricingRule.Add(new SpecialPricingRule());
        }
        public decimal CalCulatePrice(OrderItem item)
        {
            return _pricingRule.First(r => r.IsMatch(item)).CalcultorPrice(item);
        }
    }

Updated cart component code. Here we are only calling priceCalcultor.CalCulatePrice method. We have removed all calculation logic from here.

    public class Cart
    {
        private readonly List _items;
        public IPricingCalculator _pricingCalculator;
        public string CustomerEmail { get; set; }
        public Cart() : this(new PricingCalcultaor())
        {
        }
        public Cart(IPricingCalculator pricingCalculator)
        {
            _pricingCalculator = pricingCalculator;
            _items = new List();
        }
        public IEnumerable Items
        {
            get { return _items; }
        }
        public void Add(OrderItem orderItem)
        {
            _items.Add(orderItem);
        }
        public decimal TotalAmount()
        {
            decimal total = 0;
            foreach (OrderItem orderItem in Items)
            {
                total += _pricingCalculator.CalCulatePrice(orderItem);
            }
            return total;
        }
    }

By running unit test cases, all test case will execute successfully.

Let us, there is another offer, get one extra item when 4 items will be purchased. Then we have to simply add one more pricing rule class and add that rule in PricingCalculator rule list. We need not require to change Cart component.

    public class Buy4GetOneRule : IPricingRule
    {
        public decimal CalcultorPrice(OrderItem item)
        {
            decimal total = 0;
            total += item.Quantity * 1m;
            int setOfFive = item.Quantity / 5;
            return total -= setOfFive * 1m;
        }
        public bool IsMatch(OrderItem item)
        {
            return item.StockKeepingUnit.StartsWith("B4GO");
        }
    }
    public PricingCalcultaor()
    {
        _pricingRule = new List();
        _pricingRule.Add(new EachPricingRule());
        _pricingRule.Add(new PerGramPricingRule());
        _pricingRule.Add(new SpecialPricingRule());
        _pricingRule.Add(new Buy4GetOneRule());  //New Rule
    }

We should implement these principle in application from start so that it will not create problem later.

Summary

In software, All systems change during their life cycles. So Software entities should be open for extension, but closed for modification.When requirements change, we should extend the behavior, not changing old code.

Code for this principle is available here.

You may also like

Leave a Comment