Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, May 31
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Architecture»Understanding Open Closed Principle

    Understanding Open Closed Principle

    Dhanik Lal SahniBy Dhanik Lal SahniMay 2, 2018No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Understanding Open Closed Principle
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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.

    architecture design principle solid
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUnderstanding Dependency Inversion Principle with C#
    Next Article Understanding Liskov Substitution Principle
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni17 Mins Read

    How to Elevate Your Career to Salesforce Architect

    September 8, 2024
    By Dhanik Lal Sahni8 Mins Read

    Understanding the Salesforce Well-Architected Framework to Enhance Business Outcome

    August 25, 2024
    By Dhanik Lal Sahni8 Mins Read

    Streamlining Authentication: Custom Login Flow in Salesforce

    June 2, 2024
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.