用C#改写Head First Design Patterns--Command 命令(原创)
2009-07-08 15:51
447 查看
用一个控制器(带有几个开关)来控制所有的电器?神奇而简单的实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
public interface Command
{
void execute();
void execute1();
void execute2();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
/// <summary>
/// 电灯类
/// </summary>
public class Light
{
public Light(){}
public void on()
{
System.Console.WriteLine("开灯");
}
public void off()
{
System.Console.WriteLine("关灯");
}
}
/// <summary>
/// 电视类,具有不同的方法
/// </summary>
public class TV
{
public TV() { }
public void open()
{
System.Console.WriteLine("开电视");
}
public void close()
{
System.Console.WriteLine("关电视");
}
public void changeChannel()
{
System.Console.WriteLine("换台");
}
public void setVolume(int i)
{
System.Console.WriteLine("控制音量"+i.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
public class LightCommands : Command
{
Light light;
public LightCommands(Light l)
{
this.light = l;
}
#region Command 成员
public void execute()
{
light.on();
}
void Command.execute1()
{
light.off();
}
//啥也不做!
void Command.execute2()
{
//throw new NotImplementedException();
}
#endregion
}
public class TVCommands : Command
{
TV tv;
public TVCommands(TV tv)
{
this.tv = tv;
}
#region Command 成员
public void execute()
{
tv.open();
}
void Command.execute1()
{
tv.close();
}
/// <summary>
/// 特别的操作:换台
/// </summary>
void Command.execute2()
{
tv.changeChannel();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
/// <summary>
/// 遥控器,要把它作为万能的控制中心
/// </summary>
public class ControlCenter
{
Command com;
public ControlCenter() { }
public void setCommand(Command c)
{
this.com = c;
}
public void firstButtonPressed()
{
com.execute();
}
public void secondButtonPressed()
{
com.execute1();
}
/// <summary>
/// 特殊的按钮,用于处理特别需求
/// </summary>
public void SpecButtonPressed()
{
com.execute2();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
class Program
{
static void Main(string[] args)
{
ControlCenter cc = new ControlCenter();
Light l=new Light();
Command c = new LightCommands(l);
TV t = new TV();
Command c1 = new TVCommands(t);
cc.setCommand(c);
cc.firstButtonPressed();
cc.SpecButtonPressed();
cc.secondButtonPressed();
cc.setCommand(c1);
cc.firstButtonPressed();
cc.SpecButtonPressed();
cc.secondButtonPressed();
System.Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
public interface Command
{
void execute();
void execute1();
void execute2();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
/// <summary>
/// 电灯类
/// </summary>
public class Light
{
public Light(){}
public void on()
{
System.Console.WriteLine("开灯");
}
public void off()
{
System.Console.WriteLine("关灯");
}
}
/// <summary>
/// 电视类,具有不同的方法
/// </summary>
public class TV
{
public TV() { }
public void open()
{
System.Console.WriteLine("开电视");
}
public void close()
{
System.Console.WriteLine("关电视");
}
public void changeChannel()
{
System.Console.WriteLine("换台");
}
public void setVolume(int i)
{
System.Console.WriteLine("控制音量"+i.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
public class LightCommands : Command
{
Light light;
public LightCommands(Light l)
{
this.light = l;
}
#region Command 成员
public void execute()
{
light.on();
}
void Command.execute1()
{
light.off();
}
//啥也不做!
void Command.execute2()
{
//throw new NotImplementedException();
}
#endregion
}
public class TVCommands : Command
{
TV tv;
public TVCommands(TV tv)
{
this.tv = tv;
}
#region Command 成员
public void execute()
{
tv.open();
}
void Command.execute1()
{
tv.close();
}
/// <summary>
/// 特别的操作:换台
/// </summary>
void Command.execute2()
{
tv.changeChannel();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
/// <summary>
/// 遥控器,要把它作为万能的控制中心
/// </summary>
public class ControlCenter
{
Command com;
public ControlCenter() { }
public void setCommand(Command c)
{
this.com = c;
}
public void firstButtonPressed()
{
com.execute();
}
public void secondButtonPressed()
{
com.execute1();
}
/// <summary>
/// 特殊的按钮,用于处理特别需求
/// </summary>
public void SpecButtonPressed()
{
com.execute2();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Command
{
class Program
{
static void Main(string[] args)
{
ControlCenter cc = new ControlCenter();
Light l=new Light();
Command c = new LightCommands(l);
TV t = new TV();
Command c1 = new TVCommands(t);
cc.setCommand(c);
cc.firstButtonPressed();
cc.SpecButtonPressed();
cc.secondButtonPressed();
cc.setCommand(c1);
cc.firstButtonPressed();
cc.SpecButtonPressed();
cc.secondButtonPressed();
System.Console.ReadLine();
}
}
}
相关文章推荐
- 用C#改写Head First Design Patterns--SingleTon(原创)
- 用C#改写Head First Design Patterns--Adapter 适配器(原创)
- 用C#改写Head First Design Patterns--Strategy策略模式(原创)
- 用C#改写Head First Design Patterns--State(原创)
- 用C#改写Head First Design Patterns--Decorator装饰(原创)
- 用C#改写Head First Design Patterns--Iterator迭代器(原创)
- 用C#改写Head First Design Patterns--Observer观察者(原创)
- 《Head First Design Patterns》笔记六:命令模式(Command Pattern)
- Head First Design Patterns 阅读笔记之六: Command Pattern
- 《Head First Design Patterns》读书笔记之命令模式
- Head First Design Patterns - Command Pattern
- Head First Design patterns笔记-Observer Patterns (从TFS的Project alerts功能看观察者模式)
- Head First Design Patterns - Observer Pattern
- Head First Design Patterns - Template Method Pattern
- 状态模式——Head First Design Patterns
- 进一步学习设计模式——Head First Design Patterns
- 《Head First design patterns》读书笔记
- 《Head First Design Patterns》读书笔记之模板模式
- OOP Design (Head First Design Patterns) 学习笔记--03 Observer Pattern
- Head First Design Patterns(深入浅出设计模式)-目录