Friday, February 09, 2007

[Tech Tips] Implementing Command Design Pattern via Spring Framework - Part 1


I been using Spring Framework for pass few months, and I am big fans of the Framework. The framework help a lot, as we don't have to write lengthly, repetitive boring code, most application code are wire via Spring XML configuration, the framework also make our code cleaner, as it provide out of box AOP engine, developer could focus on implementing module core function, and “AOPing” other programming aspects such as transaction control, logging, security, and etc when require.

Thus, to say a big thank u on providing such great framework, and contribute back to the communities, starting from today, I will use this blog to share my experience, tips, and journal of using Spring here. Most of the write up will assume u have some basic idea of using Spring (Just do a google search on Spring tutorial).

So, my very first 2007 technical topic is “Implementing Command Design Pattern using Spring”. I will use the remote control example from very popular design pattern book, “Head First Design Patterns” from OREILLY.

The original requirement of the remote control are

1 . The remote control will have multiple buttons,
2. Each button could be program to turn on/off a household device.

I change the requirement a little, the remote control will not have any buttons, but:

1. It will accept command as a string from a console
2. Each device's command will have unique command String, such as

The very first thing we need to do is to roll out the command interface


1 package blog.coolboy.springexample.cp.command;
2
3 public interface Command {
4 public CommandType getCommandType();
5 public void execute();
6 }


Next, we needs to define an enum to hold all possible supported CommandType, each CommandType holds it's command string, which is unique. That's a helper method to return CommandType base on passing command string.
 1 package blog.coolboy.springexample.cp.command;
2
3 public enum CommandType {
4 LIGHT_ON("light.on"),
5 LIGHT_OFF("light.off"),
6 TV_ON("tv.on"),
7 TV_OFF("tv.off");
8
9 private String commandString;
10
11 // return CommandType base on passing
12 // commandString
13 public static CommandType valueOfCommandString(String cmdString) {
14 CommandType theCommandType = null;
15 for(CommandType cmdType : CommandType.values()) {
16 if (cmdType.getCommandString().equals(cmdString)) {
17 theCommandType = cmdType;
18 break;
19 }
20
21 }
22 return theCommandType;
23 }
24
25 CommandType(String cmdString) {
26 commandString = cmdString;
27 }
28
29 public String getCommandString() {
30 return commandString;
31 }
32
33 public void setCommandString(String commandString) {
34 this.commandString = commandString;
35 }
36
37 }
38

Now, implements all supported commands, below is sample code TV ON Command;

 1 package blog.coolboy.springexample.cp.command.impl;
2
3 import blog.coolboy.springexample.cp.command.Command;
4 import blog.coolboy.springexample.cp.command.CommandType;
5
6
7 public class TVOnCommand implements Command {
8
9 private final static CommandType cmdType = CommandType.TV_ON;
10
11 public TVOnCommand() {
12 // U suppose to pass in a TV instance here..
13 }
14
15 public CommandType getCommandType() {
16 return this.cmdType;
17 }
18
19 public void execute() {
20 System.out.println("Switching On TV");
21 }
22
23 }

The rest of commands (TV.Off, Light On, Light.Off) follows the same structure..

In part II, we will roll out the RemoteControl class, and use Spring to inject supported commands to the Remotecontrol, and provide remoteControlService as an interactive shell for user to test out the remote control, stay tune.

No comments: