mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-05 08:23:06 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
---
|
||||
title: Dependency Injection
|
||||
title: "Dependency Injection Pattern in Java: Boosting Maintainability with Loose Coupling"
|
||||
shortTitle: Dependency Injection
|
||||
description: "Learn about the Dependency Injection design pattern. Explore its benefits, real-world examples, class diagrams, and best practices for implementation in Java."
|
||||
category: Creational
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Dependency management
|
||||
- Inversion of control
|
||||
- Decoupling
|
||||
- Dependency management
|
||||
- Inversion of control
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@@ -13,11 +15,11 @@ tag:
|
||||
* Inversion of Control (IoC)
|
||||
* Dependency Inversion
|
||||
|
||||
## Intent
|
||||
## Intent of Dependency Injection Design Pattern
|
||||
|
||||
To decouple the creation of object dependencies from their usage, allowing for more flexible and testable code.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Dependency Injection Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -27,13 +29,13 @@ Real-world example
|
||||
|
||||
In plain words
|
||||
|
||||
> Dependency Injection separates creation of client's dependencies from its own behavior.
|
||||
> Dependency Injection separates the creation of the client's dependencies from its own behavior.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Dependency Injection Pattern in Java
|
||||
|
||||
The old wizard likes to fill his pipe and smoke tobacco once in a while. However, he doesn't want to depend on a single tobacco brand only but likes to be able to enjoy them all interchangeably.
|
||||
|
||||
@@ -112,23 +114,23 @@ The program output:
|
||||
11:54:05.308 [main] INFO com.iluwatar.dependency.injection.Tobacco -- GuiceWizard smoking RivendellTobacco
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
## Detailed Explanation of Dependency Injection Pattern with Real-World Examples
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
## When to Use the Dependency Injection Pattern in Java
|
||||
|
||||
* When aiming to reduce the coupling between classes and increase the modularity of the application.
|
||||
* In scenarios where the object creation process is complex or should be separated from the class usage.
|
||||
* In applications requiring easier unit testing by allowing dependencies to be mocked or stubbed.
|
||||
* Within frameworks or libraries that manage object lifecycles and dependencies, such as Spring or Jakarta EE (formerly Java EE).
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Dependency Injection Pattern in Java
|
||||
|
||||
* Frameworks like Spring, Jakarta EE, and Google Guice use DI extensively to manage component lifecycles and dependencies.
|
||||
* Frameworks like Spring, Jakarta EE, and Google Guice use Dependency Injection (DI) extensively to manage component lifecycles and dependencies.
|
||||
* Desktop and web applications that require flexible architecture with easily interchangeable components.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Dependency Injection Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -139,16 +141,16 @@ Benefits:
|
||||
Trade-offs:
|
||||
|
||||
* Can introduce complexity in the configuration, especially in large projects.
|
||||
* Might increase the learning curve for developers unfamiliar with DI patterns or frameworks.
|
||||
* Might increase the learning curve for developers unfamiliar with Dependency Injection patterns or frameworks.
|
||||
* Requires careful management of object lifecycles and scopes.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/) and [Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/): Used to create instances that the DI mechanism will inject.
|
||||
* [Service Locator](https://java-design-patterns.com/patterns/service-locator/): An alternative to DI for locating services or components, though it does not decouple the lookup process as effectively as DI.
|
||||
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used in conjunction with DI to provide a single instance of a service across the application.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Clean Code: A Handbook of Agile Software Craftsmanship](https://amzn.to/3wRnjp5)
|
||||
* [Dependency Injection: Design patterns using Spring and Guice](https://amzn.to/4aMyHkI)
|
||||
|
||||
Reference in New Issue
Block a user