Command模式示例代码

Command模式示例代码
一个简单的例子,它通过Command模式实现回调机制。以Fan(风扇)和Light(灯)为例。我们的目标是设计一个Switch,它可以对任一个对象进行 ‘开’ 和 ‘关’ 的操作。Fan和Light具有不同的接口,这意味着Switch必须独立于接收者接口,或者说,它不清楚接收者的接口。为了解决这一问题,每个Switch都需要合适的command作为参数。很明显,连接到Light的Switch和连接到Fan的Switch具有不同的command。因此,Command类必须是抽象的,或者是个接口。Switch的构造函数被调用时,它以一组合适的command作为参数。command作为Switch的私有变量保存下来。调用flipUp()和flipDown()操作时,它们只是简单地让合适的command进行execute()操作。Switch对调用execute()后将发生些什么一无所知。TestCommand.java
class Fan {
        public void startRotate() {
                System.out.println(‘Fan is rotating’);
        }
        public void stopRotate() {
                System.out.println(‘Fan is not rotating’);
        }
}
class Light {
        public void turnOn( ) {
                System.out.println(‘Light is on ‘);
        }
        public void turnOff( ) {
                System.out.println(‘Light is off’);
        }
}
class Switch {
        private Command UpCommand, DownCommand;
        public Switch( Command Up, Command Down) {
                UpCommand = Up; // concrete Command registers itself with the invoker
                DownCommand = Down;
        }
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver
                        UpCommand . execute ( ) ;
        }
        void flipDown( ) {
                        DownCommand . execute ( );
        }
}
class LightOnCommand implements Command {
        private Light myLight;
        public LightOnCommand ( Light L) {
                myLight  =  L;
        }
        public void execute( ) {
                myLight . turnOn( );
        }
}
class LightOffCommand implements Command {
        private Light myLight;
        public LightOffCommand ( Light L) {
                myLight  =  L;
        }
        public void execute( ) {
                myLight . turnOff( );
        }
}
class FanOnCommand implements Command {
        private Fan myFan;
        public FanOnCommand ( Fan F) {
                myFan  =  F;
        }
        public void execute( ) {
                myFan . startRotate( );
        }
}
class FanOffCommand implements Command {
        private Fan myFan;        public FanOffCommand ( Fan F) {
                myFan  =  F;
        }
        public void execute( ) {
                myFan . stopRotate( );
        }
}
public class TestCommand {
                public static void main(String[] args) {
                        Light  testLight = new Light( );
                        LightOnCommand testLOC = new LightOnCommand(testLight);
                        LightOffCommand testLFC = new LightOffCommand(testLight);
                        Switch testSwitch = new Switch(testLOC,testLFC);      
                        testSwitch.flipUp( );
                        testSwitch.flipDown( );
                        Fan testFan = new Fan( );
                        FanOnCommand foc = new FanOnCommand(testFan);
                        FanOffCommand ffc = new FanOffCommand(testFan);
                        Switch ts = new Switch( foc,ffc);
                        ts.flipUp( );
                        ts.flipDown( );
                }
}               Command.java
public interface Command {
        public abstract void execute ( );
}在上面的示例代码中,Command模式将 ‘调用操作的对象’ (Switch)和 ‘知道如何执行操作的对象’ (Light和Fan)完全分离开来。这带来了很大的灵活性:发送请求的对象只需要知道如何发送;它不必知道如何完成请求。

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s