What is the difference between loose coupling and tight coupling in the object oriented paradigm?

前端 未结 16 1782
粉色の甜心
粉色の甜心 2020-11-22 08:03

Can any one describe the exact difference between loose coupling and tight coupling in Object oriented paradigm?

相关标签:
16条回答
  • 2020-11-22 08:58

    An extract from my blog post on coupling:

    What is Tight Coupling:-

    As par above definition a Tightly Coupled Object is an object that needs to know about other objects and are usually highly dependent on each other's interfaces.

    When we change one object in a tightly coupled application often it requires changes to a number of other objects. There is no problem in a small application we can easily identify the change. But in the case of a large applications these inter-dependencies are not always known by every consumer or other developers or there is many chance of future changes.

    Let’s take a shopping cart demo code to understand the tight coupling:

    namespace DNSLooseCoupling
    {
        public class ShoppingCart
        {
            public float Price;
            public int Quantity;
    
            public float GetRowItemTotal()
            {
                return Price * Quantity;
            }
        }
    
        public class ShoppingCartContents
        {
            public ShoppingCart[] items;
    
            public float GetCartItemsTotal()
            {
                float cartTotal = 0;
                foreach (ShoppingCart item in items)
                {
                    cartTotal += item.GetRowItemTotal();
                }
                return cartTotal;
            }
        }
    
        public class Order
        {
            private ShoppingCartContents cart;
            private float salesTax;
    
            public Order(ShoppingCartContents cart, float salesTax)
            {
                this.cart = cart;
                this.salesTax = salesTax;
            }
    
            public float OrderTotal()
            {
                return cart.GetCartItemsTotal() * (2.0f + salesTax);
            }
        }
    }
    

    Problems with the above example

    Tight Coupling creates some difficulties.

    Here, OrderTotal() methods is give us complete amount for the current items of the carts. If we want to add the discount features in this cart system. It is very hard to do in above code because we have to make changes at every class as it is very tightly coupled.

    0 讨论(0)
  • Tight coupling is when a group of classes are highly dependent on one another.

    This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.

    Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns.

    A loosely-coupled class can be consumed and tested independently of other (concrete) classes.

    Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface.

    Example of tight coupling:

    class CustomerRepository
    {
        private readonly Database database;
    
        public CustomerRepository(Database database)
        {
            this.database = database;
        }
    
        public void Add(string CustomerName)
        {
            database.AddRow("Customer", CustomerName);
        }
    }
    
    class Database
    {
        public void AddRow(string Table, string Value)
        {
        }
    }
    

    Example of loose coupling:

    class CustomerRepository
    {
        private readonly IDatabase database;
    
        public CustomerRepository(IDatabase database)
        {
            this.database = database;
        }
    
        public void Add(string CustomerName)
        {
            database.AddRow("Customer", CustomerName);
        }
    }
    
    interface IDatabase
    {
        void AddRow(string Table, string Value);
    }
    
    class Database implements IDatabase
    {
        public void AddRow(string Table, string Value)
        {
        }
    }
    

    Another example here.

    0 讨论(0)
  • 2020-11-22 09:04

    In object oriented design, the amount of coupling refers to how much the design of one class depends on the design of another class. In other words, how often do changes in class A force related changes in class B? Tight coupling means the two classes often change together, loose coupling means they are mostly independent. In general, loose coupling is recommended because it's easier to test and maintain.

    You may find this paper by Martin Fowler (PDF) helpful.

    0 讨论(0)
  • 2020-11-22 09:06

    Loose Coupling is the process of giving the dependency your class needs indirectly without providing all the information of the dependency(i.e in the from of interface) in case tight coupling you directly give in the dependency which is not good way of coding.

    0 讨论(0)
提交回复
热议问题