CSC/ECE 517 Fall 2009/wiki3 5 SD: Difference between revisions
No edit summary |
|||
Line 153: | Line 153: | ||
== Conclusion == | == Conclusion == | ||
The key to the Dependency Inversion Principle is abstraction. With the abstractions, the details of the system have been isolated from each other, buffered by a wall of stable abstractions. Because of this isolation, changes to some of the details can not propagate throughout the system causing unforeseen harm. Also, the isolated details can be reused in various applications because they do not depend on anything outside of their wall of abstractions | The key to the Dependency Inversion Principle is abstraction. With the abstractions, the details of the system have been isolated from each other, buffered by a wall of stable abstractions. Because of this isolation, changes to some of the details can not propagate throughout the system causing unforeseen harm. Also, the isolated details can be reused in various applications because they do not depend on anything outside of their wall of abstractions | ||
== Index == | == Index == |
Revision as of 15:20, 18 November 2009
Dependency inversion principle
Introduction
The objects in our software should not get into a hierarchy where some objects are in the top-level ones, and dependent on low-level objects. Changes in low-level objects will then ripple-through to top-level objects which makes the software very fragile for change. It is always required that 'top-level' objects be very stable and not fragile for change, therefore the dependencies need to inverted.The point of dependency inversion is to make reusable software.Instead of two pieces of code relying on each other, they rely on some abstracted interface. Then either pieces can be reused without the other.
In conventional application architecture, lower-level components are designed to be consumed by higher-level components which enable increasingly complex systems to be built. In this composition, higher-level components depend directly upon lower-level components to achieve some task. This dependency upon lower-level components makes applications rigid, fragile and immobile. Goal of Dependency inversion principle is to decouple the high-level components from low-level components.
What is Dependency inversion principle?
Dependency inversion principle states that:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend upon details. Details should depend upon abstractions.
In conventional architecture, higher-level components depend upon lower-level components as depicted in the following diagram:[1]
http://4.bp.blogspot.com/_rO0OuTtGb2Y/SVgyGeeYzwI/AAAAAAAAATM/NPsuTSArKIg/s400/DIP1.png
In this diagram, component A depends upon component B, which in turn depends upon component C. Due to these dependencies, each of the higher-level components is coupled with the lower-level components.
The goal of the Dependency Inversion Principle is to decouple higher-level components from their dependency upon lower-level components. This may be achieved by creating interfaces as part of the higher-level component package which define the component’s external needs. This allows the component to be isolated from any particular implementation of the provided interface, thus increasing the component’s portability. The following diagram illustrates this relationship:[2]
http://4.bp.blogspot.com/_rO0OuTtGb2Y/SVgyGil3HQI/AAAAAAAAATU/lezX76UCH7Q/s400/DIP2.png
In this diagram, component A provides an interface which defines the services it needs. Component B satisfies this dependency by implementing the interface. The same relationship is additionally shown for components B and C. Take special note that the interfaces are packaged together with the higher-level components and are defined in terms of the higher-level component’s needs, not the lower-level component’s behavior. It is this association of the interface with the client component which logically inverts the conventional dependency flow.
Examples
Example1:
Consider the following block diagram. The higher level module Copy() depends upon lower level modules ReadUI() and WriteScreen().
The code for the above diagram would look like this:
Void Copy { ReadUI() Writescreen() }
Notice that the two lower modules ReadUI() and WriteScreen() are reusable where as the higher level module Copy() cannot be reused in any other context other than Reading from a UI and Writing to a screen. This is a little inconvinient because there might be some useful logic in Copy() that could have been used to say Write to a file instead to the screen.
By reversing the dependencies as shown below we can reuse the Copy() module:
class Reader { public: virtual int Read() = 0; }; class Writer { public: virtual void Write(char) = 0; }; void Copy(Reader& r, Writer& w) { int c; c = r.Read() w.Write(c); }
Example 2:
Below is an example which violates the Dependency Inversion Principle. There is a manager class which is a high level class, and the low level class Worker. A new module has to be added to the application because in the company there are some new specialized workers employed.So, new class SuperWorker was created for this.Manager class is a complex one containing a very complex logic,and it has to be changed, in order to introduce the new SuperWorker.[3]
Below are the disadvantages:
1.Manager class has to be changed(remember it is a complex one and this will involve some time and effort).
2.Some present functionality from the manager class might be affected.
3.Unit testing should be redone.
All these problems will take a lot of time to solve. Now it would be very simple if the application was designed following the Dependency Inversion Principle. That means the design has the manager class , an IWorker interface and the Worker class implementing the IWorker interface. So,to add the SuperWorker class ,all we have to do is implement the IWorker interface for it.
// Dependency Inversion Principle - Bad example class Worker { public void work() { // ....working } } class Manager { Worker m_worker; public void setWorker(Worker w) { m_worker=w; } public void manage() { m_worker.work(); } } class SuperWorker { public void work() { //.... working much more } }
Below is the code which supports the Dependency Inversion Principle. In this new design a new abstraction layer is added through the IWorker Interface. Now the problems from the above code are solved:
1.Manager class should not be changed.
2.Minimized risk to affect old funtionallity present in Manager class.
3.No need to redone the unit testing for Manager class.
// Dependency Inversion Principle - Good example interface IWorker { public void work(); } class Worker implements IWorker{ public void work() { // ....working } } class SuperWorker implements IWorker{ public void work() { //.... working much more } } class Manager { IWorker m_worker; public void setWorker(IWorker w) { m_worker=w; } public void manage() { m_worker.work(); } }
Conclusion
The key to the Dependency Inversion Principle is abstraction. With the abstractions, the details of the system have been isolated from each other, buffered by a wall of stable abstractions. Because of this isolation, changes to some of the details can not propagate throughout the system causing unforeseen harm. Also, the isolated details can be reused in various applications because they do not depend on anything outside of their wall of abstractions
Index
- Separated Interface Pattern :This pattern reduces coupling by placing the interface definition in a separate package as the implementation.
References
- http://www.ctrl-shift-b.com/2008/12/examining-dependency-inversion.html
- http://en.wikipedia.org/wiki/Dependency_inversion_principle
- http://www.oodesign.com/dependency-inversion-principle.html
- http://www.objectmentor.com/resources/articles/dip.pdf
- http://www.oodesign.com/dependency-inversion-principle.html
- http://doodleproject.sourceforge.net/articles/2001/dependencyInversionPrinciple.html