Where will loose and tight communications be used as a real scenario?

I referred to this link, Difference between loose and tight connection in Java?

After I said it still, I'm confused and don't understand at all.

As a Java beginner, I couldn't figure it out easily. Can anyone suggest a better example?

Can someone tell me that communication in java is important and why, where, how and when will it be needed when developing the code?

+6
source share
2 answers

Tight connection

  • In complex cases, the logic of one class will call the logic of another class only to provide the same service

  • If this happens, there is a so-called hard link between the two classes.

  • In this case, the first class that wants to call the logic of the second class will have to create an object from the second class

Example: we have two classes: traveller , and the second - car . traveller class invokes the logic of the car class; in this case, the traveler class creates a car class object.

This will mean that the car class will depend on the traveller object.

 Public class Traveller { Car c = new Car(); Public void startJourney() { c.move(); } } Public class Car { Public void move(){ ... } } 

Here, the traveller object is closely related to the car object.
If the traveller wants to switch from car to plane , then the entire traveller class should be changed as follows:

 Public class Traveller { Plane p = new Plane(); Public void startJourney() { p.move(); } } Public class Plane { Public void move(){ ... } } 

Loose connection

  • Our main traveller object in this case will allow an external object, the so-called container , to provide an object . Therefore, the traveller does not need to create his own class from the car or plane object, he will get it from container

  • When an object resolves the dependency injection mechanism.

  • An external container object can enter a car or plane object based on specific logic, a traveller requirement.

Example:

 Public class Traveller { Vehicle v; Public void setV(Vehicle v) { this.V = V; } Public void startJourney() { V.move(); } } 

Here, the traveller class introduces either a car object or a plane . No changes to the traveller class are required if we want to change the dependence on the car to the plane.

traveller class took a link to the vehicle, so an external object (container) can enter a car or plane object, depending on the requirements of the traveller .

+24
source

Tight Grip: -

  • When creating a complex application in java, the logic of one class will call the logic of another class to provide the same service to clients.

  • If one class invokes another class logic, then it is called sharing.

  • When one class interacts with another class, there is a stern connection between the two classes.

  • If one class wants to call the logic of the second class, then the first class needs an object of the second class, this means that the first class creates an object of the second class.

  • For example, if we have two classes, called the traveler and the car, the class of travelers invokes the logic of the class of cars; in this case, the traveler class creates a car class object.

  • In the above classes of travelers and car classes, an object is a class of car for depending on the object of travel.

Example: -

Traveler enter image description here

  • In the above example, the traveler object is closely connected with the car object, because in place of the car object, if you want to use the bicycle object, we need to make changes to the Traveler class

Example: -

travellor 1car1

Loose hitch: -

  • In Loose-Coupling, when one object depends on another class object, some external object will provide this dependency object to the main object, which we call the external object a container.

  • To get a loose connection between objects, the following two rules are required:

  • Classes must follow the POJI / POJO model.

  • Apply dependency injection mechanism.

For instance:

interface

travellor 2

  • In the above traveler class, an external object enters either a car (or) of a bicycle object.

  • When traveling, these changes are not required, we change the dependence on the car to the bicycle.

  • In the above class of travelers, we are a symbolic link to a vehicle, so that an external object (container) can enter either a car object (or) a bicycle object, depending on the requirement if the traveler.

  • In the spring frame, the spring container follows the dependency injection mechanism and introduces the dependency objects needed for the main object.

  • Spring Human Resources is a great success because of one of the main reasons this promotes Loose-Coupling between objects.

Source: - Close communication and communication between objects

+5
source

Source: https://habr.com/ru/post/958063/


All Articles