Refactor the command pattern to use lambda functions
We can leverage the lambda expressins of Java 8 onwards to implement command design pattern instead of traditional non functional way
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 50 KiB  | 
@@ -1,116 +1,89 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true" 
 | 
			
		||||
  realizations="true" associations="true" dependencies="false" nesting-relationships="true">  
 | 
			
		||||
  <class id="1" language="java" name="com.iluwatar.command.ShrinkSpell" project="command" 
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/ShrinkSpell.java" binary="false" corner="BOTTOM_RIGHT">    
 | 
			
		||||
    <position height="178" width="141" x="-30" y="681"/>    
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">      
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    </display>  
 | 
			
		||||
  </class>  
 | 
			
		||||
  <class id="2" language="java" name="com.iluwatar.command.Goblin" project="command" 
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Goblin.java" binary="false" corner="BOTTOM_RIGHT">    
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="1223"/>    
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">      
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    </display>  
 | 
			
		||||
  </class>  
 | 
			
		||||
  <class id="3" language="java" name="com.iluwatar.command.Wizard" project="command" 
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Wizard.java" binary="false" corner="BOTTOM_RIGHT">    
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="362"/>    
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">      
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    </display>  
 | 
			
		||||
  </class>  
 | 
			
		||||
  <class id="4" language="java" name="com.iluwatar.command.Command" project="command" 
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Command.java" binary="false" corner="BOTTOM_RIGHT">    
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="561"/>    
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">      
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    </display>  
 | 
			
		||||
  </class>  
 | 
			
		||||
  <class id="5" language="java" name="com.iluwatar.command.InvisibilitySpell" project="command" 
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java" binary="false" corner="BOTTOM_RIGHT">    
 | 
			
		||||
    <position height="160" width="141" x="151" y="681"/>    
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">      
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    </display>  
 | 
			
		||||
  </class>  
 | 
			
		||||
  <class id="6" language="java" name="com.iluwatar.command.Target" project="command" 
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Target.java" binary="false" corner="BOTTOM_RIGHT">    
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="1014"/>    
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">      
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    </display>  
 | 
			
		||||
  </class>  
 | 
			
		||||
  <association id="7">    
 | 
			
		||||
    <end type="SOURCE" refId="3" navigable="false">      
 | 
			
		||||
      <attribute id="8" name="redoStack">        
 | 
			
		||||
        <position height="20" width="67" x="140" y="451"/>      
 | 
			
		||||
      </attribute>      
 | 
			
		||||
      <multiplicity id="9" minimum="0" maximum="2147483647">        
 | 
			
		||||
        <position height="18" width="25" x="221" y="452"/>      
 | 
			
		||||
      </multiplicity>    
 | 
			
		||||
    </end>    
 | 
			
		||||
    <end type="TARGET" refId="4" navigable="true"/>    
 | 
			
		||||
    <display labels="true" multiplicity="true"/>  
 | 
			
		||||
  </association>  
 | 
			
		||||
  <generalization id="10">    
 | 
			
		||||
    <end type="SOURCE" refId="2"/>    
 | 
			
		||||
    <end type="TARGET" refId="6"/>  
 | 
			
		||||
  </generalization>  
 | 
			
		||||
  <association id="11">    
 | 
			
		||||
    <end type="SOURCE" refId="1" navigable="false">      
 | 
			
		||||
      <attribute id="12" name="target"/>      
 | 
			
		||||
      <multiplicity id="13" minimum="0" maximum="1"/>    
 | 
			
		||||
    </end>    
 | 
			
		||||
    <end type="TARGET" refId="6" navigable="true"/>    
 | 
			
		||||
    <display labels="true" multiplicity="true"/>  
 | 
			
		||||
  </association>  
 | 
			
		||||
  <generalization id="14">    
 | 
			
		||||
    <end type="SOURCE" refId="1"/>    
 | 
			
		||||
    <end type="TARGET" refId="4"/>  
 | 
			
		||||
  </generalization>  
 | 
			
		||||
  <association id="15">    
 | 
			
		||||
    <end type="SOURCE" refId="3" navigable="false">      
 | 
			
		||||
      <attribute id="16" name="undoStack">        
 | 
			
		||||
        <position height="20" width="70" x="-17" y="451"/>      
 | 
			
		||||
      </attribute>      
 | 
			
		||||
      <multiplicity id="17" minimum="0" maximum="2147483647">        
 | 
			
		||||
        <position height="18" width="25" x="60" y="452"/>      
 | 
			
		||||
      </multiplicity>    
 | 
			
		||||
    </end>    
 | 
			
		||||
    <end type="TARGET" refId="4" navigable="true"/>    
 | 
			
		||||
    <display labels="true" multiplicity="true"/>  
 | 
			
		||||
  </association>  
 | 
			
		||||
  <generalization id="18">    
 | 
			
		||||
    <end type="SOURCE" refId="5"/>    
 | 
			
		||||
    <end type="TARGET" refId="4"/>  
 | 
			
		||||
  </generalization>  
 | 
			
		||||
  <association id="19">    
 | 
			
		||||
    <end type="SOURCE" refId="5" navigable="false">      
 | 
			
		||||
      <attribute id="20" name="target"/>      
 | 
			
		||||
      <multiplicity id="21" minimum="0" maximum="1"/>    
 | 
			
		||||
    </end>    
 | 
			
		||||
    <end type="TARGET" refId="6" navigable="true"/>    
 | 
			
		||||
    <display labels="true" multiplicity="true"/>  
 | 
			
		||||
  </association>  
 | 
			
		||||
  <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
 | 
			
		||||
    sort-features="false" accessors="true" visibility="true">    
 | 
			
		||||
    <attributes public="true" package="true" protected="true" private="true" static="true"/>    
 | 
			
		||||
    <operations public="true" package="true" protected="true" private="true" static="true"/>  
 | 
			
		||||
  </classifier-display>  
 | 
			
		||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
 | 
			
		||||
  realizations="true" associations="true" dependencies="false" nesting-relationships="true">
 | 
			
		||||
  <class id="2" language="java" name="com.iluwatar.command.Goblin" project="command"
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Goblin.java" binary="false" corner="BOTTOM_RIGHT">
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="1223"/>
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
    </display>
 | 
			
		||||
  </class>
 | 
			
		||||
  <class id="3" language="java" name="com.iluwatar.command.Wizard" project="command"
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Wizard.java" binary="false" corner="BOTTOM_RIGHT">
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="362"/>
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
    </display>
 | 
			
		||||
  </class>
 | 
			
		||||
  <class id="6" language="java" name="com.iluwatar.command.Target" project="command"
 | 
			
		||||
    file="/command/src/main/java/com/iluwatar/command/Target.java" binary="false" corner="BOTTOM_RIGHT">
 | 
			
		||||
    <position height="-1" width="-1" x="129" y="1014"/>
 | 
			
		||||
    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
 | 
			
		||||
      sort-features="false" accessors="true" visibility="true">
 | 
			
		||||
      <attributes public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
      <operations public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
    </display>
 | 
			
		||||
  </class>
 | 
			
		||||
  <association id="7">
 | 
			
		||||
    <end type="SOURCE" refId="3" navigable="false">
 | 
			
		||||
      <attribute id="8" name="redoStack">
 | 
			
		||||
        <position height="20" width="67" x="140" y="451"/>
 | 
			
		||||
      </attribute>
 | 
			
		||||
      <multiplicity id="9" minimum="0" maximum="2147483647">
 | 
			
		||||
        <position height="18" width="25" x="221" y="452"/>
 | 
			
		||||
      </multiplicity>
 | 
			
		||||
    </end>
 | 
			
		||||
    <end type="TARGET" refId="4" navigable="true"/>
 | 
			
		||||
    <display labels="true" multiplicity="true"/>
 | 
			
		||||
  </association>
 | 
			
		||||
  <generalization id="10">
 | 
			
		||||
    <end type="SOURCE" refId="2"/>
 | 
			
		||||
    <end type="TARGET" refId="6"/>
 | 
			
		||||
  </generalization>
 | 
			
		||||
  <association id="11">
 | 
			
		||||
    <end type="SOURCE" refId="1" navigable="false">
 | 
			
		||||
      <attribute id="12" name="target"/>
 | 
			
		||||
      <multiplicity id="13" minimum="0" maximum="1"/>
 | 
			
		||||
    </end>
 | 
			
		||||
    <end type="TARGET" refId="6" navigable="true"/>
 | 
			
		||||
    <display labels="true" multiplicity="true"/>
 | 
			
		||||
  </association>
 | 
			
		||||
  <generalization id="14">
 | 
			
		||||
    <end type="SOURCE" refId="1"/>
 | 
			
		||||
    <end type="TARGET" refId="4"/>
 | 
			
		||||
  </generalization>
 | 
			
		||||
  <association id="15">
 | 
			
		||||
    <end type="SOURCE" refId="3" navigable="false">
 | 
			
		||||
      <attribute id="16" name="undoStack">
 | 
			
		||||
        <position height="20" width="70" x="-17" y="451"/>
 | 
			
		||||
      </attribute>
 | 
			
		||||
      <multiplicity id="17" minimum="0" maximum="2147483647">
 | 
			
		||||
        <position height="18" width="25" x="60" y="452"/>
 | 
			
		||||
      </multiplicity>
 | 
			
		||||
    </end>
 | 
			
		||||
    <end type="TARGET" refId="4" navigable="true"/>
 | 
			
		||||
    <display labels="true" multiplicity="true"/>
 | 
			
		||||
  </association>
 | 
			
		||||
  <generalization id="18">
 | 
			
		||||
    <end type="SOURCE" refId="5"/>
 | 
			
		||||
    <end type="TARGET" refId="4"/>
 | 
			
		||||
  </generalization>
 | 
			
		||||
  <association id="19">
 | 
			
		||||
    <end type="SOURCE" refId="5" navigable="false">
 | 
			
		||||
      <attribute id="20" name="target"/>
 | 
			
		||||
      <multiplicity id="21" minimum="0" maximum="1"/>
 | 
			
		||||
    </end>
 | 
			
		||||
    <end type="TARGET" refId="6" navigable="true"/>
 | 
			
		||||
    <display labels="true" multiplicity="true"/>
 | 
			
		||||
  </association>
 | 
			
		||||
  <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
 | 
			
		||||
    sort-features="false" accessors="true" visibility="true">
 | 
			
		||||
    <attributes public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
    <operations public="true" package="true" protected="true" private="true" static="true"/>
 | 
			
		||||
  </classifier-display>
 | 
			
		||||
  <association-display labels="true" multiplicity="true"/>
 | 
			
		||||
