Monday, 15 February 2010

oop - Explain this motivational poster about Dependency Inversion Principle -



oop - Explain this motivational poster about Dependency Inversion Principle -

in this blog post, dependency inversion principle described motivational poster:

i don't understand poster means: how soldering lamp straight wall violate dependency inversion principle , how having plugs follow dependency inversion principle. maybe skeleton java or c# code lamps , electrical outlets might helpful.

from link:

the “d” in solid dependency inversion principle, states high-level modules shouldn’t depend on low-level modules, both should depend on shared abstractions.

i not know mean 'shared' abstractions. in code, lamp 'high-level module', , get's it's powerfulness it's dependency, or 'lower-level module'.

your questions:

how soldering lamp straight wall violate dependency inversion principle

the lamp in poster violating dependency inversion principle because not relying on abstract powerfulness source. relies on concrete implementation of beingness soldered straight electrical wiring in wall. if relationship modeled in code, might this:

public class lamp { // part equivalent of "soldering lamp straight electrical wiring in wall" private electricalwiringinbobswall electricalwiring = new electricalwiringinbobswall(); private boolean ison; public void turnon() { electricalwiring.useelectricityinwatts(60); ison = true; } public void turnoff() { electricalwiring.turnoffelectricityinwatts(60); ison = false; } }

the consequences of doing lamp hard utilize in other situations. in real life, lamp require lot of rework if wanted move different part of house, utilize on generator, or take overseas , plug in different outlet. similarily, in code base, if wanted reuse lamp different powerfulness source, have alter code in lamp itself, might require retesting, or introduce new bugs.

how having plugs follow dependency inversion principle

an outlet more abstract because, user, don't have know how lamp gets it's power. have know if plug lamp in, works. outlet abstraction on powerfulness source. say, hides details of powerfulness coming from. there outlet in wall, on generator, an overseas adapter, on solar panel powerfulness source, etc.

employing dip in our class above, want lamp depend on abstraction, , have implementation passed in dependency. in code, might this:

public interface outlet { void useelectricityinwatts(int watts); void turnoffelectricityinwatts(int watts); } public class lamp { private outlet outlet; private boolean ison; // pass in dependency instead of instantiating , rely on interface (abstraction) public void pluginto(outlet outlet) { this.outlet = outlet; } public void turnon() { outlet.useelectricityinwatts(60); ison = true; } public void turnoff() { outlet.turnoffelectricityinwatts(60); ison = false; } }

now, lamp can utilize type of outlet: bobsoutletinhiswall, generatoroutlet, europeanadapteroutlet, etc. can moved , used in other situations, , not have alter @ all.

i know illustration far perfect, hope explains poster means.

oop dependencies solid-principles dip-principle

No comments:

Post a Comment