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

前端 未结 16 1781
粉色の甜心
粉色の甜心 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:53

    The way I understand it is, that tightly coupled architecture does not provide a lot of flexibility for change when compared to loosely coupled architecture.

    But in case of loosely coupled architectures, message formats or operating platforms or revamping the business logic does not impact the other end. If the system is taken down for a revamp, of course the other end will not be able to access the service for a while but other than that, the unchanged end can resume message exchange as it was before the revamp.

    0 讨论(0)
  • 2020-11-22 08:55

    There is a lot of nice answers here using analogies but a friend at work gave me an example that I liked more than all of the ones mentioned here... Eyes and Glasses!

    Tight Coupling

    Tight coupling would be the eyes. If I want to fix my vision, I'ts very expensive to get an eye transplant and holds a fair amount of risk. But what if the designer (being the human race) found a better way. Add a feature that is loosely coupled to the body so it can be easily changed! (yes.. glasses)

    Loose coupling

    I can easily replace my glasses without breaking my underlying vision. I can take off the glasses and my vision will be how it was before (not better or worse). Using different pairs of glasses changes how we see the world through our eyes with little risk and easy maintainability.

    Summary

    So next time someone asks you "who cares if my code is tightly-coupled?" The answer is all about effort to change, effort to maintain and risk of change.

    So how is this done in C#? Interfaces and Dependency Injection!

    EDIT

    This a good example of the Decorator pattern as well, where the the eyes are the class we are decorating by meeting interface requirements but giving different functionality (e.g. sunglasses, reading glasses, magnifying glasses for jewelers e.t.c)

    0 讨论(0)
  • 2020-11-22 08:55

    If an object's creation/existence dependents on another object which can't be tailored, its tight coupling. And, if the dependency can be tailored, its loose coupling. Consider an example in Java:

    class Car {
    
        private Engine engine = new Engine( "X_COMPANY" ); // this car is being created with "X_COMPANY" engine
        // Other parts
    
        public Car() { 
            // implemenation 
        }
    
    }
    

    The client of Car class can create one with ONLY "X_COMPANY" engine.

    Consider breaking this coupling with ability to change that:

    class Car {
    
        private Engine engine;
        // Other members
    
        public Car( Engine engine ) { // this car can be created with any Engine type
            this.engine = engine;
        }
    
    }
    

    Now, a Car is not dependent on an engine of "X_COMPANY" as it can be created with types.

    A Java specific note: using Java interfaces just for de-coupling sake is not a proper desing approach. In Java, an interface has a purpose - to act as a contract which intrisically provides de-coupling behavior/advantage.

    Bill Rosmus's comment in accepted answer has a good explanation.

    0 讨论(0)
  • 2020-11-22 08:56

    Loose coupling is and answer to to old style hardcoded dependencies and related issues issues like frequent recompilation when anything changes and code reuse. It stresses on implementing the worker logic in components and avoiding solution specific wire up code there.

    Loose Coupling = IoC See this for easier explanation.

    0 讨论(0)
  • 2020-11-22 08:56

    It's about classes dependency rate to another ones which is so low in loosely coupled and so high in tightly coupled. To be clear in the service orientation architecture, services are loosely coupled to each other against monolithic which classes dependency to each other is on purpose

    0 讨论(0)
  • 2020-11-22 08:57

    Tight Coupling means one class is dependent on another class.
    Loose Coupling means one class is dependent on interface rather than class.

    In tight coupling, there are hard-coded dependency declared in methods.
    In loose coupling, we must pass dependency externally at runtime instead of hard-coded. (Loose couple systems use interface for decreased dependency with class.)

    For example, we have a system that can send output in two or more ways like JSON output, CSV output, etc.

    Tight Coupled

    public interface OutputGenerator {
        public void generateOutput();
    }
    
    public class CSVOutputGenerator implements OutputGenerator {
        public void generateOutput() {
            System.out.println("CSV Output Generator");
        }
    }
    
    public class JSONOutputGenerator implements OutputGenerator {
        public void generateOutput() {
            System.out.println("JSON Output Generator");
        }
    }
    
    // In Other Code, we write Output Generator like...
    public class Class1 {
        public void generateOutput() {
            // Here Output will be in CSV-Format, because of hard-coded code.
            // This method tightly coupled with CSVOutputGenerator class, if we want another Output, we must change this method.
            // Any method, that calls Class1's generateOutput will return CSVOutput, because Class1 is tight couple with CSVOutputGenerator.
            OutputGenerator outputGenerator = new CSVOutputGenerator();
            output.generateOutput();
        }
    }
    

    In the example above, if we want to change the output in JSON, then we need to find and change in the whole code, because Class1 is tightly coupled with the CSVOutputGenerator class.

    Loose Coupled

    public interface OutputGenerator {
        public void generateOutput();
    }
    
    public class CSVOutputGenerator implements OutputGenerator {
        public void generateOutput() {
            System.out.println("CSV Output Generator");
        }
    }
    
    public class JSONOutputGenerator implements OutputGenerator {
        public void generateOutput() {
            System.out.println("JSON Output Generator");
        }
    }
    
    // In Other Code, we write Output Generator like...
    public class Class1 {
        public void generateOutput(OutputGenerator outputGenerator) {
            // if you want to write JSON, pass object of JSONOutputGenerator (Dependency will be passed externally to this method)
            // if you want to write CSV, pass object of CSVOutputGenerator (Dependency will be passed externally to this method)
    
            // Due to loose couple with class, we don't need to change code of Class1, because Class1 is loose coupled with CSVOutputGenerator or JSONOutputGenerator class
            // Any method, that calls Class1's generateOutput will desired output, because Class1 does not tight couple with CSVOutputGenerator or JSONOutputGenerator class
            OutputGenerator outputGenerator = outputGenerator;
            output.generateOutput();
        }
    }
    
    0 讨论(0)
提交回复
热议问题