public enum absolutedirection
{
 north=0, east, south, west
}
public enum relativedirection
{
 right, left, back
}
public interface ivehiclecontrol
{
 void accelerate(int paramamount);
 void decelerate(int paramamount);
 void turn(relativedirection paramdirection); 
}
public interface ivehiclemodel
{
 string name{ get; set;}
 int speed{ get; set;}
 int maxspeed{ get;}
 int maxturnspeed{ get;}
 int maxreversespeed { get;}
 absolutedirection direction{get; set;}
 void turn(relativedirection paramdirection);
 void accelerate(int paramamount);
 void decelerate(int paramamount);
}
public interface ivehicleview
{
 void disableacceleration();
 void enableacceleration();
 void disabledeceleration();
 void enabledeceleration();
 void disableturning();
 void enableturning();
 }
public interface ivehiclecontrol
{
 void requestaccelerate(int paramamount);
 void requestdecelerate(int paramamount);
 void requestturn(relativedirection paramdirection); 
 void setmodel(ivehiclemodel paramauto);
 void setview(ivehicleview paramview);
}
public interface ivehiclemodel
{
 string name{ get; set;}
 int speed{ get; set;}
 int maxspeed{ get;}
 int maxturnspeed{ get;}
 int maxreversespeed { get;}
 absolutedirection direction{get; set;}
 void turn(relativedirection paramdirection);
 void accelerate(int paramamount);
 void decelerate(int paramamount);
 void addobserver(ivehicleview paramview);
 void removeobserver(ivehicleview paramview);
 void notifyobservers();
} 
public class ivehicleview
{
 void disableacceleration();
 void enableacceleration();
 void disabledeceleration();
 void enabledeceleration();
 void disableturning();
 void enableturning();
 void update(ivehiclemodel parammodel);
}
public abstract class automobile: ivehiclemodel
{
 "declarations "#region "declarations "
 private arraylist alist = new arraylist();
 private int mintspeed = 0;
 private int mintmaxspeed = 0;
 private int mintmaxturnspeed = 0;
 private int mintmaxreversespeed = 0;
 private absolutedirection mdirection = absolutedirection.north;
 private string mstrname = "";
 #endregion
 "constructor"#region "constructor"
 public automobile(int parammaxspeed, int parammaxturnspeed, int parammaxreversespeed, string paramname)
 {
  this.mintmaxspeed = parammaxspeed;
  this.mintmaxturnspeed = parammaxturnspeed;
  this.mintmaxreversespeed = parammaxreversespeed;
  this.mstrname = paramname;
 }
 #endregion
 "ivehiclemodel members"#region "ivehiclemodel members"
 public void addobserver(ivehicleview paramview)
 {
  alist.add(paramview);
 }
 public void removeobserver(ivehicleview paramview)
 {
  alist.remove(paramview);
 }
 public void notifyobservers()
 {
  foreach(ivehicleview view in alist)
  {
   view.update(this);
  }
 }
 public string name
 {
  get
  {
   return this.mstrname;
  }
  set
  {
   this.mstrname = value;
  }
 }
 public int speed
 {
  get
  {
   return this.mintspeed;
  }
 }
 public int maxspeed
 {
  get
  {
   return this.mintmaxspeed;
  }
 }
 public int maxturnspeed
 {
  get
  {
   return this.mintmaxturnspeed;
  }
 }
 public int maxreversespeed
 {
  get
  {
   return this.mintmaxreversespeed;
  }
 }
 public absolutedirection direction
 {
  get
  {
   return this.mdirection;
  }
 }
 public void turn(relativedirection paramdirection)
 {
  absolutedirection newdirection;
  switch(paramdirection)
  {
   case relativedirection.right:
    newdirection = (absolutedirection)((int)(this.mdirection + 1) %4);
    break;
   case relativedirection.left:
    newdirection = (absolutedirection)((int)(this.mdirection + 3) %4);
    break;
   case relativedirection.back:
    newdirection = (absolutedirection)((int)(this.mdirection + 2) %4);
    break;
   default:
    newdirection = absolutedirection.north;
    break;
  }
  this.mdirection = newdirection;
  this.notifyobservers();
 }
 public void accelerate(int paramamount)
 {
  this.mintspeed += paramamount;
  if(mintspeed >= this.mintmaxspeed) mintspeed = mintmaxspeed;
  this.notifyobservers();
 }
 public void decelerate(int paramamount)
 {
  this.mintspeed -= paramamount;
  if(mintspeed <= this.mintmaxreversespeed) mintspeed = mintmaxreversespeed;
  this.notifyobservers();
 }
 #endregion
}
public class automobilecontrol: ivehiclecontrol
{
 private ivehiclemodel model;
 private ivehicleview view;
 public automobilecontrol(ivehiclemodel parammodel, ivehicleview paramview)
 {
  this.model = parammodel;
  this.view = paramview;
 }
 public automobilecontrol()
 {}
 ivehiclecontrol members#region ivehiclecontrol members
 public void setmodel(ivehiclemodel parammodel)
 {
  this.model = parammodel;
 }
 public void setview(ivehicleview paramview)
 {
  this.view = paramview;
 }
 public void requestaccelerate(int paramamount)
 {
  if(model != null)
  {
   model.accelerate(paramamount);
   if(view != null) setview();
  }
 }
 public void requestdecelerate(int paramamount)
 {
  if(model != null)
  {
   model.decelerate(paramamount);
   if(view != null) setview();
  }
 }
 public void requestturn(relativedirection paramdirection)
 {
  if(model != null)
  {
   model.turn(paramdirection);
   if(view != null) setview();
  }
 }
 #endregion
 public void setview()
 {
  if(model.speed >= model.maxspeed)
  {
   view.disableacceleration();
   view.enabledeceleration();
  }
  else if(model.speed <= model.maxreversespeed)
  {
   view.disabledeceleration();
   view.enableacceleration();
  }
  else
  {
   view.enableacceleration();
   view.enabledeceleration();
  }
  if(model.speed >= model.maxturnspeed)
  {
   view.disableturning();
  }
  else
  {
   view.enableturning();
  }
 }
}
public class acme2000sportscar:automobile
{
 public acme2000sportscar(string paramname):base(250, 40, -20, paramname){}
 public acme2000sportscar(string paramname, int parammaxspeed, int parammaxturnspeed, int parammaxreversespeed):
 base(parammaxspeed, parammaxturnspeed, parammaxreversespeed, paramname){}
}
public class autoview : system.windows.forms.usercontrol, ivehicleview
{ 
 private ivehiclecontrol control = new acme.automobilecontrol(); 
 private ivehiclemodel model = new acme.acme2000sportscar("speedy");
} 
public autoview()
{
 // this call is required by the windows.forms form designer.
 initializecomponent();
 wireup(control, model);
}
public void wireup(ivehiclecontrol paramcontrol, ivehiclemodel parammodel)
{
 // if we're switching models, don't keep watching
 // the old one! 
 if(model != null)
 {
  model.removeobserver(this);
 }
 model = parammodel;
 control = paramcontrol;
 control.setmodel(model);
 control.setview(this);
 model.addobserver(this);
}
private void btnaccelerate_click(object sender, system.eventargs e)
{
 control.requestaccelerate(int.parse(this.txtamount.text));
}
private void btndecelerate_click(object sender, system.eventargs e)
{
 control.requestdecelerate(int.parse(this.txtamount.text));
}
private void btnleft_click(object sender, system.eventargs e)
{
 control.requestturn(relativedirection.left);
}
private void btnright_click(object sender, system.eventargs e)
{
 control.requestturn(relativedirection.right);
}
public void updateinterface(ivehiclemodel auto)
{
 this.label1.text = auto.name + " heading " + auto.direction.tostring() + " at speed: " + auto.speed.tostring();
 this.pbar.value = (auto.speed>0)? auto.speed*100/auto.maxspeed : auto.speed*100/auto.maxreversespeed;
}
public void disableacceleration()
{
 this.btnaccelerate.enabled = false;
}
public void enableacceleration()
{
 this.btnaccelerate.enabled = true;
}
public void disabledeceleration()
{
 this.btndecelerate.enabled = false;
}
public void enabledeceleration()
{
 this.btndecelerate.enabled = true;
}
public void disableturning()
{
 this.btnright.enabled = this.btnleft.enabled = false;
}
public void enableturning()
{
 this.btnright.enabled = this.btnleft.enabled = true;
}
public void update(ivehiclemodel parammodel)
{
 this.updateinterface(parammodel);
}
public class acme2000truck: automobile
{
 public acme2000truck(string paramname):base(80, 25, -12, paramname){}
 public acme2000truck(string paramname, int parammaxspeed, int parammaxturnspeed, int parammaxreversespeed):
base(parammaxspeed, parammaxturnspeed, parammaxreversespeed, paramname){} 
}
private void btnbuildnew_click(object sender, system.eventargs e)
{
 this.autoview1.wireup(new acme.automobilecontrol(), new acme.acme2000truck(this.txtname.text));
}
public void requestaccelerate(int paramamount)
{
 if(model != null)
 {
  int amount = paramamount;
  if(amount > 5) amount = 5;
  model.accelerate(amount);
  if(view != null) setview();
 }
}
public void requestdecelerate(int paramamount)
{
 if(model != null)
 {
  int amount = paramamount;
  if(amount > 5) amount = 5;
  model.accelerate(amount);
  model.decelerate(amount);
  if(view != null) setview();
 }
}
private void btnbuildnew_click(object sender, system.eventargs e)
{
 this.autoview1.wireup(new acme.slowpokecontrol(), new acme.acme2000truck(this.txtname.text));
}
新闻热点
疑难解答