implements Twin design pattern #63, add credit and rephrase the comments

This commit is contained in:
hoswey 2015-11-16 10:01:03 +08:00
parent fdbfa9e8ee
commit 142274f3f7
2 changed files with 10 additions and 13 deletions

View File

@ -17,4 +17,8 @@ inheritance in java
**Applicability:** Use the Twin idiom when **Applicability:** Use the Twin idiom when
* to simulate multiple inheritance in a language that does not support this feature. * to simulate multiple inheritance in a language that does not support this feature.
* to avoid certain problems of multiple inheritance such as name clashes. * to avoid certain problems of multiple inheritance such as name clashes.
**Credits:**
* [Twin A Design Pattern for Modeling Multiple Inheritance](http://www.ssw.uni-linz.ac.at/Research/Papers/Moe99/Paper.pdf)

View File

@ -3,20 +3,12 @@ package com.iluwatar.twin;
/** /**
* Twin pattern is a design pattern which provides a standard solution to simulate multiple * Twin pattern is a design pattern which provides a standard solution to simulate multiple
* inheritance in java. * inheritance in java.
*
* <p> * <p>
* In this example, there is a ball game, a ball needs to subclass {@link GameItem} which provide * In this example, the essence of the Twin pattern is the {@link BallItem} class and
* some common method like draw and click. Moreover, it needs to subclass {@link Thread} as ball is * {@link BallThread} class represent the twin objects to coordinate with each other(via the twin
* a moving item (we use {@link Thread} instead of implements {@link Runnable} for example only) * reference) like a single class inheriting from {@link GameItem} and {@link Thread}.
* <p>
* Threre is scenario, when user click the ball, the ball will stop, when user click it gain, it
* will resume to move. We create two class, ons is {@link BallItem} which subclass {@link GameItem}
* , another is {@link BallThread} which subclass {@link Thread}. These two object both hold a field
* named "Twin" reference to another object. In {@link BallItem#click()}, it will invoke
* {@link BallThread} to suspend or resume moving; in {@link BallThread#run()}, it will invoke
* {@link BallItem} for drawing.
*
*/ */
public class App { public class App {
/** /**
@ -44,6 +36,7 @@ public class App {
waiting(); waiting();
// exit
ballThread.stopMe(); ballThread.stopMe();
} }