-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyInversionViolation.java
More file actions
54 lines (42 loc) · 1.01 KB
/
DependencyInversionViolation.java
File metadata and controls
54 lines (42 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class DependencyInversionViolation {
public static void main(String[] args) {
Switch lightSwitch = new Switch(new LightBulb());
lightSwitch.turnOn();
lightSwitch.turnOff();
Switch fanSwitch = new Switch(new Fan());
fanSwitch.turnOn();
fanSwitch.turnOff();
}
}
interface Switchable {
void turnOn();
void turnOff();
}
class LightBulb implements Switchable {
public void turnOn() {
System.out.println("LightBulb is ON");
}
public void turnOff() {
System.out.println("LightBulb is OFF");
}
}
class Fan implements Switchable {
public void turnOn() {
System.out.println("Fan is ON");
}
public void turnOff() {
System.out.println("Fan is OFF");
}
}
class Switch {
private Switchable device;
public Switch(Switchable device) {
this.device = device;
}
public void turnOn() {
device.turnOn();
}
public void turnOff() {
device.turnOff();
}
}