Dependency Injection and Inversion of Control
19.1 Dependency Injection
19.2 Dependency Injection in Practice
19.3 Applying Dependency Injection through Member Functionality
Dependency Injection
Dependency Injection is a concept where the container defines the relationships between components (classes), instead of developers assigning these relationships through direct coding.
Since direct associations do not occur in the code, changes to each class become more flexible (loosely coupled).
Strong Coupling and Loose Coupling
In reality, if the air conditioning system in a car malfunctions, naturally, you would repair or replace only the air conditioning system. However, what if the air conditioning functionality is designed to be related to the car's engine? In that case, even a minor issue with the air conditioning could lead to needing to address the car's engine as well. This situation could become complex. In other words, in a car's components, functionalities that are similar should be strongly coupled, while functionalities that are not closely related should be designed to not affect each other. This concept applies to programs as well.
A program is composed of independent functionalities. For instance, an online store consists of product management, order management, member management, and bulletin board management. Each of these functionalities is composed of multiple classes performing specific tasks. However, if changes occur in a class responsible for one functionality, and other unrelated classes also need to be modified, similar issues can arise as in the car example.
Therefore, related functionalities should be strongly coupled, while unrelated functionalities should be loosely coupled for a well-designed program. The opposite scenario could lead to problems.
Issues with Implementing in Java Code:
Currently, the BoardDAO class is implementing the board functionality by interfacing with Oracle.
- If there is a change from Oracle to MySQL as the database system, it will require modifying the functionality within the BoardDAO class.
- Furthermore, changes may also be needed in the BoardService class, which utilizes the BoardDAO class.
==========================>>>>>>>
- Therefore, directly creating and using objects in Java code (tightly coupled) can lead to complex problems.
- If changes in one class have a cascading effect on other parts, this approach (directly creating objects in Java code) may not be a good practice.
19.1.2 Implementing Interface for Board Functionality
- When a need arises to integrate with MySQL during development, instead of modifying the existing BoardOracleDAOImpl class, another class implementing the BoardDAO interface, called BoardMySqlDAOImpl, can be created. This new implementation can be used in BoardServiceImpl without altering the existing codebase.
- However, even when using interfaces, the BoardServiceImpl class itself still needs to be modified directly within the source code.
19.1.3 Applying Dependency Injection to Board Functionality
Advantages of Dependency Injection:
- Minimizes dependencies among classes, simplifying the codebase.
- Facilitates easier maintenance and management of the application.
- Unlike the traditional implementation approach where developers manually control object creation and disposal within the code, dependency injection delegates the responsibility of object creation, disposal, and interdependencies to the container.
Inversion of Control (IoC):
- In traditional code, developers directly control object instantiation, while in the Spring Framework, Spring takes over the responsibility of object control.
- There are various forms of IoC, and in Spring, the term Dependency Injection (DI) is used more frequently to refer to the implementation of IoC functionality.
Methods of Dependency Injection in Spring:
1. Constructor Injection
2. Setter Injection
XML
- DTD File
- XML File
- XML Schema File
- XSL
- DTD (Document Type Definition) File:
- DTD is a set of rules that defines the structure and the legal elements and attributes of an XML document.
- It specifies the data elements and their relationships within an XML document.
- DTD files are used to validate and ensure that an XML document adheres to a specific structure.
- XML File:
- An XML file contains data in a structured format using tags to define elements and attributes.
- It's used to store and transport data, and it's human-readable as well as machine-readable.
- XML Schema File:
- XML Schema is an alternative to DTD that provides a more powerful and flexible way to define the structure, content, and data types of XML documents.
- It uses XML-based syntax to define the rules and constraints for valid XML documents.
- XML Schema files are commonly used for validation and ensuring data consistency.
- XSL (eXtensible Stylesheet Language):
- XSL is used for transforming XML documents into various formats, such as HTML, plain text, or other XML structures.
- XSL consists of two parts: XSLT (XSL Transformations) for transformation and XPath for navigation and querying XML data.
- XSL stylesheets define how the XML data should be presented or transformed.