added command sample
This commit is contained in:
		
							
								
								
									
										20
									
								
								command/src/main/java/com/iluwatar/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								command/src/main/java/com/iluwatar/App.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public class App  | ||||
| { | ||||
|     public static void main( String[] args ) | ||||
|     { | ||||
|     	Wizard wizard = new Wizard(); | ||||
|     	Goblin goblin = new Goblin(); | ||||
|  | ||||
|     	goblin.printStatus(); | ||||
|     	 | ||||
|     	wizard.castSpell(new ShrinkSpell(), goblin); | ||||
|     	goblin.printStatus(); | ||||
|     	 | ||||
|     	wizard.castSpell(new InvisibilitySpell(), goblin); | ||||
|     	goblin.printStatus(); | ||||
|     	wizard.undoLastSpell(); | ||||
|     	goblin.printStatus(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								command/src/main/java/com/iluwatar/Command.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								command/src/main/java/com/iluwatar/Command.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public abstract class Command { | ||||
|  | ||||
| 	public abstract void execute(Target target); | ||||
| 	 | ||||
| 	public abstract void undo(); | ||||
|  | ||||
| 	@Override | ||||
| 	public abstract String toString();	 | ||||
| 	 | ||||
| } | ||||
							
								
								
									
										15
									
								
								command/src/main/java/com/iluwatar/Goblin.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								command/src/main/java/com/iluwatar/Goblin.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public class Goblin extends Target { | ||||
|  | ||||
| 	public Goblin() { | ||||
| 		this.setSize(Size.NORMAL); | ||||
| 		this.setVisibility(Visibility.VISIBLE); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "Goblin"; | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										28
									
								
								command/src/main/java/com/iluwatar/InvisibilitySpell.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								command/src/main/java/com/iluwatar/InvisibilitySpell.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public class InvisibilitySpell extends Command { | ||||
|  | ||||
| 	private Target target; | ||||
| 	 | ||||
| 	public InvisibilitySpell() { | ||||
| 		target = null; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void execute(Target target) { | ||||
| 		target.setVisibility(Visibility.INVISIBLE); | ||||
| 		this.target = target; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void undo() { | ||||
| 		if (target != null) { | ||||
| 			target.setVisibility(Visibility.VISIBLE); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "Invisibility spell"; | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										32
									
								
								command/src/main/java/com/iluwatar/ShrinkSpell.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								command/src/main/java/com/iluwatar/ShrinkSpell.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public class ShrinkSpell extends Command { | ||||
|  | ||||
| 	private Size oldSize; | ||||
| 	 | ||||
| 	private Target target; | ||||
| 	 | ||||
| 	public ShrinkSpell() { | ||||
| 		oldSize = null; | ||||
| 		target = null; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void execute(Target target) { | ||||
| 		oldSize = target.getSize(); | ||||
| 		target.setSize(Size.SMALL); | ||||
| 		this.target = target; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void undo() { | ||||
| 		if (oldSize != null && target != null) { | ||||
| 			target.setSize(oldSize); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "Shrink spell"; | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										31
									
								
								command/src/main/java/com/iluwatar/Size.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								command/src/main/java/com/iluwatar/Size.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public enum Size { | ||||
|  | ||||
| 	SMALL, | ||||
| 	NORMAL, | ||||
| 	LARGE; | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		 | ||||
| 		String s = ""; | ||||
| 		 | ||||
| 		switch (this) { | ||||
| 		case LARGE: | ||||
| 			s = "large"; | ||||
| 			break; | ||||
| 		case NORMAL: | ||||
| 			s = "normal"; | ||||
| 			break; | ||||
| 		case SMALL: | ||||
| 			s = "small"; | ||||
| 			break; | ||||
| 		default: | ||||
| 			break; | ||||
| 		} | ||||
| 		return s; | ||||
| 	} | ||||
|  | ||||
| 	 | ||||
| } | ||||
							
								
								
									
										31
									
								
								command/src/main/java/com/iluwatar/Target.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								command/src/main/java/com/iluwatar/Target.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public abstract class Target { | ||||
|  | ||||
| 	private Size size; | ||||
|  | ||||
| 	private Visibility visibility; | ||||
| 	 | ||||
| 	public Size getSize() { | ||||
| 		return size; | ||||
| 	} | ||||
|  | ||||
| 	public void setSize(Size size) { | ||||
| 		this.size = size; | ||||
| 	} | ||||
|  | ||||
| 	public Visibility getVisibility() { | ||||
| 		return visibility; | ||||
| 	} | ||||
|  | ||||
| 	public void setVisibility(Visibility visibility) { | ||||
| 		this.visibility = visibility; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public abstract String toString(); | ||||
| 	 | ||||
| 	public void printStatus() { | ||||
| 		System.out.println(String.format("%s, size=%s visibility=%s", this, getSize(), getVisibility())); | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										26
									
								
								command/src/main/java/com/iluwatar/Visibility.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								command/src/main/java/com/iluwatar/Visibility.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public enum Visibility { | ||||
|  | ||||
| 	VISIBLE, | ||||
| 	INVISIBLE; | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		 | ||||
| 		String s = ""; | ||||
| 		 | ||||
| 		switch (this) { | ||||
| 		case INVISIBLE: | ||||
| 			s = "invisible"; | ||||
| 			break; | ||||
| 		case VISIBLE: | ||||
| 			s = "visible"; | ||||
| 			break; | ||||
| 		default: | ||||
| 			break; | ||||
| 		 | ||||
| 		} | ||||
| 		return s; | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										31
									
								
								command/src/main/java/com/iluwatar/Wizard.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								command/src/main/java/com/iluwatar/Wizard.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| public class Wizard extends Target { | ||||
|  | ||||
| 	private Command previousSpell; | ||||
| 	 | ||||
| 	public Wizard() { | ||||
| 		this.setSize(Size.NORMAL); | ||||
| 		this.setVisibility(Visibility.VISIBLE); | ||||
| 		previousSpell = null; | ||||
| 	} | ||||
| 	 | ||||
| 	public void castSpell(Command command, Target target) { | ||||
| 		System.out.println(this + " casts " + command + " at " + target); | ||||
| 		command.execute(target); | ||||
| 		previousSpell = command; | ||||
| 	} | ||||
|  | ||||
| 	public void undoLastSpell() { | ||||
| 		if (previousSpell != null) { | ||||
| 			System.out.println(this + " undoes " + previousSpell); | ||||
| 			previousSpell.undo(); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return "Wizard"; | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user