Reformat rest of the design patterns - Issue #224
This commit is contained in:
		| @@ -5,46 +5,50 @@ import java.util.List; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * When a message with a parameter is sent to an object, the resultant behaviour is defined by the  | ||||
|  * implementation of that method in the receiver. Sometimes the behaviour must also be determined  | ||||
|  * by the type of the parameter. | ||||
|  * When a message with a parameter is sent to an object, the resultant behaviour is defined by the | ||||
|  * implementation of that method in the receiver. Sometimes the behaviour must also be determined by | ||||
|  * the type of the parameter. | ||||
|  * <p> | ||||
|  * One way to implement this would be to create multiple instanceof-checks for the methods parameter. | ||||
|  * However, this creates a maintenance issue. When new types are added we would also need to change | ||||
|  * the method's implementation and add a new instanceof-check. This violates the single responsibility | ||||
|  * principle - a class should have only one reason to change. | ||||
|  * One way to implement this would be to create multiple instanceof-checks for the methods | ||||
|  * parameter. However, this creates a maintenance issue. When new types are added we would also need | ||||
|  * to change the method's implementation and add a new instanceof-check. This violates the single | ||||
|  * responsibility principle - a class should have only one reason to change. | ||||
|  * <p> | ||||
|  * Instead of the instanceof-checks a better way is to make another virtual call on the parameter | ||||
|  * object. This way new functionality can be easily added without the need to modify existing | ||||
|  * implementation (open-closed principle). | ||||
|  * <p> | ||||
|  * In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other. Each | ||||
|  * object has its own coordinates which are checked against the other objects' coordinates. If | ||||
|  * In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other. | ||||
|  * Each object has its own coordinates which are checked against the other objects' coordinates. If | ||||
|  * there is an overlap, then the objects collide utilizing the Double Dispatch pattern. | ||||
|  * | ||||
|  */ | ||||
| public class App { | ||||
| 	 | ||||
| 	/** | ||||
| 	 * Program entry point | ||||
| 	 * @param args command line args | ||||
| 	 */ | ||||
|     public static void main( String[] args ) { | ||||
|     	// initialize game objects and print their status | ||||
|     	List<GameObject> objects = new ArrayList<>(); | ||||
|     	objects.add(new FlamingAsteroid(0, 0, 5, 5)); | ||||
|     	objects.add(new SpaceStationMir(1, 1, 2, 2)); | ||||
|     	objects.add(new Meteoroid(10, 10, 15, 15)); | ||||
|     	objects.add(new SpaceStationIss(12, 12, 14, 14)); | ||||
|     	objects.stream().forEach(o -> System.out.println(o)); | ||||
|     	System.out.println(""); | ||||
|     	 | ||||
|     	// collision check | ||||
|     	objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { if (o1 != o2 && o1.intersectsWith(o2)) o1.collision(o2); } )); | ||||
|     	System.out.println(""); | ||||
|     	 | ||||
|     	// output eventual object statuses | ||||
|     	objects.stream().forEach(o -> System.out.println(o)); | ||||
|     	System.out.println(""); | ||||
|     } | ||||
|  | ||||
|   /** | ||||
|    * Program entry point | ||||
|    *  | ||||
|    * @param args command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|     // initialize game objects and print their status | ||||
|     List<GameObject> objects = new ArrayList<>(); | ||||
|     objects.add(new FlamingAsteroid(0, 0, 5, 5)); | ||||
|     objects.add(new SpaceStationMir(1, 1, 2, 2)); | ||||
|     objects.add(new Meteoroid(10, 10, 15, 15)); | ||||
|     objects.add(new SpaceStationIss(12, 12, 14, 14)); | ||||
|     objects.stream().forEach(o -> System.out.println(o)); | ||||
|     System.out.println(""); | ||||
|  | ||||
|     // collision check | ||||
|     objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { | ||||
|       if (o1 != o2 && o1.intersectsWith(o2)) | ||||
|         o1.collision(o2); | ||||
|     })); | ||||
|     System.out.println(""); | ||||
|  | ||||
|     // output eventual object statuses | ||||
|     objects.stream().forEach(o -> System.out.println(o)); | ||||
|     System.out.println(""); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -7,13 +7,13 @@ package com.iluwatar.doubledispatch; | ||||
|  */ | ||||
| public class FlamingAsteroid extends Meteoroid { | ||||
|  | ||||
| 	public FlamingAsteroid(int left, int top, int right, int bottom) { | ||||
| 		super(left, top, right, bottom); | ||||
| 		setOnFire(true); | ||||
| 	} | ||||
|   public FlamingAsteroid(int left, int top, int right, int bottom) { | ||||
|     super(left, top, right, bottom); | ||||
|     setOnFire(true); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collision(GameObject gameObject) { | ||||
| 		gameObject.collisionResolve(this); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collision(GameObject gameObject) { | ||||
|     gameObject.collisionResolve(this); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -2,48 +2,47 @@ package com.iluwatar.doubledispatch; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Game objects have coordinates and some | ||||
|  * other status information. | ||||
|  * Game objects have coordinates and some other status information. | ||||
|  * | ||||
|  */ | ||||
| public abstract class GameObject extends Rectangle { | ||||
| 	 | ||||
| 	private boolean damaged; | ||||
| 	private boolean onFire; | ||||
| 	 | ||||
| 	public GameObject(int left, int top, int right, int bottom) { | ||||
| 		super(left, top, right, bottom); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return String.format("%s at %s damaged=%b onFire=%b", this.getClass().getSimpleName(),  | ||||
| 				super.toString(), isDamaged(), isOnFire()); | ||||
| 	} | ||||
| 	 | ||||
| 	public boolean isOnFire() { | ||||
| 		return onFire; | ||||
| 	} | ||||
| 	 | ||||
| 	public void setOnFire(boolean onFire) { | ||||
| 		this.onFire = onFire; | ||||
| 	} | ||||
| 	 | ||||
| 	public boolean isDamaged() { | ||||
| 		return damaged; | ||||
| 	} | ||||
| 	 | ||||
| 	public void setDamaged(boolean damaged) { | ||||
| 		this.damaged = damaged; | ||||
| 	} | ||||
| 	 | ||||
| 	public abstract void collision(GameObject gameObject); | ||||
| 	 | ||||
| 	public abstract void collisionResolve(FlamingAsteroid asteroid); | ||||
|  | ||||
| 	public abstract void collisionResolve(Meteoroid meteoroid); | ||||
|   private boolean damaged; | ||||
|   private boolean onFire; | ||||
|  | ||||
| 	public abstract void collisionResolve(SpaceStationMir mir); | ||||
|   public GameObject(int left, int top, int right, int bottom) { | ||||
|     super(left, top, right, bottom); | ||||
|   } | ||||
|  | ||||
| 	public abstract void collisionResolve(SpaceStationIss iss); | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return String.format("%s at %s damaged=%b onFire=%b", this.getClass().getSimpleName(), | ||||
|         super.toString(), isDamaged(), isOnFire()); | ||||
|   } | ||||
|  | ||||
|   public boolean isOnFire() { | ||||
|     return onFire; | ||||
|   } | ||||
|  | ||||
|   public void setOnFire(boolean onFire) { | ||||
|     this.onFire = onFire; | ||||
|   } | ||||
|  | ||||
|   public boolean isDamaged() { | ||||
|     return damaged; | ||||
|   } | ||||
|  | ||||
|   public void setDamaged(boolean damaged) { | ||||
|     this.damaged = damaged; | ||||
|   } | ||||
|  | ||||
|   public abstract void collision(GameObject gameObject); | ||||
|  | ||||
|   public abstract void collisionResolve(FlamingAsteroid asteroid); | ||||
|  | ||||
|   public abstract void collisionResolve(Meteoroid meteoroid); | ||||
|  | ||||
|   public abstract void collisionResolve(SpaceStationMir mir); | ||||
|  | ||||
|   public abstract void collisionResolve(SpaceStationIss iss); | ||||
| } | ||||
|   | ||||
| @@ -7,32 +7,36 @@ package com.iluwatar.doubledispatch; | ||||
|  */ | ||||
| public class Meteoroid extends GameObject { | ||||
|  | ||||
| 	public Meteoroid(int left, int top, int right, int bottom) { | ||||
| 		super(left, top, right, bottom); | ||||
| 	} | ||||
|   public Meteoroid(int left, int top, int right, int bottom) { | ||||
|     super(left, top, right, bottom); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collision(GameObject gameObject) { | ||||
| 		gameObject.collisionResolve(this); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collision(GameObject gameObject) { | ||||
|     gameObject.collisionResolve(this); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(FlamingAsteroid asteroid) { | ||||
| 		System.out.println(String.format("%s hits %s.", asteroid.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collisionResolve(FlamingAsteroid asteroid) { | ||||
|     System.out.println(String.format("%s hits %s.", asteroid.getClass().getSimpleName(), this | ||||
|         .getClass().getSimpleName())); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(Meteoroid meteoroid) { | ||||
| 		System.out.println(String.format("%s hits %s.", meteoroid.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collisionResolve(Meteoroid meteoroid) { | ||||
|     System.out.println(String.format("%s hits %s.", meteoroid.getClass().getSimpleName(), this | ||||
|         .getClass().getSimpleName())); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(SpaceStationMir mir) { | ||||
| 		System.out.println(String.format("%s hits %s.", mir.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collisionResolve(SpaceStationMir mir) { | ||||
|     System.out.println(String.format("%s hits %s.", mir.getClass().getSimpleName(), this.getClass() | ||||
|         .getSimpleName())); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(SpaceStationIss iss) { | ||||
| 		System.out.println(String.format("%s hits %s.", iss.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
| 	}	 | ||||
|   @Override | ||||
|   public void collisionResolve(SpaceStationIss iss) { | ||||
|     System.out.println(String.format("%s hits %s.", iss.getClass().getSimpleName(), this.getClass() | ||||
|         .getSimpleName())); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -2,43 +2,46 @@ package com.iluwatar.doubledispatch; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Rectangle has coordinates and can be checked for overlap against | ||||
|  * other Rectangles. | ||||
|  * Rectangle has coordinates and can be checked for overlap against other Rectangles. | ||||
|  * | ||||
|  */ | ||||
| public class Rectangle { | ||||
|  | ||||
| 	private int left; | ||||
| 	private int top; | ||||
| 	private int right; | ||||
| 	private int bottom; | ||||
|   private int left; | ||||
|   private int top; | ||||
|   private int right; | ||||
|   private int bottom; | ||||
|  | ||||
| 	public Rectangle(int left, int top, int right, int bottom) { | ||||
| 		this.left = left; | ||||
| 		this.top = top; | ||||
| 		this.right = right; | ||||
| 		this.bottom = bottom; | ||||
| 	} | ||||
| 	 | ||||
| 	public int getLeft() { | ||||
| 		return left; | ||||
| 	} | ||||
| 	public int getTop() { | ||||
| 		return top; | ||||
| 	} | ||||
| 	public int getRight() { | ||||
| 		return right; | ||||
| 	} | ||||
| 	public int getBottom() { | ||||
| 		return bottom; | ||||
| 	}	 | ||||
| 	 | ||||
| 	boolean intersectsWith(Rectangle r) { | ||||
| 		return !(r.getLeft() > getRight() || r.getRight() < getLeft() || r.getTop() > getBottom() || r.getBottom() < getTop()); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return String.format("[%d,%d,%d,%d]", getLeft(), getTop(), getRight(), getBottom()); | ||||
| 	} | ||||
|   public Rectangle(int left, int top, int right, int bottom) { | ||||
|     this.left = left; | ||||
|     this.top = top; | ||||
|     this.right = right; | ||||
|     this.bottom = bottom; | ||||
|   } | ||||
|  | ||||
|   public int getLeft() { | ||||
|     return left; | ||||
|   } | ||||
|  | ||||
|   public int getTop() { | ||||
|     return top; | ||||
|   } | ||||
|  | ||||
|   public int getRight() { | ||||
|     return right; | ||||
|   } | ||||
|  | ||||
|   public int getBottom() { | ||||
|     return bottom; | ||||
|   } | ||||
|  | ||||
|   boolean intersectsWith(Rectangle r) { | ||||
|     return !(r.getLeft() > getRight() || r.getRight() < getLeft() || r.getTop() > getBottom() || r | ||||
|         .getBottom() < getTop()); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return String.format("[%d,%d,%d,%d]", getLeft(), getTop(), getRight(), getBottom()); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -7,12 +7,12 @@ package com.iluwatar.doubledispatch; | ||||
|  */ | ||||
| public class SpaceStationIss extends SpaceStationMir { | ||||
|  | ||||
| 	public SpaceStationIss(int left, int top, int right, int bottom) { | ||||
| 		super(left, top, right, bottom); | ||||
| 	} | ||||
|   public SpaceStationIss(int left, int top, int right, int bottom) { | ||||
|     super(left, top, right, bottom); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collision(GameObject gameObject) { | ||||
| 		gameObject.collisionResolve(this); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collision(GameObject gameObject) { | ||||
|     gameObject.collisionResolve(this); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -7,45 +7,42 @@ package com.iluwatar.doubledispatch; | ||||
|  */ | ||||
| public class SpaceStationMir extends GameObject { | ||||
|  | ||||
| 	public SpaceStationMir(int left, int top, int right, int bottom) { | ||||
| 		super(left, top, right, bottom); | ||||
| 	} | ||||
|   public SpaceStationMir(int left, int top, int right, int bottom) { | ||||
|     super(left, top, right, bottom); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collision(GameObject gameObject) { | ||||
| 		gameObject.collisionResolve(this); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collision(GameObject gameObject) { | ||||
|     gameObject.collisionResolve(this); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(FlamingAsteroid asteroid) { | ||||
| 		System.out.println(String.format("%s hits %s. %s is damaged! %s is set on fire!",  | ||||
| 				asteroid.getClass().getSimpleName(), this.getClass().getSimpleName(),  | ||||
| 				this.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
| 		setDamaged(true); | ||||
| 		setOnFire(true); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collisionResolve(FlamingAsteroid asteroid) { | ||||
|     System.out.println(String.format("%s hits %s. %s is damaged! %s is set on fire!", asteroid | ||||
|         .getClass().getSimpleName(), this.getClass().getSimpleName(), this.getClass() | ||||
|         .getSimpleName(), this.getClass().getSimpleName())); | ||||
|     setDamaged(true); | ||||
|     setOnFire(true); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(Meteoroid meteoroid) { | ||||
| 		System.out.println(String.format("%s hits %s. %s is damaged!",  | ||||
| 				meteoroid.getClass().getSimpleName(), this.getClass().getSimpleName(),  | ||||
| 				this.getClass().getSimpleName())); | ||||
| 		setDamaged(true); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collisionResolve(Meteoroid meteoroid) { | ||||
|     System.out.println(String.format("%s hits %s. %s is damaged!", meteoroid.getClass() | ||||
|         .getSimpleName(), this.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
|     setDamaged(true); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(SpaceStationMir mir) { | ||||
| 		System.out.println(String.format("%s hits %s. %s is damaged!",  | ||||
| 				mir.getClass().getSimpleName(), this.getClass().getSimpleName(),  | ||||
| 				this.getClass().getSimpleName())); | ||||
| 		setDamaged(true); | ||||
| 	} | ||||
|   @Override | ||||
|   public void collisionResolve(SpaceStationMir mir) { | ||||
|     System.out.println(String.format("%s hits %s. %s is damaged!", mir.getClass().getSimpleName(), | ||||
|         this.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
|     setDamaged(true); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void collisionResolve(SpaceStationIss iss) { | ||||
| 		System.out.println(String.format("%s hits %s. %s is damaged!",  | ||||
| 				iss.getClass().getSimpleName(), this.getClass().getSimpleName(),  | ||||
| 				this.getClass().getSimpleName())); | ||||
| 		setDamaged(true); | ||||
| 	}	 | ||||
|   @Override | ||||
|   public void collisionResolve(SpaceStationIss iss) { | ||||
|     System.out.println(String.format("%s hits %s. %s is damaged!", iss.getClass().getSimpleName(), | ||||
|         this.getClass().getSimpleName(), this.getClass().getSimpleName())); | ||||
|     setDamaged(true); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -10,10 +10,10 @@ import com.iluwatar.doubledispatch.App; | ||||
|  * | ||||
|  */ | ||||
| public class AppTest { | ||||
| 	 | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		String[] args = {}; | ||||
| 		App.main(args); | ||||
| 	} | ||||
|  | ||||
|   @Test | ||||
|   public void test() { | ||||
|     String[] args = {}; | ||||
|     App.main(args); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -12,11 +12,11 @@ import com.iluwatar.doubledispatch.Rectangle; | ||||
|  */ | ||||
| public class RectangleTest { | ||||
|  | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		Assert.assertTrue(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(0,0,1,1))); | ||||
| 		Assert.assertTrue(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(-1,-5,7,8))); | ||||
| 		Assert.assertFalse(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(2,2,3,3))); | ||||
| 		Assert.assertFalse(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(-2,-2,-1,-1))); | ||||
| 	} | ||||
|   @Test | ||||
|   public void test() { | ||||
|     Assert.assertTrue(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(0, 0, 1, 1))); | ||||
|     Assert.assertTrue(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(-1, -5, 7, 8))); | ||||
|     Assert.assertFalse(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(2, 2, 3, 3))); | ||||
|     Assert.assertFalse(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(-2, -2, -1, -1))); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user