WEB开发网
开发学院软件开发Java hand first 设计模式 - 命令模式 阅读

hand first 设计模式 - 命令模式

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示: 关灯操作 Java代码publicclassLightOffCommandimplementsCommand{privateLightlight;publicLightOffCommand(Lightlight){this.light=light;}@Overridepublicvoidexec

关灯操作

Java代码   

public class LightOffCommand implements Command { 
 
 private Light light; 
 
 public LightOffCommand(Light light){ 
 this.light = light; 
 } 
 
 @Override 
 public void execute() { 
 // TODO Auto-generated method stub 
 light.off(); 
 
 } 
 
 @Override 
 public void undo() { 
 // TODO Auto-generated method stub 
 light.on(); 
 
 } 
 
}

电视

Java代码   

public class TV implements Switch { 
 
 private String name; 
 
 public TV(String name) { 
 this.name = name; 
 } 
 
 @Override 
 public void off() { 
 // TODO Auto-generated method stub 
 System.out.println(name + " is off"); 
 
 } 
 
 @Override 
 public void on() { 
 // TODO Auto-generated method stub 
 System.out.println(name + " is on"); 
 
 } 
 
}

//开电视

Java代码   

public class TVOnCommand implements Command { 
 
 private TV tv; 
 
 public TVOnCommand(TV tv) { 
 this.tv = tv; 
 } 
 
 @Override 
 public void execute() { 
 // TODO Auto-generated method stub 
 tv.on(); 
 
 } 
 
 @Override 
 public void undo() { 
 // TODO Auto-generated method stub 
 tv.off(); 
 
 } 
 
}

上一页  1 2 3 4 5  下一页

Tags:hand first 设计模式

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接