Skip to content

Commit 1cdd338

Browse files
committed
Commented the code.
1 parent 082f473 commit 1cdd338

File tree

8 files changed

+60
-1
lines changed

8 files changed

+60
-1
lines changed

double-dispatch/src/main/java/com/iluwatar/App.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,43 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**
7+
*
8+
* When a message with a parameter is sent to an object, the resultant behaviour is defined by the
9+
* implementation of that method in the receiver. Sometimes the behaviour must also be determined
10+
* by the type of the parameter.
11+
*
12+
* One way to implement this would be to create multiple instanceof-checks for the methods parameter.
13+
* However, this creates a maintenance issue. When new types are added we would also need to change
14+
* the method's implementation and add a new instanceof-check. This violates the single responsibility
15+
* principle - a class should have only one reason to change.
16+
*
17+
* Instead of the instanceof-checks a better way is to make another virtual call on the parameter
18+
* object. This way new functionality can be easily added without the need to modify existing
19+
* implementation (open-closed principle).
20+
*
21+
* In this example we have hierarchy of objects (GameObject) that can collide to each other. Each
22+
* object has its own coordinates which are checked against the other objects' coordinates. If
23+
* there is an overlap, then the objects collide utilizing the Double Dispatch pattern.
24+
*
25+
*/
626
public class App {
727

828
public static void main( String[] args ) {
29+
// initialize game objects and print their status
930
List<GameObject> objects = new ArrayList<>();
1031
objects.add(new FlamingAsteroid(0, 0, 5, 5));
1132
objects.add(new SpaceStationMir(1, 1, 2, 2));
1233
objects.add(new Meteoroid(10, 10, 15, 15));
1334
objects.add(new SpaceStationIss(12, 12, 14, 14));
14-
1535
objects.stream().forEach(o -> System.out.println(o));
1636
System.out.println("");
1737

38+
// collision check
1839
objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { if (o1 != o2 && o1.intersectsWith(o2)) o1.collision(o2); } ));
1940
System.out.println("");
2041

42+
// output eventual object statuses
2143
objects.stream().forEach(o -> System.out.println(o));
2244
System.out.println("");
2345
}

double-dispatch/src/main/java/com/iluwatar/FlamingAsteroid.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Flaming asteroid game object
6+
*
7+
*/
38
public class FlamingAsteroid extends Meteoroid {
49

510
public FlamingAsteroid(int left, int top, int right, int bottom) {

double-dispatch/src/main/java/com/iluwatar/GameObject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Game objects have coordinates and some
6+
* other status information.
7+
*
8+
*/
39
public abstract class GameObject extends Rectangle {
410

511
private boolean damaged;

double-dispatch/src/main/java/com/iluwatar/Meteoroid.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Meteoroid game object
6+
*
7+
*/
38
public class Meteoroid extends GameObject {
49

510
public Meteoroid(int left, int top, int right, int bottom) {

double-dispatch/src/main/java/com/iluwatar/Rectangle.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Rectangle has coordinates and can be checked for overlap against
6+
* other Rectangles.
7+
*
8+
*/
39
public class Rectangle {
410

511
private int left;

double-dispatch/src/main/java/com/iluwatar/SpaceStationIss.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Space station ISS game object
6+
*
7+
*/
38
public class SpaceStationIss extends SpaceStationMir {
49

510
public SpaceStationIss(int left, int top, int right, int bottom) {

double-dispatch/src/main/java/com/iluwatar/SpaceStationMir.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Space station Mir game object
6+
*
7+
*/
38
public class SpaceStationMir extends GameObject {
49

510
public SpaceStationMir(int left, int top, int right, int bottom) {

double-dispatch/src/test/java/com/iluwatar/RectangleTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55

6+
/**
7+
*
8+
* Unit test for Rectangle
9+
*
10+
*/
611
public class RectangleTest {
712

813
@Test

0 commit comments

Comments
 (0)