Java Tip: 实现Command模式[2]

[入库:2005年8月19日] [更新:2007年3月25日]

本文简介:选择自 lostmouse 的 blog

        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( );

本文关键:Command
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top