Skip to content

Instantly share code, notes, and snippets.

@sanpingz
Created June 17, 2012 02:42
Show Gist options
  • Save sanpingz/2943226 to your computer and use it in GitHub Desktop.
Save sanpingz/2943226 to your computer and use it in GitHub Desktop.
命令模式
import static com.mceiba.util.Print.*;
import java.util.*;
interface Command { void action(); }
enum Signal { RED, YELLOW, GREEN }
public class Commands{
public static void main(String[] args){
EnumMap<Signal, Command> em = new EnumMap<Signal, Command>(Signal.class);
em.put(Signal.RED, new Command(){
public void action() { println("stop"); }
});
em.put(Signal.YELLOW, new Command(){
public void action() { println("wait"); }
});
em.put(Signal.GREEN, new Command(){
public void action() { println("go"); }
});
for(Map.Entry<Signal, Command> e : em.entrySet()){
print(e.getKey()+": ");
e.getValue().action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment