SalesforceCodex
  • Home
  • Architecture
  • Salesforce
  • .NET
  • Angular
  • Questions
    • Salesforce
    • .NET
    • Angular
  • Tech News
  • Social
  • Projects
    • Architecture

      Cohesion And Coupling – Approach for Design –…

      May 2, 2018

      Architecture

      Cohesion And Coupling – Approach for Design

      May 2, 2018

      Architecture

      Choosing Between Traditional Web Pages and Single Page…

      May 2, 2018

      Architecture

      Understanding Liskov Substitution Principle

      May 2, 2018

      Architecture

      Understanding Open Closed Principle

      May 2, 2018

SalesforceCodex

  • Home
  • Architecture
  • Salesforce
  • .NET
  • Angular
  • Questions
    • Salesforce
    • .NET
    • Angular
  • Tech News
  • Social
  • Projects
    • Architecture

      Cohesion And Coupling – Approach for Design –…

      May 2, 2018

      Architecture

      Cohesion And Coupling – Approach for Design

      May 2, 2018

      Architecture

      Choosing Between Traditional Web Pages and Single Page…

      May 2, 2018

      Architecture

      Understanding Liskov Substitution Principle

      May 2, 2018

      Architecture

      Understanding Open Closed Principle

      May 2, 2018

Architecture

Understanding Open Closed Principle

written by admin May 2, 2018
Understanding Open Closed Principle

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.

architecturedesign principlesolid
0 comment
0
Facebook Twitter Google + Pinterest
admin

previous post
Understanding Dependency Inversion Principle with C#
next post
Understanding Liskov Substitution Principle

You may also like

Practical Approach to Singleton Design Pattern

May 2, 2018

Understanding SOLID Design Principles using real objects

May 2, 2018

Understanding Liskov Substitution Principle

May 2, 2018

Understanding Single Responsibility Pattern – Problem and It’s...

May 2, 2018

Understanding Dependency Inversion Principle with C#

May 2, 2018

Cohesion And Coupling – Approach for Design

May 2, 2018

Cohesion And Coupling – Approach for Design –...

May 2, 2018

Choosing Between Traditional Web Pages and Single Page...

May 2, 2018

Leave a Comment Cancel Reply

Recent Posts

  • Field Access Explorer In lightning Web Component
  • Dependent PickList In Lightning Web Component
  • Create Dynamic Patch REST API in Salesforce Apex
  • Show Image in Lightning Web Component Data Table
  • Integrate Salesforce Stackexchange using Lightning Web Component and Apex
vagminecloud.com

SalesforceCodex

Salesforce Codex

Recent Comments

  • Dhanik Lal Sahni on Runing Salesforce App using Voice command – Speech-To-Text API
  • Dhanik Lal Sahni on Integrate Salesforce with WhatsApp using Twilio API
  • SUCHARITA MONDAL on Runing Salesforce App using Voice command – Speech-To-Text API
  • sakshi on Integrate Salesforce with WhatsApp using Twilio API
  • Dhanik Lal Sahni on Runing Salesforce App using Voice command – Speech-To-Text API

Archives

  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • May 2018

Categories

Recent Posts

  • Field Access Explorer In lightning Web Component

    December 6, 2019
  • Dependent PickList In Lightning Web Component

    November 19, 2019
  • Create Dynamic Patch REST API in Salesforce Apex

    October 31, 2019
  • Show Image in Lightning Web Component Data Table

    October 10, 2019
  • Integrate Salesforce Stackexchange using Lightning Web Component and Apex

    October 2, 2019

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

Recent Posts

  • Field Access Explorer In lightning Web Component

    December 6, 2019
  • Dependent PickList In Lightning Web Component

    November 19, 2019
  • Create Dynamic Patch REST API in Salesforce Apex

    October 31, 2019
  • Show Image in Lightning Web Component Data Table

    October 10, 2019
  • Integrate Salesforce Stackexchange using Lightning Web Component and Apex

    October 2, 2019

Tags

.net (1) .net core (1) .net news (1) .net question (1) Android developers (1) angular (1) angular2+ (2) angular cli (1) apex (13) Apex code IDE (1) Apex logging (1) architecture (9) Asynchronous apex (1) batch processing (1) Code versioning for Salesforce Project (1) community user (2) design principle (9) Error logging in Apex (1) Exception logging in apex (1) future method (1) health tips (1) healthy (1) IDE for Salesforce Development (1) lightning (15) Lightning web component (10) lwc (4) model dialog (1) news (1) Play Store (1) portal user (1) Queueable (1) salesforce (18) Salesforce IDE (1) Salesforce Interview Question (1) salesforce news (2) salesforce question (2) Scheduled Apex (1) Signature Pad (1) software engineers (1) solid (6) tfs (1) Visual Studio Code (2) web api (2) Winter 20 (8) workflow vs process builder (1)

My Recommended Books

Twitter Timeline

Tweets by SalesforceCodex

SUBSCRIBE NEWSLETTER

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

  • Facebook
  • Twitter
  • Linkedin
  • Youtube
  • Github
  • Stack-Overflow

@2016 - SalesforceCodex. All Right Reserved. Designed and Developed by Vagmine Cloud Solution