The DI Design Pattern
DI is a technique whereby one object supplies the dependencies of another object. A dependency of an object is anything required in order to perform its operation in the application. Before diving into an explanation of what DI is, let's try to understand the fundamental element in the preceding definition with an example.
Let's say we have two classes:
As shown in the preceding diagram, Class A uses some properties/methods of Class B. Thus, we can say that ClassB
is a dependency of ClassA
.
Let's look at a more real-world example (albeit simplified). Most websites, whether social media websites, government websites for disbursal of services, or e-commerce platforms, require a user to register in order to use the services offered by the website. Imagine you are developing one such website. You require a UserRegistrationService
class to gather user details, save them in a database...