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

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

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

16条回答
  •  旧时难觅i
    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();
        }
    }
    

提交回复
热议问题