Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • 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
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Friday, May 16
    • 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 Liskov Substitution Principle

    Understanding Liskov Substitution Principle

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

    The Liskov Substitution Principle states that subtypes must be substitutable for their base types. In order to substitute work, child class must not

    • Remove base class behavior
    • Violate base class invariants

    In general calling code should not know they are different from base types. LSP suggests that IS-A relationship should be replaced with IS-SUBSTITUTABLE-FOR.

    Let us take example of any web site subscription. Web site offer two types of customer, one is free and other is paid customer.

    So we will be having an interface like below

        public interface ICustomer
        {
            string CustomerName { set; get; }
            int CustomerCode { set; get; }
            int ProductQuantity { set; get; }
            double ProductRate { set; get; }
            CustomerType CustomerType { get; }
            double GetDiscount();
            string PrintInvoice(double _amount);
        }
    

    Two classes will be created for free and paid customer.

       
        class FreeCustomerSalesEnity:ICustomer
        {
            private string _customername;
            private int _customercode;
            private int _productquantity;
            private double _productrate;
            public string CustomerName
            {
                set { _customername = value; }
                get { return _customername; }
            }
            public int CustomerCode
            {
                set { _customercode = value; }
                get { return _customercode; }
            }
            public int ProductQuantity
            {
                set { _productquantity = value; }
                get { return _productquantity; }
            }
            public double ProductRate
            {
                set { _productrate = value; }
                get { return _productrate; }
            }
            public CustomerType CustomerType
            {
                get
                {
                    return CustomerType.Free;
                }
            }
            public double GetDiscount()
            {
                return 0;
            }
            public string PrintInvoice(double _amount)
            {
                throw new NotImplementedException();
            }
        }
    
        public enum CustomerType : int
        {
            Free,
            Paid
        }
        public class PaidCustomerSalesEnity : ICustomer
        {
            private string _customername;
            private int _customercode;
            private int _productquantity;
            private double _productrate;
            public string CustomerName
            {
                set { _customername = value; }
                get { return _customername; }
            }
            public int CustomerCode
            {
                set { _customercode = value; }
                get { return _customercode; }
            }
            public int ProductQuantity
            {
                set { _productquantity = value; }
                get { return _productquantity; }
            }
            public double ProductRate
            {
                set { _productrate = value; }
                get { return _productrate; }
            }
            public CustomerType CustomerType
            {
                get
                {
                    return CustomerType.Paid;
                }
            }
            public double GetDiscount()
            {
                double rate = ProductQuantity * ProductRate;
                double discountamount = 0;
                double disrate = 20;
                discountamount = (disrate / 100) * rate;
                rate = rate - discountamount;
                return rate;
            }
            public string PrintInvoice(double _amount)
            {
                return "Product Invoice For Customer " + CustomerName + " with Total Amount " + _amount;
            }
        }
    

    Code to add subscription for both classes.

        ICustomer objIcust;
        List listcust = new List();
        objIcust = new WithoutRefector.PaidCustomerSalesEnity();
        objIcust.CustomerName = "Paid Customer";
        objIcust.CustomerCode = 001;
        objIcust.ProductQuantity = 5;
        objIcust.ProductRate = 20;
        listcust.Add(objIcust);
        objIcust = new WithoutRefector.FreeCustomerSalesEnity();
        objIcust.CustomerName = "Free Customer";
        objIcust.CustomerCode = 002;
        objIcust.ProductQuantity = 5;
        objIcust.ProductRate = 20;
        listcust.Add(objIcust);
        string printinvoice = "";
        foreach (ICustomer iCust in listcust)
        {
            double amount = iCust.GetDiscount();
            printinvoice = iCust.PrintInvoice(amount);  // Throw exception for free customer as it is not required there
            lstCustomerDiscount.Items.Add("Invoice Report –> " + printinvoice);
        }
    

    Lets refactor above code to comply with Liskov Substitution principal.

    Create seprate interface for FreeCustomer. This interface will not require discount and invoice functionality.

        public interface IFreeCustomer
        {
            string CustomerName { set; get; }
            int CustomerCode { set; get; }
            CustomerType CustomerType { get; }       
        }
    
        public class FreeCustomerSalesEntity : IFreeCustomer
        {
            private string _customername;
            private int _customercode;
            private int _productquantity;
            private double _productrate;
            public string CustomerName
            {
                set { _customername = value; }
                get { return _customername; }
            }
            public int CustomerCode
            {
                set { _customercode = value; }
                get { return _customercode; }
            }
            public int ProductQuantity
            {
                set { _productquantity = value; }
                get { return _productquantity; }
            }
            public double ProductRate
            {
                set { _productrate = value; }
                get { return _productrate; }
            }
            public CustomerType CustomerType
            {
                get
                {
                   return  CustomerType.Free;
                }            
            }
        }
    

    Create IPaidCustomer, which will implement IFreeCustomer and IDiscount interface.

        public interface IDiscount
        {
            double GetDiscount();
        }
    
        public interface IPaidCustomer : IFreeCustomer, IDiscount
        {
            int ProductQuantity { get; set; }
            double ProductRate { get; set; }
            string PrintInvoice(double _amount);
        }
    
        public class PaidCustomerSalesEntity : IPaidCustomer
        {
            private string _customername;
            private int _customercode;
            private int _productquantity;
            private double _productrate;
            public CustomerType CustomerType
            {
                get
                {
                    return CustomerType.Paid;
                }
            }
            public string CustomerName
            {
                set { _customername = value; }
                get { return _customername; }
            }
            public int CustomerCode
            {
                set { _customercode = value; }
                get { return _customercode; }
            }
            public int ProductQuantity
            {
                set { _productquantity = value; }
                get { return _productquantity; }
            }
            public double ProductRate
            {
                set { _productrate = value; }
                get { return _productrate; }
            }
            public double GetDiscount()
            {
                if (CustomerType == CustomerType.Paid)
                {
                    double rate = ProductQuantity * ProductRate;
                    double discountamount = 0;
                    double disrate = 20;
                    discountamount = (disrate / 100) * rate;
                    rate = rate - discountamount;
                    return rate;
                }
                return 0;
            }
            public string PrintInvoice(double _amount)
            {
                return "Product Invoice For Customer " + CustomerName + " with Total Amount " + _amount;
            }
        }
    

    Above paid class implementing discount and invoice printing functionalities. Thease classes will be used like this

        IPaidCustomer objIcust;
        List listcust = new List();
        objIcust = new RefectorCode.PaidCustomerSalesEnity();
        objIcust.CustomerName = "Paid Customer";
        objIcust.CustomerCode = 001;
        objIcust.ProductQuantity = 5;
        objIcust.ProductRate = 20;
        listcust.Add(objIcust);  //Paid customer is converted as Free Customer
        IFreeCustomer obj = new FreeCustomerSalesEntity();
        obj.CustomerName = "Free Customer";
        obj.CustomerCode = 002;
        listcust.Add(obj);
        string printinvoice = "";
        foreach (IFreeCustomer iCust in listcust)
        {
           lstCustomerDiscount.Items.Add("Invoice Report –> " + printinvoice);
        }
    

    Full code for above priciple is available here.

    architecture design principle solid
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUnderstanding Open Closed Principle
    Next Article Choosing Between Traditional Web Pages and Single Page Application
    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
    • 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
    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 (110) 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) einstein (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 (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) 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
    • 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
    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 (110) 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) einstein (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 (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) 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.