Design Patterns: Inversion of Control

Providing flexibility into the domain by abstracting implementation.

0-inversion-of-control

Using abstraction between concrete and class implementation, avoid tightly-coupled classes, which leads to changes in once class to ripple throughout your application.
One method of doing this is the Inversion of Control design pattern which allows the class calling the behavior to be ignorant of its details, or, “loosely-coupled.”

To illustrate this pattern, I’ll begin with the abstraction – an interface through which my class will interact to call Pay().
This interface is where I’ll define just the signature of the behavior I wish to use, but I won’t provide the implementation.

1-inversion-of-control

Now I’ll provide the implementation of Pay(). However, remember that my class which will use this behavior won’t interact with this classes directly.

2-inversion-of-control

Here’s where the magic begins. My CustomerPayment class will act as a conduit between user of the behavior and its implementation.
Notice that it uses an interface instance to call the behavior.
3-inversion-of-control

With wrapper of abstraction completed, I will now use the behavior.

4-inversion-of-control

The benefit of using this design pattern is that if the implementation details of the behavior change, it won’t affect any other classes.5-inversion-of-control

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s