Skip to main content

Coupling & Cohesion

Coupling

 The degree of dependency between component is called coupling. If dependency is more then it is considered as tightly coupling and if dependency is less then it is considered as loosely coupling.

eg-:

class B{
static int y=C.x;
}
class C{
static int x=D.z;
}
class D{
static int z=E.m();
}
class E{
      public static int m(){
          return 10;
     }
}

The above component are said to be tightly coupled with each other because dependency between component is more.

Tightly coupling is not a good programming practice because it has several serious disvantages
   
  •      Without effecting remaining component, we can't modify and component and hence enhanced will become difficult.
  •      It subraces reusability 
  •      if reduces  maintainability of the application.
Hence, we have to maintain dependency between the components as less as possible that is loosely coupling is a good programming practice.

                                                                        Cohesion

For every component a clear well define functionality is defined, then  component is said to be followed high cohesion.


Low Cohesion-:In Java, cohesion refers to the degree to which the elements within a module (e.g., a class or a module) are related to each other and contribute to a single, well-defined purpose. Low cohesion occurs when a module performs multiple unrelated tasks or has elements that are not closely related. This can lead to several issues, such as increased complexity, maintainability problems, and difficulty in understanding and modifying the codebase.
                                    

                            Login page

                              Validation

                                Inbox 

                            Reply page

                            Compost

                                    .

                                    .

                                    .

                                    .

                         Low cohesion


High Cohesion-:In Java, high cohesion is a design principle that aims to create classes or modules that have a strong, clear, and focused purpose. Cohesion refers to how closely the elements within a module or class are related to each other. High cohesion means that the elements within the module work together and serve a single, well-defined purpose, making the module easier to understand, maintain, and modify. When a class has high cohesion, it typically indicates a well-designed and organized system.

High cohesion is always good programming practice because it has several advantages
  • without effecting remaining components, we can modify any component, hence enhancement will become easy.
  • It prompts reusability of the code (where ever validation is required, we can reuse the same validate servlet without rewriting.
  • It improves maintainability of the application.

High Cohesion

    
                                                                        High Cohesion


Note-: Loosely coupling and high cohesion are good programming practices.

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...