You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: facade/README.md
+182-1Lines changed: 182 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,188 @@ tags:
15
15
Provide a unified interface to a set of interfaces in a subsystem.
16
16
Facade defines a higher-level interface that makes the subsystem easier to use.
17
17
18
-

18
+
## Explanation
19
+
20
+
Real world example
21
+
22
+
> How does a goldmine work? "Well, the miners go down there and dig gold!" you say. That is what you believe because you are using a simple interface that goldmine provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade.
23
+
24
+
In plain words
25
+
26
+
> Facade pattern provides a simplified interface to a complex subsystem.
27
+
28
+
Wikipedia says
29
+
30
+
> A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
31
+
32
+
**Programmatic Example**
33
+
34
+
Taking our goldmine example from above. Here we have the dwarven mine worker hierarchy
35
+
36
+
```
37
+
public abstract class DwarvenMineWorker {
38
+
39
+
private static final Logger LOGGER = LoggerFactory.getLogger(DwarvenMineWorker.class);
40
+
41
+
public void goToSleep() {
42
+
LOGGER.info("{} goes to sleep.", name());
43
+
}
44
+
45
+
public void wakeUp() {
46
+
LOGGER.info("{} wakes up.", name());
47
+
}
48
+
49
+
public void goHome() {
50
+
LOGGER.info("{} goes home.", name());
51
+
}
52
+
53
+
public void goToMine() {
54
+
LOGGER.info("{} goes to the mine.", name());
55
+
}
56
+
57
+
private void action(Action action) {
58
+
switch (action) {
59
+
case GO_TO_SLEEP:
60
+
goToSleep();
61
+
break;
62
+
case WAKE_UP:
63
+
wakeUp();
64
+
break;
65
+
case GO_HOME:
66
+
goHome();
67
+
break;
68
+
case GO_TO_MINE:
69
+
goToMine();
70
+
break;
71
+
case WORK:
72
+
work();
73
+
break;
74
+
default:
75
+
LOGGER.info("Undefined action");
76
+
break;
77
+
}
78
+
}
79
+
80
+
public void action(Action... actions) {
81
+
for (Action action : actions) {
82
+
action(action);
83
+
}
84
+
}
85
+
86
+
public abstract void work();
87
+
88
+
public abstract String name();
89
+
90
+
static enum Action {
91
+
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
92
+
}
93
+
}
94
+
95
+
public class DwarvenTunnelDigger extends DwarvenMineWorker {
96
+
97
+
private static final Logger LOGGER = LoggerFactory.getLogger(DwarvenTunnelDigger.class);
98
+
99
+
@Override
100
+
public void work() {
101
+
LOGGER.info("{} creates another promising tunnel.", name());
102
+
}
103
+
104
+
@Override
105
+
public String name() {
106
+
return "Dwarven tunnel digger";
107
+
}
108
+
}
109
+
110
+
public class DwarvenGoldDigger extends DwarvenMineWorker {
111
+
112
+
private static final Logger LOGGER = LoggerFactory.getLogger(DwarvenGoldDigger.class);
113
+
114
+
@Override
115
+
public void work() {
116
+
LOGGER.info("{} digs for gold.", name());
117
+
}
118
+
119
+
@Override
120
+
public String name() {
121
+
return "Dwarf gold digger";
122
+
}
123
+
}
124
+
125
+
public class DwarvenCartOperator extends DwarvenMineWorker {
126
+
127
+
private static final Logger LOGGER = LoggerFactory.getLogger(DwarvenCartOperator.class);
128
+
129
+
@Override
130
+
public void work() {
131
+
LOGGER.info("{} moves gold chunks out of the mine.", name());
132
+
}
133
+
134
+
@Override
135
+
public String name() {
136
+
return "Dwarf cart operator";
137
+
}
138
+
}
139
+
140
+
```
141
+
142
+
To operate all these goldmine workers we have the facade
0 commit comments