Created
June 17, 2012 02:42
-
-
Save sanpingz/2943226 to your computer and use it in GitHub Desktop.
命令模式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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