Dependency Inversion Principle and the Dependency Injection Pattern

Hesam Seyed Mousavi, Jan 22, 2013

Image

The Dependency Inversion principle (DIP) helps to decouple your code by ensuring that you depend on abstractions rather than concrete implementations. This principle, which is paramount to understanding design patterns. Dependency Injection (DI) is an implementation of this principle. You will often find the names Dependency Inversion and Dependency Injection used interchangeably, but they both refer to the same process of decoupling your code.

Interface Segregation Principle
The Interface Segregation principle states that clients should not be forced to depend on interfaces they don’t use.

Intent
As interfaces grow, they take on more responsibility. It is crucial that you ensure they aren’t trying to be all things to all objects. The Interface Segregation principle is all about separating fat interfaces into small, specific groups of related functionality. This enables subclasses to be created that implement only subsets of behavior instead of classes having to implement a monolithic contract littered with the dreaded NotImplementedException.

Liskov Substitution Principle
The Liskov Substitution principle (LSP) states that subclasses must behave the same as their base class.

Factory Pattern
The Factory pattern enables you to encapsulate the creation of objects and remove the responsibility from client code.

•    The Decorator pattern allows you to add behavior and state to an existing class dynamically.

•     The Template pattern defines a skeleton algorithm that delegates to subclasses to implement specific steps that could vary.

•     The State pattern allows you to separate behavior dependent on state from the object itself.

•     The Strategy pattern permits you to swap algorithms dynamically and separate calculations from data.

•     The Specification pattern allows selection criteria logic to be separated and reused from business entities.

•     The Composite pattern enables objects to be combined but act as a single instance to join logic and behavior dynamically.

•     The Layer Supertype pattern acts as a common base class from all objects in the business layer providing implementation for common logic.

•     The Dependency Injection principle inverts the responsibility of obtaining dependent classes by allowing them to be injected via a class’s constructor, property, or method.

•     The Interface Segregation principle splits fat interfaces into separate related groups of contracts, making it easier to use and more understandable in your code.

•     The Liskov Substitution Principle reminds you that subclasses should act as you would expect a base class to be used, without the need to downcast to check for specific subclass behavior.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s