Reformat rest of the design patterns - Issue #224
This commit is contained in:
		| @@ -5,39 +5,40 @@ import java.util.List; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * A system with lots of objects can lead to complexities when a client wants to subscribe  | ||||
|  * to events. The client has to find and register for each object individually, if each  | ||||
|  * object has multiple events then each event requires a separate subscription. | ||||
|  * A system with lots of objects can lead to complexities when a client wants to subscribe to | ||||
|  * events. The client has to find and register for each object individually, if each object has | ||||
|  * multiple events then each event requires a separate subscription. | ||||
|  * <p> | ||||
|  * An Event Aggregator acts as a single source of events for many objects. It registers  | ||||
|  * for all the events of the many objects allowing clients to register with just the aggregator. | ||||
|  * An Event Aggregator acts as a single source of events for many objects. It registers for all the | ||||
|  * events of the many objects allowing clients to register with just the aggregator. | ||||
|  * <p> | ||||
|  * In the example {@link LordBaelish}, {@link LordVarys} and {@link Scout} deliver events to | ||||
|  * {@link KingsHand}. {@link KingsHand}, the event aggregator, then delivers the events | ||||
|  * to {@link KingJoffrey}. | ||||
|  * {@link KingsHand}. {@link KingsHand}, the event aggregator, then delivers the events to | ||||
|  * {@link KingJoffrey}. | ||||
|  * | ||||
|  */ | ||||
| public class App { | ||||
|  | ||||
| 	/** | ||||
| 	 * Program entry point | ||||
| 	 * @param args command line args | ||||
| 	 */ | ||||
| 	public static void main(String[] args) { | ||||
| 		 | ||||
| 		KingJoffrey kingJoffrey = new KingJoffrey(); | ||||
| 		KingsHand kingsHand = new KingsHand(kingJoffrey); | ||||
|   /** | ||||
|    * Program entry point | ||||
|    *  | ||||
|    * @param args command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|  | ||||
| 		List<EventEmitter> emitters = new ArrayList<>(); | ||||
| 		emitters.add(kingsHand); | ||||
| 		emitters.add(new LordBaelish(kingsHand)); | ||||
| 		emitters.add(new LordVarys(kingsHand)); | ||||
| 		emitters.add(new Scout(kingsHand)); | ||||
| 		 | ||||
| 		for (Weekday day: Weekday.values()) { | ||||
| 			for (EventEmitter emitter: emitters) { | ||||
| 				emitter.timePasses(day); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|     KingJoffrey kingJoffrey = new KingJoffrey(); | ||||
|     KingsHand kingsHand = new KingsHand(kingJoffrey); | ||||
|  | ||||
|     List<EventEmitter> emitters = new ArrayList<>(); | ||||
|     emitters.add(kingsHand); | ||||
|     emitters.add(new LordBaelish(kingsHand)); | ||||
|     emitters.add(new LordVarys(kingsHand)); | ||||
|     emitters.add(new Scout(kingsHand)); | ||||
|  | ||||
|     for (Weekday day : Weekday.values()) { | ||||
|       for (EventEmitter emitter : emitters) { | ||||
|         emitter.timePasses(day); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -7,15 +7,16 @@ package com.iluwatar.event.aggregator; | ||||
|  */ | ||||
| public enum Event { | ||||
|  | ||||
| 	STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED("Traitor detected"); | ||||
| 	 | ||||
| 	private String description; | ||||
| 	 | ||||
| 	Event(String description) { | ||||
| 		this.description = description;	 | ||||
| 	} | ||||
| 	 | ||||
|     public String toString() { | ||||
|         return description; | ||||
|     } | ||||
|   STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED( | ||||
|       "Traitor detected"); | ||||
|  | ||||
|   private String description; | ||||
|  | ||||
|   Event(String description) { | ||||
|     this.description = description; | ||||
|   } | ||||
|  | ||||
|   public String toString() { | ||||
|     return description; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -10,26 +10,26 @@ import java.util.List; | ||||
|  */ | ||||
| public abstract class EventEmitter { | ||||
|  | ||||
| 	private List<EventObserver> observers; | ||||
|   private List<EventObserver> observers; | ||||
|  | ||||
| 	public EventEmitter() { | ||||
| 		observers = new LinkedList<>(); | ||||
| 	} | ||||
|   public EventEmitter() { | ||||
|     observers = new LinkedList<>(); | ||||
|   } | ||||
|  | ||||
| 	public EventEmitter(EventObserver obs) { | ||||
| 		this(); | ||||
| 		registerObserver(obs); | ||||
| 	} | ||||
| 	 | ||||
| 	public void registerObserver(EventObserver obs) { | ||||
| 		observers.add(obs); | ||||
| 	} | ||||
| 	 | ||||
| 	protected void notifyObservers(Event e) { | ||||
| 		for (EventObserver obs: observers) { | ||||
| 			obs.onEvent(e); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	public abstract void timePasses(Weekday day); | ||||
|   public EventEmitter(EventObserver obs) { | ||||
|     this(); | ||||
|     registerObserver(obs); | ||||
|   } | ||||
|  | ||||
|   public void registerObserver(EventObserver obs) { | ||||
|     observers.add(obs); | ||||
|   } | ||||
|  | ||||
|   protected void notifyObservers(Event e) { | ||||
|     for (EventObserver obs : observers) { | ||||
|       obs.onEvent(e); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public abstract void timePasses(Weekday day); | ||||
| } | ||||
|   | ||||
| @@ -6,7 +6,7 @@ package com.iluwatar.event.aggregator; | ||||
|  * | ||||
|  */ | ||||
| public interface EventObserver { | ||||
| 	 | ||||
| 	void onEvent(Event e); | ||||
|  | ||||
|   void onEvent(Event e); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -7,8 +7,8 @@ package com.iluwatar.event.aggregator; | ||||
|  */ | ||||
| public class KingJoffrey implements EventObserver { | ||||
|  | ||||
| 	@Override | ||||
| 	public void onEvent(Event e) { | ||||
| 		System.out.println("Received event from the King's Hand: " + e.toString()); | ||||
| 	} | ||||
|   @Override | ||||
|   public void onEvent(Event e) { | ||||
|     System.out.println("Received event from the King's Hand: " + e.toString()); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -2,27 +2,26 @@ package com.iluwatar.event.aggregator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * KingsHand observes events from multiple sources and delivers them | ||||
|  * to listeners. | ||||
|  * KingsHand observes events from multiple sources and delivers them to listeners. | ||||
|  * | ||||
|  */ | ||||
| public class KingsHand extends EventEmitter implements EventObserver { | ||||
|  | ||||
| 	public KingsHand() { | ||||
| 		super(); | ||||
| 	} | ||||
|   public KingsHand() { | ||||
|     super(); | ||||
|   } | ||||
|  | ||||
| 	public KingsHand(EventObserver obs) { | ||||
| 		super(obs); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void onEvent(Event e) { | ||||
| 		notifyObservers(e); | ||||
| 	} | ||||
|   public KingsHand(EventObserver obs) { | ||||
|     super(obs); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public void timePasses(Weekday day) { | ||||
| 		// NOP | ||||
| 	} | ||||
|   @Override | ||||
|   public void onEvent(Event e) { | ||||
|     notifyObservers(e); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void timePasses(Weekday day) { | ||||
|     // NOP | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -6,19 +6,19 @@ package com.iluwatar.event.aggregator; | ||||
|  * | ||||
|  */ | ||||
| public class LordBaelish extends EventEmitter { | ||||
| 	 | ||||
| 	public LordBaelish() { | ||||
| 		super(); | ||||
| 	} | ||||
|  | ||||
| 	public LordBaelish(EventObserver obs) { | ||||
| 		super(obs); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void timePasses(Weekday day) { | ||||
| 		if (day.equals(Weekday.FRIDAY)) { | ||||
| 			notifyObservers(Event.STARK_SIGHTED); | ||||
| 		} | ||||
| 	} | ||||
|   public LordBaelish() { | ||||
|     super(); | ||||
|   } | ||||
|  | ||||
|   public LordBaelish(EventObserver obs) { | ||||
|     super(obs); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void timePasses(Weekday day) { | ||||
|     if (day.equals(Weekday.FRIDAY)) { | ||||
|       notifyObservers(Event.STARK_SIGHTED); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -6,19 +6,19 @@ package com.iluwatar.event.aggregator; | ||||
|  * | ||||
|  */ | ||||
| public class LordVarys extends EventEmitter { | ||||
| 	 | ||||
| 	public LordVarys() { | ||||
| 		super(); | ||||
| 	} | ||||
|  | ||||
| 	public LordVarys(EventObserver obs) { | ||||
| 		super(obs); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void timePasses(Weekday day) { | ||||
| 		if (day.equals(Weekday.SATURDAY)) { | ||||
| 			notifyObservers(Event.TRAITOR_DETECTED); | ||||
| 		} | ||||
| 	} | ||||
|   public LordVarys() { | ||||
|     super(); | ||||
|   } | ||||
|  | ||||
|   public LordVarys(EventObserver obs) { | ||||
|     super(obs); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void timePasses(Weekday day) { | ||||
|     if (day.equals(Weekday.SATURDAY)) { | ||||
|       notifyObservers(Event.TRAITOR_DETECTED); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -6,19 +6,19 @@ package com.iluwatar.event.aggregator; | ||||
|  * | ||||
|  */ | ||||
| public class Scout extends EventEmitter { | ||||
| 	 | ||||
| 	public Scout() { | ||||
| 		super(); | ||||
| 	} | ||||
|  | ||||
| 	public Scout(EventObserver obs) { | ||||
| 		super(obs); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void timePasses(Weekday day) { | ||||
| 		if (day.equals(Weekday.TUESDAY)) { | ||||
| 			notifyObservers(Event.WARSHIPS_APPROACHING); | ||||
| 		} | ||||
| 	} | ||||
|   public Scout() { | ||||
|     super(); | ||||
|   } | ||||
|  | ||||
|   public Scout(EventObserver obs) { | ||||
|     super(obs); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void timePasses(Weekday day) { | ||||
|     if (day.equals(Weekday.TUESDAY)) { | ||||
|       notifyObservers(Event.WARSHIPS_APPROACHING); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -6,16 +6,17 @@ package com.iluwatar.event.aggregator; | ||||
|  * | ||||
|  */ | ||||
| public enum Weekday { | ||||
| 	 | ||||
| 	MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); | ||||
|  | ||||
| 	private String description; | ||||
| 	 | ||||
| 	Weekday(String description) { | ||||
| 		this.description = description; | ||||
| 	} | ||||
| 	 | ||||
| 	public String toString() { | ||||
| 		return description; | ||||
| 	} | ||||
|   MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY( | ||||
|       "Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); | ||||
|  | ||||
|   private String description; | ||||
|  | ||||
|   Weekday(String description) { | ||||
|     this.description = description; | ||||
|   } | ||||
|  | ||||
|   public String toString() { | ||||
|     return description; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| package com.iluwatar.event.aggregator; | ||||
|  | ||||
| import org.junit.Test; | ||||
|  | ||||
| import com.iluwatar.event.aggregator.App; | ||||
| @@ -10,9 +11,9 @@ import com.iluwatar.event.aggregator.App; | ||||
|  */ | ||||
| public class AppTest { | ||||
|  | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		String[] args = {}; | ||||
| 		App.main(args); | ||||
| 	} | ||||
|   @Test | ||||
|   public void test() { | ||||
|     String[] args = {}; | ||||
|     App.main(args); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user