implements Twin design pattern #63
This commit is contained in:
53
twin/src/main/java/com/iluwatar/twin/App.java
Normal file
53
twin/src/main/java/com/iluwatar/twin/App.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.iluwatar.twin;
|
||||
|
||||
/**
|
||||
* Twin pattern is a design pattern which provides a standard solution to simulate multiple
|
||||
* inheritance in java.
|
||||
*
|
||||
* <p>
|
||||
* In this example, there is a ball game, a ball needs to subclass {@link GameItem} which provide
|
||||
* some common method like draw and click. Moreover, it needs to subclass {@link Thread} as ball is
|
||||
* a moving item (we use {@link Thread} instead of implements {@link Runnable} for example only)
|
||||
* <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 {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
BallItem ballItem = new BallItem();
|
||||
BallThread ballThread = new BallThread();
|
||||
|
||||
ballItem.setTwin(ballThread);
|
||||
ballThread.setTwin(ballItem);
|
||||
|
||||
ballThread.start();
|
||||
|
||||
waiting();
|
||||
|
||||
ballItem.click();
|
||||
|
||||
waiting();
|
||||
|
||||
ballItem.click();
|
||||
|
||||
waiting();
|
||||
|
||||
ballThread.stopMe();
|
||||
}
|
||||
|
||||
private static void waiting() throws Exception {
|
||||
Thread.sleep(2500);
|
||||
}
|
||||
}
|
41
twin/src/main/java/com/iluwatar/twin/BallItem.java
Normal file
41
twin/src/main/java/com/iluwatar/twin/BallItem.java
Normal file
@ -0,0 +1,41 @@
|
||||
|
||||
package com.iluwatar.twin;
|
||||
|
||||
/**
|
||||
* This class represents a Ball which extends {@link GameItem} and implements the logic for ball
|
||||
* item, like move and draw. It hold a reference of {@link BallThread} to delegate the suspend and
|
||||
* resume task.
|
||||
*/
|
||||
public class BallItem extends GameItem {
|
||||
|
||||
private boolean isSuspended = false;
|
||||
|
||||
private BallThread twin;
|
||||
|
||||
public void setTwin(BallThread twin) {
|
||||
this.twin = twin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDraw() {
|
||||
|
||||
System.out.println("doDraw");
|
||||
}
|
||||
|
||||
public void move() {
|
||||
System.out.println("move");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
|
||||
isSuspended = !isSuspended;
|
||||
|
||||
if (isSuspended) {
|
||||
twin.suspendMe();
|
||||
} else {
|
||||
twin.resumeMe();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
53
twin/src/main/java/com/iluwatar/twin/BallThread.java
Normal file
53
twin/src/main/java/com/iluwatar/twin/BallThread.java
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
package com.iluwatar.twin;
|
||||
|
||||
/**
|
||||
* This class is a UI thread for drawing the {@link BallItem}, and provide the method for suspend
|
||||
* and resume. It hold the reference of {@link BallItem} to delegate the draw task.
|
||||
*
|
||||
*/
|
||||
|
||||
public class BallThread extends Thread {
|
||||
|
||||
private BallItem twin;
|
||||
|
||||
private volatile boolean isSuspended;
|
||||
|
||||
private volatile boolean isRunning = true;
|
||||
|
||||
public void setTwin(BallItem twin) {
|
||||
this.twin = twin;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
while (isRunning) {
|
||||
while (!isSuspended) {
|
||||
twin.draw();
|
||||
twin.move();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void suspendMe() {
|
||||
isSuspended = true;
|
||||
System.out.println("Begin to suspend BallThread");
|
||||
}
|
||||
|
||||
public void resumeMe() {
|
||||
isSuspended = false;
|
||||
System.out.println("Begin to resume BallThread");
|
||||
}
|
||||
|
||||
public void stopMe() {
|
||||
this.isRunning = false;
|
||||
this.isSuspended = true;
|
||||
}
|
||||
}
|
||||
|
25
twin/src/main/java/com/iluwatar/twin/GameItem.java
Normal file
25
twin/src/main/java/com/iluwatar/twin/GameItem.java
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
package com.iluwatar.twin;
|
||||
|
||||
/**
|
||||
* GameItem is a common class which provides some common methods for game object.
|
||||
*/
|
||||
public abstract class GameItem {
|
||||
|
||||
/**
|
||||
* Template method, do some common logic before draw
|
||||
*
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
public void draw() {
|
||||
System.out.println("draw");
|
||||
doDraw();
|
||||
}
|
||||
|
||||
public abstract void doDraw();
|
||||
|
||||
|
||||
public abstract void click();
|
||||
}
|
17
twin/src/test/java/com/iluwatar/twin/AppTest.java
Normal file
17
twin/src/test/java/com/iluwatar/twin/AppTest.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.iluwatar.twin;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user