Hello everyone, after the first article about Spring overview, now let’s dive into the two central concepts and the heart of Spring: IoC (Inversion of Control) and DI (Dependency Injection). But before going into these two concepts, I will give an article that is not part of the Spring Framework ecosystem, but it is the theoretical basis for the concept of DI, and a little theory will be easier for you to imagine. the concept of Tight-coupling, Loose-coupling, Loosely coupled (maybe it sounds strange, even though it’s in OOP).

Watching: What is Loose coupling?

Tight-coupling and Loose-coupling

Ok everyone, now we will go into 2 small concepts of object-oriented programming:

What is tight-coupling? Tight-coupling occurs when a group of layers has a high bond (dependency) to other layers in the program. Example: Think for a moment about our skin, the skin is stuck to your body. It’s fine when you were born here. But what if you want to change your skin color from white to black? Can you imagine how painful it would be to peel your skin, color it, and then glue it back on? Changing your skin is difficult because it is so closely tied to your body. You cannot change easily. You would have to fundamentally redesign a person to make this possible. Bottom line #1: In other words, if you want to change your skin, you’ll have to change the design of your body as well. because the two are combined – they are tightly coupled. Conclusion: When classes depend or bind each other when changing the application, maintenance or upgrade instead of small changes we must entail change with related classes this is very unscientific in software design right ???? what is loose-coupling? In contrast to Tight-coupling, Loose-coupling is to encourage the design of layers that have separate roles and reduce the interrelationship in the program. Example: Now think about getting dressed in the morning or evening. dark up to you. You don’t like blue? No problem, you can get a red shirt instead. You can do this easily because the shirt doesn’t really connect to your body like your skin does. The shirt doesn’t know or care about the body it’s being put on. In other words, you can change your clothes without actually changing your body. Bottom line #2: If you change your clothes, you’re not forced to change your body – when you can, then you have loose links.

READ MORE  Advertising What's Different? Advertisement

See also: What is W3c – W3c Web Standards

When you can’t do that, then you have strong linkage. Conclusion: Loose linkage helps classes work independently, have clear responsibilities, and when we need to upgrade and maintain, we need inheritance, add an interface or extend the code class without changing the old code. Practical examples for 2 concepts and how to reduce dependency (Loosely coupled)

For example, when you work with input and output, when we read and save data to the database, the code we don’t care what we save because the class that saves to the database already has classes to take care of, Similar to exporting output data, we don’t care what data to output because exporting to an Excel file will have the Export Excel class in charge, exporting to CSV will have the Export CSV class taking care of it. The workflow we only need to call the functions to save data, output data and depending on the user requirements, the layer selection operation is performed accordingly. Ok through the code will be easier to understand for everyone ????First is an example for Tight-coupling okay when working dependency classes are injected directly and become an important property in a class and when desired Changing the code will lead to a lot of trouble.

See also: Look up words: what does tram anh the Phieu mean, the meaning of the word The Phieu?

As you can see above, the ExportCSV class is associated with ExportData and now when you ask to change the export of JSON instead of CSV, how do you solve this? Then we will reduce the dependency loosely coupled because now the ExportCSV class lives and dies even though it exists when ExportData exists ????

READ MORE  Benefits And Types of Network

Ok now we add an interface to only care about the implementation method without knowing its implementation details, but the requirement now is to output the JSON format but the interface defines an ExportCSV class like that. Well, now is the time, depending on the user requirements that we inject or add to the dependent classes to make the program run better.

Then, depending on the business needs, we will have the corresponding export classes and these classes will implement the IExport Interface whose task is Export. Then the ExportData class just takes the task of exporting the data, regardless of whether it outputs JSON or CSV, which we inject logic binding classes into. In addition, when people look at the code above, they will find it relatively long when declaring this and that, that’s why the Design Patterns were born and the Factory Method will help us solve the problem depending on the requirements and create a object has the appropriate class (I will cover in Series Design Pattern hen

). Ok above are the concepts of Tight-coupling, Loose-coupling as well as how to reduce the dependency between Loosely coupled classes in object-oriented programming. If you don’t understand something, leave a comment and I’ll answer it. Below is the reference link for the article, thank you everyone for following this ^^ Thank you very much!