Wednesday, 5 February 2020

Java Object Coupling


Java Object Coupling -
a) Loosly  Coupling
b) Tight Coupling
Class Person {
      Address address = new Address();
}

Class Address {
    private String location;
}

Now we need to see relationship between Person and Address,

Person object creating the object using new Address(), means Person Object responsible to create the address Object. When person object dies then address object also died.

The relationship between Person and Address are Tight Coupling.

Loosly Coupling:
----------------------
Class Organization{

       Address address;

       Organization(Address address) {
            this.address = address;
       }
}

Class Test {
       public static void main(String... args) {
              Address address  = new Address();
              Organization org = new Organization(address);
       }
 }

The relationship between Organization object and Address Object loosly coupled.

Here Organization not creating the address object. Here Test person creating the address object then passing the org object. if org object dies address obj will not die.

Hence it's loosly coupled.


No comments:

Post a Comment