Skip to content

Commit d26454a

Browse files
committed
docs: update twin
1 parent 4b5f34e commit d26454a

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

twin/README.md

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ public class BallItem extends GameItem {
7575
}
7676
}
7777
}
78+
```
7879

80+
```java
7981
@Slf4j
8082
public class BallThread extends Thread {
8183
@Setter
@@ -117,25 +119,85 @@ public class BallThread extends Thread {
117119
To use these classes together:
118120

119121
```java
120-
var ballItem = new BallItem();
121-
var ballThread = new BallThread();
122+
public class App {
123+
124+
public static void main(String[] args) throws Exception {
125+
126+
var ballItem = new BallItem();
127+
var ballThread = new BallThread();
128+
129+
ballItem.setTwin(ballThread);
130+
ballThread.setTwin(ballItem);
131+
132+
ballThread.start();
122133

123-
ballItem.setTwin(ballThread);
124-
ballThread.setTwin(ballItem);
134+
waiting();
135+
136+
ballItem.click();
137+
138+
waiting();
139+
140+
ballItem.click();
141+
142+
waiting();
143+
144+
// exit
145+
ballThread.stopMe();
146+
}
147+
148+
private static void waiting() throws Exception {
149+
Thread.sleep(750);
150+
}
151+
}
125152
```
126153

127-
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`.
128155

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.
130163

131-
![Twin](./etc/twin.png "Twin")
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.
132190

133191
## Applicability
134192

135193
* 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.
136194
* Useful in performance-critical applications where inheritance might introduce unnecessary overhead.
137195
* Applicable in systems requiring resilience through the ability to replace or update one of the twins without affecting the other.
138196

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+
139201
## Known Uses
140202

141203
* User interfaces where different frameworks are used for rendering and logic.
@@ -165,4 +227,3 @@ Trade-offs:
165227
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
166228
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
167229
* [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

Comments
 (0)