导航

一个设计的小例子-警察抓小偷

Posted on 2007-02-28 12:16  Macro  阅读(2457)  评论(8编辑  收藏  举报

软件设计总让人感觉不容易,Design Model也让人觉得难以掌握,下面的小例子是我与朋友讨论这个话题时随手写的,感觉所谓的设计就是把某种机制用面向对象的工具实现出来,"机制"大概就是所谓的原则或原理,"工具"就是各个面向对象语言提供的面向对象的手段,接口啊,类呵什么的,当然不同的语言实现起来略有不同.两者结合就可以做点设计了.呵呵,以上只是自己未有深思熟虑的个人看法.
下面是UML图和Csharp写的代码:

代码

  1using System;
  2using System.Collections;
  3 
  4namespace cs{
  5
  6public class Name{
  7     private string strFirstName="";
  8     private string strLastName;
  9     public string FirstName{
 10         getreturn strFirstName;}
 11         set{ strFirstName = value;}
 12     }

 13     public string LastName{
 14         getreturn strLastName;}
 15         set{ strLastName = value;}
 16     }

 17    private  Name(){}
 18    public Name(string f,string l)
 19    {
 20        strFirstName = f;strLastName = l;
 21    }

 22    public   string GetName()
 23        return strFirstName + strLastName; 
 24    }

 25}
//class
 26
 27public class Address{
 28  private string strState;//国家
 29  private string strCity; //城市
 30  public Address(){}
 31  public string State{
 32  get{return  strState;}
 33  set{strState = value;}
 34  }

 35  public string City{
 36  get{return  strCity;}
 37  set{strCity = value;}
 38  }

 39public override string ToString(){
 40   return strState + strCity;
 41  }

 42}
//class
 43
 44public abstract class Human{
 45   protected Name name;
 46   public Name Name{
 47     get{return name;}
 48     set{name = value;}
 49   }

 50}
//EOF Human
 51
 52public class Resident : Human{
 53private Address addr;
 54PoliceStation ps;
 55private IIllegaler attacker;
 56public IIllegaler Attacker{
 57  get{return attacker;}
 58}

 59public Address Address{
 60  get{return addr;}
 61  set{addr = value;}
 62  }
//Address
 63
 64  public override string ToString(){
 65   return name.GetName()+addr;
 66  }

 67
 68  public void Regist(PoliceStation ps){
 69     this.ps = ps;
 70  }

 71  private void Accuse(){
 72     Console.WriteLine("居民{0}向{1}警察局报警了",this.Name.GetName(),ps.Name); 
 73     ps.Accept(this);
 74  }

 75  public void IsAttacked(IIllegaler il){
 76         ///居民可能根据性格和情况判断是否报警,此处省略
 77        attacker = il;
 78     
 79        Console.WriteLine("居民{0} 被{1}了",this.Name.GetName(),ps.GetIllegalInfo(il.IllegalCode)); 
 80    this.Accuse();     
 81    
 82  }

 83}
//EOF Resident
 84
 85public class Robber:Resident,IRobbing{
 86  public string IllegalCode{
 87    getreturn "2";}
 88  }

 89  public void Robbing( Resident r){
 90     Console.WriteLine("强盗 {0} 抢劫了{1}他走上了犯罪的道路",((Resident)this).Name.GetName(),r.Name.GetName());
 91     r.IsAttacked(this);   
 92  }
  
 93}

 94public class Theaf:Resident,IStealing{
 95  public string IllegalCode{
 96    getreturn "1";}
 97  }

 98  public void Stealing( Resident r){
 99     Console.WriteLine("贼 {0} 偷窃了{1}他走上了犯罪的道路",((Resident)this).Name.GetName(),r.Name.GetName());
100     r.IsAttacked(this);   
101  }
  
102}

103
104public interface IIllegaler{
105 string IllegalCode{
106 get;
107 }

108}

109public interface IRobbing:IIllegaler{
110   void Robbing( Resident r);  
111}

112
113public interface IStealing:IIllegaler{
114   void Stealing( Resident r);
115}

116
117public interface IArrest{
118   void Arrest( IIllegaler r);
119}

120
121public class Police : Resident,IArrest{
122
123   public void Arrest( IIllegaler r){
124      Resident resident = (Resident)r; 
125      Console.WriteLine("警察{0}机智勇敢地抓捕了犯罪分子:{1}",this.Name.GetName(),resident.Name.GetName());
126   }
      
127}

128
129public class PoliceStation{
130   public static Hashtable illegalTable = new Hashtable();
131   public  const int STUFF_NUM = 10
132   private int currentStuffCount;
133   private string name;
134   public string Name{
135     get{return name;}
136     set{name = value;}
137   }

138
139   private Police [] staff;
140   public string GetIllegalInfo(string code){
141      Hashtable hs = PoliceStation.illegalTable;
142      Object o = hs[code];
143      return (string)o;
144   }

145   public PoliceStation(string name){
146       ///创建STUFF_NUM个警察
147       staff = new Police[STUFF_NUM];
148       currentStuffCount = 0;
149       this.name = name;
150       illegalTable.Add("1","偷窃");
151       illegalTable.Add("2","抢劫");
152
153   }

154   ///受理报警案件
155   public void Accept(Resident r){
156      Console.WriteLine("{0}警察局接到了良民{1}报警",name,r.Name.GetName()); 
157      ///挑选警察
158      Random rand = new Random();
159      int n = rand.Next()%STUFF_NUM;
160      Police p = staff[n];
161      Console.WriteLine("警察 {0} 被委派抓捕违法分子!",p.Name.GetName());
162      p.Arrest(r.Attacker);
163   }

164   public void employ(Police p){
165     staff[currentStuffCount++= p;
166     Console.WriteLine(name + " 警察局 招募了第{0}个警察,警察名字为{1}",currentStuffCount,p.Name.GetName());
167   }

168}

169
170public class T{
171public static void Main(){
172 T t = new T();
173 PoliceStation ps = new PoliceStation("北京市");
174 for (int i=0;i<PoliceStation.STUFF_NUM;i++){
175     Police p = new Police();
176     p.Name = new Name("Police",i.ToString());
177     ps.employ(p);
178 }

179     Robber r = new Robber();
180     r.Name = new Name("Robot","Chao");
181     Resident resident = new Resident();
182     resident.Regist(ps);
183     resident.Name = new Name("John","Push");
184     //Console.WriteLine("");
185     r.Robbing(resident);
186     Theaf theaf = new Theaf();
187     theaf.Name = new Name("Threaf","Tom");
188     theaf.Stealing(resident);
189}

190}

191}
//EOF namespace
192
193

执行结果: