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»Cohesion And Coupling – Approach for Design – Continue

    Cohesion And Coupling – Approach for Design – Continue

    Dhanik Lal SahniBy Dhanik Lal SahniMay 2, 2018Updated:March 19, 2023No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Cohesion And Coupling – Approach for Design – Continue
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Cohesion and Coupling are essences of a good design process. The system should be decomposed into many modules to make it manageable for system changes. Projects which are well-designed are rarely in trouble with system changes. These changes should be properly reviewed and recognized.

    Read the first part of this topic here.

    Coupling

    Coupling is the measure of the degree of interdependence between modules. Two modules with high coupling are strongly interconnected and thus, dependent on each other. Two modules with low coupling are not dependent on one another. Lossely coupled systems are made up of modules which are relatively independent. Highly coupled system are dependent on each other module. A good design will always have low coupling.

    Coupling is of many types. Following are types

    • Data Coupling
    • Stamp Coupling
    • Control Coupling
    • Common Coupling
    • Content Coupling

    Low Coupling (Highly Desirable)

    Low Coupling is highly desirable. This indicate that module are not inter related. Each module is doing its work independently.

    1. Data Coupling

    Two modules said to be data coupled when dependency is only related to data passing. Other than passing data there is no dependency. Let us take following two modules Interest and Print. These two modules get account as parameter and do their own job. There is no other dependency.

        public static void Main()
        {
            Account account = new Account();
            var interest = new Interest();
            var print = new Statement();
            interest.CalculateInterest(account);
            print.PrintStatement(account);
        }
        public class Account
        {
            //Properties
        }
        public class Interest
        {
            public void CalculateInterest(Account account)
            {
                //calculation Logic
            }
        }
        public class Statement
        {
            public void PrintStatement(Account account)
            {
                    //Printing Logic
            }
        }
    

    2. Stamp Coupling

    Two modules are stamp coupled if they communicate via a passed data structure that contains more information than necessary for them to perform their functions.
    Let us take following two modules Interest and Transaction. Transaction module do transfer of money and Interest module calculate interest in period of time. We have passed complete account detail, which is not required by both modules. Instead of complete account detail, we should pass required detail only.

        public static void Main()
        {
            Account account = new Account();
            var interest = new Transaction();
            var print = new Interest();
            interest.Deposit(account);
            print.CalculateInterest(account);
        }
        public class Account
        {
            //Properties
        }
        public class Transaction
        {
            public void Deposit(Account account)
            {
                //Deposit logic
            }
            public void Withdraw(Account account)
            {
                //Withdraw logic
            }
        }
        public class Interest
        {
            public void CalculateInterest(Account account)
            {
                //Printing Logic
            }
        }
    

    For transaction, we need only two properties. So should pass only these information only.

        public class TranAccount
        {
            public double Amount { get; set; }
            public long AccountNumber { get; set; }
        }
    

    For interest calculation, we need only following properties. So should pass only these information only.

        public class InterestCalculation
        {
            public long AccountNumber { get; set; }
            public DateTime CalculationStartDate { get; set; }
            public DateTime CalculationEndDate { get; set; }
        }
    

    Medium Coupling (Moderate)

    1. Control Coupling

    Two modules said to be control coupled if they communicate by passing of control information. This is achieved by passing some flag from one module to another module.

    Let us take an example of customer addition system, Customer is added then mail will be sent based on record insertion status.

        public class Customer
        {
            public void Save()
            {
                if (InsertRecord())
                {
                    Notification.SendEmail(true);
                }
            }
            private bool InsertRecord()
            {
                //Record insertion logic
                return true;
            }
        }
        public class Notification
        {
            public static void SendEmail(bool isRecord )
            {
                if(isRecord)
                { 
                    //Email Notification Logic
                }           
            }
        }
    

    Low Coupling (Lowest)

    1. Common Coupling

    Two modules are common coupled if they shared data using global access. Let us take example of ApplicationData class which store company information. This information is saved in Customer and Address class also.

        public static class ApplicationData
        {
            public static string GetCompanyName
            {
                get
                {
                    return "DotNetCodex";
                }
            }
            public static string GetCompanyAddress
            {
                get
                {
                    return "Ghaziabad";
                }
            }
        }
        public class Entity
        {
            public string CompanyName { get; set; }
        }
        public class Customers: Entity
        {
            public void Save()
            {
                try
                {
                    this.CompanyName = ApplicationData.GetCompanyName;
                }
                catch (Exception ex)
                {
                    // Logic 
                }
            }
        }
        public class Address:Entity
        {
            public void Save()
            {
                try
                {
                    this.CompanyName = ApplicationData.GetCompanyName;
                }
                catch (Exception ex)
                {
                }
            }
        }
    

    2. Content Coupling

    Two modules are content coupled if one module changes data of other module or control passed from one module to another module. It is like Dependency Inversion Control where control is passed to other module.

    Let us take example of account generation class. In this class INotification sender is passed using constructor. How notification will be send that is role of notification class. There is no role of account class.

        public class Account
        {
            INotificationSender emailSender = null;
            public Account(INotificationSender sender)
            {
                emailSender = sender;
            }
            public void OpenAccount()
            {
                Customer customer = new Customer()
                {
                    FirstName = "Dhanik",
                    LastName = "Sahni",
                    DateOfBorth = new DateTime(1981, 07, 05),
                    Address = new Address
                    {
                        Address1 = "SVF",
                        Address2 = "Lal Kuan",
                        City = "Ghaziabad",
                        State = "UP",
                        Zip = "201002"
                    },
                    Contact = new Contact
                    {
                        Email = "dhanik.sahni@yahoo.com",
                        Fax = "3234234242"
                    }
                };
                //Concreate Object 
                emailSender.SendNotification(customer);
            }
        }
    

    Benefits of low coupling and high cohesive system are

    • Maintainability – Changes are confined in a single module.
    • Testability – Modules are confined to single so unit testing is also easy to perform.
    • Reusablity – Modules are divided in different and single unit. No useless function are introduced in module.

    Summary

    Cohesion is related to the principle that a class/method should be responsible for one thing only. Coupling is how interdependent different parts of the system are. e.g how and where there are dependencies. If two classes make calls to methods of each other then they are tightly coupled, as changing one would mean having to change the other. Low coupling and High cohesion is desirable for good application design. This will help in easy modification when any changes are required.

    architecture design principle
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleCohesion And Coupling – Approach for Design
    Next Article Difference Between Finalize and Dispose Method
    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.