|
5 | 5 |
|
6 | 6 | /**
|
7 | 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. |
| 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 by |
| 10 | + * the type of the parameter. |
11 | 11 | * <p>
|
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. |
| 12 | + * One way to implement this would be to create multiple instanceof-checks for the methods |
| 13 | + * parameter. However, this creates a maintenance issue. When new types are added we would also need |
| 14 | + * to change the method's implementation and add a new instanceof-check. This violates the single |
| 15 | + * responsibility principle - a class should have only one reason to change. |
16 | 16 | * <p>
|
17 | 17 | * Instead of the instanceof-checks a better way is to make another virtual call on the parameter
|
18 | 18 | * object. This way new functionality can be easily added without the need to modify existing
|
19 | 19 | * implementation (open-closed principle).
|
20 | 20 | * <p>
|
21 |
| - * In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other. Each |
22 |
| - * object has its own coordinates which are checked against the other objects' coordinates. If |
| 21 | + * In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other. |
| 22 | + * Each object has its own coordinates which are checked against the other objects' coordinates. If |
23 | 23 | * there is an overlap, then the objects collide utilizing the Double Dispatch pattern.
|
24 | 24 | *
|
25 | 25 | */
|
26 | 26 | public class App {
|
27 |
| - |
28 |
| - /** |
29 |
| - * Program entry point |
30 |
| - * @param args command line args |
31 |
| - */ |
32 |
| - public static void main( String[] args ) { |
33 |
| - // initialize game objects and print their status |
34 |
| - List<GameObject> objects = new ArrayList<>(); |
35 |
| - objects.add(new FlamingAsteroid(0, 0, 5, 5)); |
36 |
| - objects.add(new SpaceStationMir(1, 1, 2, 2)); |
37 |
| - objects.add(new Meteoroid(10, 10, 15, 15)); |
38 |
| - objects.add(new SpaceStationIss(12, 12, 14, 14)); |
39 |
| - objects.stream().forEach(o -> System.out.println(o)); |
40 |
| - System.out.println(""); |
41 |
| - |
42 |
| - // collision check |
43 |
| - objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { if (o1 != o2 && o1.intersectsWith(o2)) o1.collision(o2); } )); |
44 |
| - System.out.println(""); |
45 |
| - |
46 |
| - // output eventual object statuses |
47 |
| - objects.stream().forEach(o -> System.out.println(o)); |
48 |
| - System.out.println(""); |
49 |
| - } |
| 27 | + |
| 28 | + /** |
| 29 | + * Program entry point |
| 30 | + * |
| 31 | + * @param args command line args |
| 32 | + */ |
| 33 | + public static void main(String[] args) { |
| 34 | + // initialize game objects and print their status |
| 35 | + List<GameObject> objects = new ArrayList<>(); |
| 36 | + objects.add(new FlamingAsteroid(0, 0, 5, 5)); |
| 37 | + objects.add(new SpaceStationMir(1, 1, 2, 2)); |
| 38 | + objects.add(new Meteoroid(10, 10, 15, 15)); |
| 39 | + objects.add(new SpaceStationIss(12, 12, 14, 14)); |
| 40 | + objects.stream().forEach(o -> System.out.println(o)); |
| 41 | + System.out.println(""); |
| 42 | + |
| 43 | + // collision check |
| 44 | + objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { |
| 45 | + if (o1 != o2 && o1.intersectsWith(o2)) |
| 46 | + o1.collision(o2); |
| 47 | + })); |
| 48 | + System.out.println(""); |
| 49 | + |
| 50 | + // output eventual object statuses |
| 51 | + objects.stream().forEach(o -> System.out.println(o)); |
| 52 | + System.out.println(""); |
| 53 | + } |
50 | 54 | }
|
0 commit comments