Courscomposant2 Spring1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Spring Core Module

Dependency Injection
Sami Bhiri
Outline
• Decoupling interfaces from implementations
• Dependency resolution
• Dependency resolution : definition
• Dependency resolution : modes

April 20 2
Decoupling interfaces from
implementations

April 20 3
Open/Closed principle

• An application should be open for extension, closed for modification


• Its behavior can be extended without changing the source code

⇒ Better maintainability and evolvability

• Key practice : loose coupling between tightly coupled modules

April 20 4
Coupling to classes

April 20 5
Coupling to classes

Class Broker is coupled


to class Negotiator

April 20 6
Coupling to classes

April 20 7
Coupling to classes = Strong coupling

• Class Broker is tightly coupled to class Negotiator


• Class Broker always requires an instance of Negotiator

• We cannot tie Broker to a new version of Negotiator without changing the


code of Broker

⇒ The application is open for modification

April 20 8
Coupling to interfaces

Broker is coupled to the


interface INegotiator

April 20 9
Coupling to interfaces
The attribute neg can be
assigned to any class of type INegotiator

April 20 10
Coupling to interfaces = towards loose
coupling
• Coupling to interfaces is a first step towards loosely coupling
• Open to extension, closed to modification

• Decoupling interfaces from implementations enable modular


programming
• Module = interface + implementation

• Better maintainability, reusability and testability

April 20 11
Dependency resolution

April 20 12
Dependency resolution : definition
• Dependency occurs between an object of class C1 and an object of
class C2 if :
• C1 has an attribute of type C2
• C1 extends C2
• C1 calls a method of C2
• Dependency is transitive

• Dependency resolution : simply assigning a value to a dependency


attribute

April 20 13
Dependency resolutions : example
Provided interface

Object of type Broker is dependent, via the attribute neg, on an


object of type INegotiator

We need to resolve the dependency, otherwise null pointer exception

April 20 14
Dependency resolution: modes
• Static instantiation
• Hard coded
• Dependency injection
• Constructor based
• Property setting based
• Dynamic instantiation
• Programmatically
• Framework based

April 20 15
Hard coded dependency resolution
package modulaire;
• The dependency is resolved
public class Broker implements IBroker { inside the component itself
private INegotiator neg;
• The component is strongly
public Broker(){ coupled to one class
neg = new Negotiator();
}
• Bad resolution to be avoided
public double getBrokerPrice(String productRef){
return 1.07*neg.getStorePrice(productRef);
}
}

April 20 16
Dependency injection : constructor based
public class Broker implements IBroker { public class Facade {

private INegotiator neg; private static IBroker myBroker;

public Broker(INegotiator negotiator){ public static void main(String[] args){


neg = negotiator;
} myBroker = new Broker(new Negotiator());

public double getBrokerPrice(String productRef){


return 1.07*neg.getStorePrice(productRef); System.out.println("Prix = " +
}} myBroker.getBrokerPrice("PCG0037")+" TND");
}}

Dependency is injected by an external entity via the constructor


April 20 17
Dependency injection : constructor based
public class Broker implements IBroker { public class Facade {

private INegotiator neg; private static IBroker myBroker;

public Broker(INegotiator negotiator){ public static void main(String[] args){


neg = negotiator;
} myBroker = new Broker(new NegotiatorCheaper());

public double getBrokerPrice(String productRef){ System.out.println("Prix = " +


return 1.07*neg.getStorePrice(productRef); myBroker.getBrokerPrice("PCG0037")+" TND");
}} }}

• Dependency is injected by an external entity via the constructor


• We can change the configuration of the broker object
April 20 18
Dependency injection : property setting
based
public class Broker implements IBroker { public class Facade {
private static IBroker myBroker;
private INegotiator neg; public static void main(String[] args){
myBroker = new Broker();
public void setNeg(INegotiator neg) {
this.neg = neg; ((Broker) myBroker).setNeg(new
} Negotiator());

public double getBrokerPrice(String


System.out.println("Prix = " +
productRef){
myBroker.getBrokerPrice("PCG0037")+" TND");
return 1.07*neg.getStorePrice(productRef);
}}
}}

Dependency is injected by an external entity via property setting

April 20 19
Dependency injection : property setting
based
public class Broker implements IBroker { public class Facade {
private static IBroker myBroker;
private INegotiator neg; public static void main(String[] args){
myBroker = new Broker();
public void setNeg(INegotiator neg) {
this.neg = neg; ((Broker) myBroker).setNeg(new
} NegotiatorCheaper());

public double getBrokerPrice(String


System.out.println("Prix = " +
productRef){
myBroker.getBrokerPrice("PCG0037")+" TND");
return 1.07*neg.getStorePrice(productRef);
}}
}}

• Dependency is injected by an external entity via property setting


• We can change the configuration of the broker object
April 20 20
Dynamic dependency injection :
programmatically
• Define in a separate configuration file the type of components to
create and the classes to assign to dependency attributes

• An external program reads the configuration file, use introspection to


create the required components and resolve dependencies as
specified in the configuration file

What a framework like Spring does for us

April 20 21
Dynamic dependency injection :
framework based

Will be covered in the next part

April 20 22

You might also like