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: twin/README.md
+49-38
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,10 @@ description: "Explore the Twin design pattern in Java with examples. Learn how t
5
5
category: Structural
6
6
language: en
7
7
tag:
8
-
- Decoupling
9
-
- Object composition
10
-
- Performance
11
-
- Resilience
8
+
- Decoupling
9
+
- Object composition
10
+
- Performance
11
+
- Resilience
12
12
---
13
13
14
14
## Intent of Twin Design Pattern
@@ -21,11 +21,11 @@ Real-world example
21
21
22
22
> An analogous real-world example of the Twin design pattern can be found in the relationship between a driver and a driving simulator. Imagine a driver (the first class) and a driving simulator (the second class) that both need to interact with the same set of vehicle controls (steering, acceleration, braking) and receive the same feedback (speed, engine status).
23
23
>
24
-
> Despite performing similar functions, the driver and the simulator cannot share a common base class because they operate in fundamentally different environments—one in the physical world and the other in a virtual environment. Instead, they are "twinned" to ensure consistent interaction with the vehicle controls and feedback mechanisms. This setup allows improvements or changes to be made to the simulator without affecting the driver and vice versa, maintaining the system's overall flexibility and resilience.
24
+
> Despite performing similar functions, the driver and the simulator cannot share a common base class because they operate in fundamentally different environments—one in the physical world and the other in a virtual environment. Instead, they are "twinned" to ensure consistent interaction with the vehicle controls and feedback mechanisms. This setup allows improvements or changes to be made to the simulator without affecting the driver and vice versa, maintaining the system's overall flexibility and resilience.
25
25
26
26
In plain words
27
27
28
-
> It provides a way to form two closely coupled subclasses that can act as a twin class having two ends.
28
+
> It provides a way to form two closely coupled subclasses that can act as a twin class having two ends.
29
29
30
30
Wikipedia says
31
31
@@ -84,36 +84,47 @@ public class BallItem extends GameItem {
84
84
publicclassBallThreadextendsThread {
85
85
@Setter
86
86
privateBallItem twin;
87
-
privatevolatileboolean isSuspended;
88
87
privatevolatileboolean isRunning =true;
88
+
privateboolean isSuspended =false;
89
89
90
90
publicvoidrun() {
91
-
while (isRunning) {
92
-
if (!isSuspended) {
91
+
synchronized (this) {
92
+
while (isRunning) {
93
+
while (isSuspended) {
94
+
try {
95
+
wait();
96
+
} catch (InterruptedException e) {
97
+
Thread.currentThread().interrupt();
98
+
return;
99
+
}
100
+
}
93
101
twin.draw();
94
102
twin.move();
95
-
}
96
-
try {
97
-
Thread.sleep(250);
98
-
} catch (InterruptedException e) {
99
-
thrownewRuntimeException(e);
103
+
try {
104
+
Thread.sleep(250);
105
+
} catch (InterruptedException e) {
106
+
Thread.currentThread().interrupt();
107
+
return;
108
+
}
100
109
}
101
110
}
102
111
}
103
112
104
-
publicvoidsuspendMe() {
113
+
publicsynchronizedvoidsuspendMe() {
105
114
isSuspended =true;
106
-
LOGGER.info("Begin to suspend BallThread");
115
+
LOGGER.info("Suspending BallThread");
107
116
}
108
117
109
-
publicvoidresumeMe() {
118
+
publicsynchronizedvoidresumeMe() {
110
119
isSuspended =false;
111
-
LOGGER.info("Begin to resume BallThread");
120
+
notify();
121
+
LOGGER.info("Resuming BallThread");
112
122
}
113
123
114
-
publicvoidstopMe() {
115
-
this.isRunning =false;
116
-
this.isSuspended =true;
124
+
publicsynchronizedvoidstopMe() {
125
+
isRunning =false;
126
+
isSuspended =false;
127
+
notify();
117
128
}
118
129
}
119
130
```
@@ -192,40 +203,40 @@ This setup allows `BallItem` and `BallThread` to act together as a single cohesi
192
203
193
204
## When to Use the Twin Pattern in Java
194
205
195
-
* Use when you need to decouple classes that share common functionality but cannot inherit from a common base class due to various reasons such as the use of different frameworks or languages.
196
-
* Useful in performance-critical applications where inheritance might introduce unnecessary overhead.
197
-
* Applicable in systems requiring resilience through the ability to replace or update one of the twins without affecting the other.
206
+
- Use when you need to decouple classes that share common functionality but cannot inherit from a common base class due to various reasons such as the use of different frameworks or languages.
207
+
- Useful in performance-critical applications where inheritance might introduce unnecessary overhead.
208
+
- Applicable in systems requiring resilience through the ability to replace or update one of the twins without affecting the other.
198
209
199
210
## Twin Pattern Java Tutorials
200
211
201
-
*[Twin – A Design Pattern for Modeling Multiple Inheritance (Hanspeter Mössenböck)](http://www.ssw.uni-linz.ac.at/Research/Papers/Moe99/Paper.pdf)
212
+
-[Twin – A Design Pattern for Modeling Multiple Inheritance (Hanspeter Mössenböck)](http://www.ssw.uni-linz.ac.at/Research/Papers/Moe99/Paper.pdf)
202
213
203
214
## Real-World Applications of Twin Pattern in Java
204
215
205
-
* User interfaces where different frameworks are used for rendering and logic.
206
-
* Systems integrating legacy code with new implementations where direct inheritance is not feasible.
216
+
- User interfaces where different frameworks are used for rendering and logic.
217
+
- Systems integrating legacy code with new implementations where direct inheritance is not feasible.
207
218
208
219
## Benefits and Trade-offs of Twin Pattern
209
220
210
221
Benefits:
211
222
212
-
* Reduces coupling between classes, promoting modularity and easier maintenance.
213
-
* Improves flexibility and reuse of classes across different frameworks or languages.
214
-
* Enhances performance by avoiding the overhead associated with inheritance.
223
+
- Reduces coupling between classes, promoting modularity and easier maintenance.
224
+
- Improves flexibility and reuse of classes across different frameworks or languages.
225
+
- Enhances performance by avoiding the overhead associated with inheritance.
215
226
216
227
Trade-offs:
217
228
218
-
* Can lead to code duplication if not managed properly.
219
-
* Increased complexity in managing the interaction between twin classes.
229
+
- Can lead to code duplication if not managed properly.
230
+
- Increased complexity in managing the interaction between twin classes.
220
231
221
232
## Related Java Design Patterns
222
233
223
-
*[Adapter](https://java-design-patterns.com/patterns/adapter/): Both patterns deal with compatibility issues, but Adapter focuses on converting interfaces while Twin deals with class collaboration without inheritance.
224
-
*[Bridge](https://java-design-patterns.com/patterns/bridge/): Similar in decoupling abstraction from implementation, but Twin specifically avoids inheritance.
225
-
*[Proxy](https://java-design-patterns.com/patterns/proxy/): Manages object access, similar to how Twin handles interaction, but Proxy typically focuses on control and logging.
234
+
-[Adapter](https://java-design-patterns.com/patterns/adapter/): Both patterns deal with compatibility issues, but Adapter focuses on converting interfaces while Twin deals with class collaboration without inheritance.
235
+
-[Bridge](https://java-design-patterns.com/patterns/bridge/): Similar in decoupling abstraction from implementation, but Twin specifically avoids inheritance.
236
+
-[Proxy](https://java-design-patterns.com/patterns/proxy/): Manages object access, similar to how Twin handles interaction, but Proxy typically focuses on control and logging.
226
237
227
238
## References and Credits
228
239
229
-
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
230
-
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
231
-
*[Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
240
+
-[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
241
+
-[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
242
+
-[Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
0 commit comments