Tag:

architecture

    Cohesion And Coupling – Approach for Design – Continue

    Read 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 = "[email protected]",
                        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 – Module 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.

    May 2, 2018 0 comment
    0 Facebook Twitter Google + Pinterest