</class-diagram>
 | 
			
		||||
</class-diagram>
 | 
			
		||||
 
 | 
			
		||||
@@ -4,33 +4,11 @@ package com.iluwatar.command {
 | 
			
		||||
    + App()
 | 
			
		||||
    + main(args : String[]) {static}
 | 
			
		||||
  }
 | 
			
		||||
  interface Command {
 | 
			
		||||
    + Command()
 | 
			
		||||
    + execute(Target) {abstract}
 | 
			
		||||
    + redo() {abstract}
 | 
			
		||||
    + toString() : String {abstract}
 | 
			
		||||
    + undo() {abstract}
 | 
			
		||||
  }
 | 
			
		||||
  class Goblin {
 | 
			
		||||
    + Goblin()
 | 
			
		||||
    + toString() : String
 | 
			
		||||
  }
 | 
			
		||||
  class InvisibilitySpell {
 | 
			
		||||
    - target : Target
 | 
			
		||||
    + InvisibilitySpell()
 | 
			
		||||
    + execute(target : Target)
 | 
			
		||||
    + redo()
 | 
			
		||||
    + toString() : String
 | 
			
		||||
    + undo()
 | 
			
		||||
  }
 | 
			
		||||
  class ShrinkSpell {
 | 
			
		||||
    - oldSize : Size
 | 
			
		||||
    - target : Target
 | 
			
		||||
    + ShrinkSpell()
 | 
			
		||||
    + execute(target : Target)
 | 
			
		||||
    + redo()
 | 
			
		||||
    + toString() : String
 | 
			
		||||
    + undo()
 | 
			
		||||
    + changeSize()
 | 
			
		||||
    + changeVisibility()
 | 
			
		||||
  }
 | 
			
		||||
  enum Size {
 | 
			
		||||
    + NORMAL {static}
 | 
			
		||||
@@ -62,22 +40,19 @@ package com.iluwatar.command {
 | 
			
		||||
  }
 | 
			
		||||
  class Wizard {
 | 
			
		||||
    - LOGGER : Logger {static}
 | 
			
		||||
    - redoStack : Deque<Command>
 | 
			
		||||
    - undoStack : Deque<Command>
 | 
			
		||||
    - redoStack : Deque<Runnable>
 | 
			
		||||
    - undoStack : Deque<Runnable>
 | 
			
		||||
    + Wizard()
 | 
			
		||||
    + castSpell(command : Command, target : Target)
 | 
			
		||||
    + castSpell(Runnable : runnable)
 | 
			
		||||
    + redoLastSpell()
 | 
			
		||||
    + toString() : String
 | 
			
		||||
    + undoLastSpell()
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
Target -->  "-size" Size
 | 
			
		||||
Wizard -->  "-undoStack" Command
 | 
			
		||||
ShrinkSpell -->  "-oldSize" Size
 | 
			
		||||
InvisibilitySpell -->  "-target" Target
 | 
			
		||||
ShrinkSpell -->  "-target" Target
 | 
			
		||||
Wizard -->  "-changeSize" Goblin
 | 
			
		||||
Wizard -->  "-changeVisibility" Goblin
 | 
			
		||||
Target -->  "-visibility" Visibility
 | 
			
		||||
Goblin --|> Target
 | 
			
		||||
InvisibilitySpell ..|> Command
 | 
			
		||||
ShrinkSpell ..|> Command
 | 
			
		||||
App --> "castSpell" Wizard
 | 
			
		||||
@enduml
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user