18不使用委托实现能自动侦测车距的智能汽车

发明一辆智能汽车,能自动侦测与前方车辆的车距,当车距小于100米的时候,就报警。

 

□ 思路

● 报警器显然是观察者
● 智能汽车就是被观察者
智能汽车有一个侦测车距的方法,当车距小于100米的时候,就触发一个事件。这个事件就遍历所有注册的观察者,让每个观察这作出响应。

 

被观察者接口只负责注册和取消注册。

   1:      //被观察者接口
   2:      public interface IObservable
   3:      {
   4:          void Register(IObserver obj);
   5:          void Unregister(IObserver obj);
   6:      }


观察者接口也只有一个行为。

 

   1:      //观察者接口
   2:      public interface IObserver
   3:      {
   4:          void Update();
   5:      }

还需要一个被观察者抽象基类实现被观察者接口,他维护了一个被观察者的集合,并实施注册、取消注册和通知观察者,通知观察者的方法涉及为virtual,需要抽象类的子类来触发。

 

   1:  //被观察者抽象基类
   2:      public abstract class SubjectBase : IObservable
   3:      {
   4:          private List<IObserver>  container = new List<IObserver>();
   5:   
   6:          public void Register(IObserver obj)
   7:          {
   8:              container.Add(obj);
   9:          }
  10:   
  11:          public void Unregister(IObserver obj)
  12:          {
  13:              container.Remove(obj);
  14:          }
  15:   
  16:          protected virtual void Notify()
  17:          {
  18:              foreach (IObserver observer in container)
  19:              {
  20:                  observer.Update();
  21:              }
  22:          }
  23:      }
  24:   

 

智能汽车就继承了被观察者这个抽象基类,他不仅有测距的方法,还有当车距小于100米时触发事件,触发事件的实质就是调用被观察者抽象基类的通知方法,让观察者作出响应。

 

   1:    public class SmartCar : SubjectBase
   2:      {
   3:          private string brand = string.Empty;
   4:          private string type = string.Empty;
   5:          private int distance;
   6:   
   7:          public SmartCar(string brand, string type)
   8:          {
   9:              this.brand = brand;
  10:              this.type = type;
  11:              this.distance = 1000;
  12:          }
  13:   
  14:          public string Brand {get { return brand; }}
  15:          public string Type {get { return type; }}
  16:   
  17:          public SmartCar():this("大众","Polo"){}
  18:   
  19:          protected virtual void OnDistance()
  20:          {
  21:              base.Notify();//也就是遍历观察者,让观察者对动作有响应
  22:          }
  23:   
  24:          public void DetectDistance()
  25:          {
  26:              for (int i = 200; i > 98; i--)
  27:              {
  28:                  distance = i;
  29:                  if (distance < 100)
  30:                  {
  31:                      OnDistance();
  32:                  }
  33:              }
  34:          }
  35:      }
  36:   
  37:  报警器就是观察者,只需要作响应。
  38:   
  39:      public class Alarm : IObserver
  40:      {
  41:          public void Update()
  42:          {
  43:              Console.WriteLine("嘀嘀嘀:与前方车距小于100米,请保持好车距!");
  44:          }
  45:      }
  46:   

 

主程序先让被观察者注册观察者,然后运行被观察者的方法,当车距小于100米,就触发被观察者的通知方法,实际上调用的是被观察者抽象基类的虚方法,挨个通知观察者作出响应。

 

   1:    class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              SmartCar smartCar = new SmartCar();
   6:              Alarm alarm = new Alarm();
   7:   
   8:              smartCar.Register(alarm);
   9:              smartCar.DetectDistance();
  10:              Console.ReadKey();
  11:          }
  12:      }       
  13:   
  14:   
  15:   

结果:

1

你可能感兴趣的:(实现)