File tree Expand file tree Collapse file tree 6 files changed +88
-0
lines changed
main/java/guru/springframework/blog/dependencyinversionprinciple
test/java/guru/springframework/blog/dependencyinversionprinciple/highlevel Expand file tree Collapse file tree 6 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 1+ package guru .springframework .blog .dependencyinversionprinciple .highlevel ;
2+
3+
4+ public class ElectricPowerSwitch implements Switch {
5+ public Switchable client ;
6+ public boolean on ;
7+ public ElectricPowerSwitch (Switchable client ) {
8+ this .client = client ;
9+ this .on = false ;
10+ }
11+ public boolean isOn () {
12+ return this .on ;
13+ }
14+ public void press (){
15+ boolean checkOn = isOn ();
16+ if (checkOn ) {
17+ client .turnOff ();
18+ this .on = false ;
19+ } else {
20+ client .turnOn ();
21+ this .on = true ;
22+ }
23+
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package guru .springframework .blog .dependencyinversionprinciple .highlevel ;
2+
3+ public interface Switch {
4+ boolean isOn ();
5+ void press ();
6+ }
Original file line number Diff line number Diff line change 1+ package guru .springframework .blog .dependencyinversionprinciple .highlevel ;
2+
3+ public interface Switchable {
4+ void turnOn ();
5+ void turnOff ();
6+ }
Original file line number Diff line number Diff line change 1+ package guru .springframework .blog .dependencyinversionprinciple .lowlevel ;
2+
3+ import guru .springframework .blog .dependencyinversionprinciple .highlevel .Switchable ;
4+
5+ public class Fan implements Switchable {
6+ @ Override
7+ public void turnOn () {
8+ System .out .println ("Fan: Fan turned on..." );
9+ }
10+
11+ @ Override
12+ public void turnOff () {
13+ System .out .println ("Fan: Fan turned off..." );
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package guru .springframework .blog .dependencyinversionprinciple .lowlevel ;
2+
3+ import guru .springframework .blog .dependencyinversionprinciple .highlevel .Switchable ;
4+
5+ public class LightBulb implements Switchable {
6+ @ Override
7+ public void turnOn () {
8+ System .out .println ("LightBulb: Bulb turned on..." );
9+ }
10+
11+ @ Override
12+ public void turnOff () {
13+ System .out .println ("LightBulb: Bulb turned off..." );
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package guru .springframework .blog .dependencyinversionprinciple .highlevel ;
2+
3+ import guru .springframework .blog .dependencyinversionprinciple .lowlevel .Fan ;
4+ import guru .springframework .blog .dependencyinversionprinciple .lowlevel .LightBulb ;
5+ import org .junit .Test ;
6+
7+ public class ElectricPowerSwitchTest {
8+
9+ @ Test
10+ public void testPress () throws Exception {
11+ Switchable switchableBulb =new LightBulb ();
12+ Switch bulbPowerSwitch =new ElectricPowerSwitch (switchableBulb );
13+ bulbPowerSwitch .press ();
14+ bulbPowerSwitch .press ();
15+
16+ Switchable switchableFan =new Fan ();
17+ Switch fanPowerSwitch =new ElectricPowerSwitch (switchableFan );
18+ fanPowerSwitch .press ();
19+ fanPowerSwitch .press ();
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments