In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design.
It is a description or template for how to solve a problem that can be used in many different situations.
Why should we use design patterns?
Design patterns can speed up the development process by providing tested, proven development paradigms.
Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.
Scenario
Given a class A which needs to use an instance of class B. How do you implement this idea without creating a direct dependency between 2 classes?
Dependency Injection
Dependency injection is a programming technique that makes a class independent of its dependencies.
The goal of the dependency injection technique is to remove this dependency by separating the usage from the creation of the object. This reduces the amount of required boilerplate code and improves flexibility.
The 4 Roles in Dependency Injection
The service (B) you want to use.
The client (A) that uses the service.
An interface that’s used by the client (A) and implemented by the service (B).
The injector which creates a service instance (B) and injects it into the client (A).
Code example with Spring framework
Quick Recap
Traditional Approach --> need to instantiate an implementation of the Item interface within the Store class itself.
public class Store {
private Item item;
public Store() {
item = new ItemImpl1();
}
}
DI Approach --> no need to specify the implementation of the Item that we want:
public class Store {
private Item item;
public Store(Item item) {
this.item = item;
}
}
The Interface represents the IoC container
In the Spring framework, the interface ApplicationContext is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles
The Spring framework provides several implementations of the ApplicationContext interface:
--> ClassPathXmlApplicationContext && FileSystemXmlApplicationContext for standalone applications
--> WebApplicationContext for web applications.
To instantiate a container
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DI can be done through constructors, setters or fields
What are design patterns?
repeatable solution
to acommonly occurring problem
in software design.Why should we use design patterns?
Scenario
Given a class A which needs to use an instance of class B. How do you implement this idea without creating a direct dependency between 2 classes?
Dependency Injection
independent
of itsdependencies
.The 4 Roles in Dependency Injection
service
(B) you want to use.client
(A) that uses the service.interface
that’s used by the client (A) and implemented by the service (B).injector
which creates a service instance (B) and injects it into the client (A).Code example with Spring framework
Quick Recap
Traditional Approach --> need to instantiate an implementation of the Item interface within the Store class itself.
DI Approach --> no need to specify the implementation of the Item that we want:
The Interface represents the IoC container
ApplicationContext
is responsible for instantiating, configuring and assembling objects known asbeans
, as well as managing their life cyclesApplicationContext
interface: -->ClassPathXmlApplicationContext
&&FileSystemXmlApplicationContext
for standalone applications -->WebApplicationContext
for web applications.DI can be done through constructors, setters or fields