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
This setup allows `BallItem` and `BallThread` to act together as a single cohesive unit in the game, leveraging the capabilities of both `GameItem` and `Thread` without multiple inheritance.
154
+
Let's break down what happens in `App`.
128
155
129
-
## Class diagram
156
+
1. An instance of `BallItem` and `BallThread` are created.
157
+
2. The `BallItem` and `BallThread` instances are set as twins of each other. This means that each instance has a reference to the other.
158
+
3. The `BallThread` is started. This begins the execution of the `run` method in the `BallThread` class, which continuously calls the `draw` and `move` methods of the `BallItem` (its twin) as long as the `BallThread` is not suspended.
159
+
4. The program waits for 750 milliseconds. This is done to allow the `BallThread` to execute its `run` method a few times.
160
+
5. The `click` method of the `BallItem` is called. This toggles the `isSuspended` state of the `BallItem` and its twin `BallThread`. If the `BallThread` was running, it gets suspended. If it was suspended, it resumes running.
161
+
6. Steps 4 and 5 are repeated twice. This means the `BallThread` is suspended and resumed once.
162
+
7. Finally, the `stopMe` method of the `BallThread` is called to stop its execution.
130
163
131
-

164
+
Console output:
165
+
166
+
```
167
+
14:29:33.778 [Thread-0] INFO com.iluwatar.twin.GameItem -- draw
168
+
14:29:33.780 [Thread-0] INFO com.iluwatar.twin.BallItem -- doDraw
169
+
14:29:33.780 [Thread-0] INFO com.iluwatar.twin.BallItem -- move
170
+
14:29:34.035 [Thread-0] INFO com.iluwatar.twin.GameItem -- draw
171
+
14:29:34.035 [Thread-0] INFO com.iluwatar.twin.BallItem -- doDraw
172
+
14:29:34.035 [Thread-0] INFO com.iluwatar.twin.BallItem -- move
173
+
14:29:34.291 [Thread-0] INFO com.iluwatar.twin.GameItem -- draw
174
+
14:29:34.291 [Thread-0] INFO com.iluwatar.twin.BallItem -- doDraw
175
+
14:29:34.291 [Thread-0] INFO com.iluwatar.twin.BallItem -- move
176
+
14:29:34.533 [main] INFO com.iluwatar.twin.BallThread -- Begin to suspend BallThread
177
+
14:29:35.285 [main] INFO com.iluwatar.twin.BallThread -- Begin to resume BallThread
178
+
14:29:35.308 [Thread-0] INFO com.iluwatar.twin.GameItem -- draw
179
+
14:29:35.308 [Thread-0] INFO com.iluwatar.twin.BallItem -- doDraw
180
+
14:29:35.308 [Thread-0] INFO com.iluwatar.twin.BallItem -- move
181
+
14:29:35.564 [Thread-0] INFO com.iluwatar.twin.GameItem -- draw
182
+
14:29:35.564 [Thread-0] INFO com.iluwatar.twin.BallItem -- doDraw
183
+
14:29:35.565 [Thread-0] INFO com.iluwatar.twin.BallItem -- move
184
+
14:29:35.817 [Thread-0] INFO com.iluwatar.twin.GameItem -- draw
185
+
14:29:35.817 [Thread-0] INFO com.iluwatar.twin.BallItem -- doDraw
186
+
14:29:35.817 [Thread-0] INFO com.iluwatar.twin.BallItem -- move
187
+
```
188
+
189
+
This setup allows `BallItem` and `BallThread` to act together as a single cohesive unit in the game, leveraging the capabilities of both `GameItem` and `Thread` without multiple inheritance.
132
190
133
191
## Applicability
134
192
135
193
* 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.
136
194
* Useful in performance-critical applications where inheritance might introduce unnecessary overhead.
137
195
* Applicable in systems requiring resilience through the ability to replace or update one of the twins without affecting the other.
138
196
197
+
## Tutorials
198
+
199
+
*[Twin – A Design Pattern for Modeling Multiple Inheritance (Hanspeter Mössenböck)](http://www.ssw.uni-linz.ac.at/Research/Papers/Moe99/Paper.pdf)
200
+
139
201
## Known Uses
140
202
141
203
* User interfaces where different frameworks are used for rendering and logic.
@@ -165,4 +227,3 @@ Trade-offs:
165
227
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
166
228
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
167
229
*[Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
168
-
*[Twin – A Design Pattern for Modeling Multiple Inheritance - Hanspeter Mössenböck](http://www.ssw.uni-linz.ac.at/Research/Papers/Moe99/Paper.pdf)
0 commit comments