From the course: Java EE: Contexts and Dependency Injection

What are dependency and coupling?

From the course: Java EE: Contexts and Dependency Injection

Start my 1-month free trial

What are dependency and coupling?

- [Instructor] Before I even start to talk about context and dependency injection, I must define what we mean when we talk about dependency. So what is dependency? Well dependency is about the requirement that a class has to use other classes for it to function properly. Requiring another class's function is a core characteristic of object orientated programming. One class will very often use a functionality of another, in fact this is how we are supposed to write object orientated software. With single purpose classes, that specialize in doing just one thing. So any other class can use the functionality in that class. So if class X uses the class Y, then X is said to be dependent on class Y. And Y is a dependency of class X. Class X may have many dependencies, and class Y may also have many dependencies. This is desirable, as it allows functionality to be reused throughout the application. without duplicating code. Classes that use other classes are said to be coupled. And these dependencies can be either loosely or tightly coupled. Let's have a look at some code that demonstrates tighty coupled classes. Here are two classes, the product service class, the generation EAN 13 barcode for a product. This class depends on the EAN 13 barcode class. Now when a product service class is created, the constructor must be passed an instance of the EAN 13 barcode class, and only the EAN 13 barcode class can be passed. The product service is dependent on this barcode class. This is an example of a tightly coupled class. As no other class, other than this specific EAN 13 barcode class can be used to create a product. It is said that the product service class is tightly coupled to the EAN 13 barcode class. This is considered undesirable, as it means the only barcode that the product service can generate for a product is type EAN 13. So, what if later on we need to generate an EAN 8 or an EAN 5 barcode or some other type of barcode. This design, we cannot. Tightly coupled classes reduce code reuse. Which is an important tenant of object orientated programming. And aides development, code readability, and code standards. As developers, we should strive for loosely coupled designs between objects. So what does a loosely coupled relationship between classes look like. A way to loosen coupling is via interfaces. So the product service class could be made to depend on the code generator interface, rather than the concrete EAN 13 barcode class. Let's have a look at this in code. As you can see the product service is dependent on the code generator interface. And we have three implementations of that interface. EAN 5 barcode, EAN 8 barcode, and EAN 13 barcode. Now, when the product service is instantiated, any one of the three code generator implementations can be passed to the constructor. You can see that in action in the bootstrap class. Where I have created a product service three times. Each time with a different code generator implementation. And if I run the code, you can clearly see, that a different EAN is generated for each product service. This way of achieving loose coupling is perfectly acceptable but, we can do better. And this is where dependency injection comes in.

Contents