软件设计总让人感觉不容易,Design Model也让人觉得难以掌握,下面的小例子是我与朋友讨论这个话题时随手写的,感觉所谓的设计就是把某种机制用面向对象的工具实现出来,"机制"大概就是所谓的原则或原理,"工具"就是各个面向对象语言提供的面向对象的手段,接口啊,类呵什么的,当然不同的语言实现起来略有不同.两者结合就可以做点设计了.呵呵,以上只是自己未有深思熟虑的个人看法.
下面是UML图和Csharp写的代码:
代码
1
using System;
2
using System.Collections;
3
4
namespace cs{
5
6
public class Name{
7
private string strFirstName="李";
8
private string strLastName;
9
public string FirstName{
10
get{ return strFirstName;}
11
set{ strFirstName = value;}
12
}
13
public string LastName{
14
get{ return 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
27
public 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
}
39
public override string ToString(){
40
return strState + strCity;
41
}
42
}//class
43
44
public abstract class Human{
45
protected Name name;
46
public Name Name{
47
get{return name;}
48
set{name = value;}
49
}
50
}//EOF Human
51
52
public class Resident : Human{
53
private Address addr;
54
PoliceStation ps;
55
private IIllegaler attacker;
56
public IIllegaler Attacker{
57
get{return attacker;}
58
}
59
public 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
85
public class Robber:Resident,IRobbing{
86
public string IllegalCode{
87
get{ return "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
}
94
public class Theaf:Resident,IStealing{
95
public string IllegalCode{
96
get{ return "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
104
public interface IIllegaler{
105
string IllegalCode{
106
get;
107
}
108
}
109
public interface IRobbing:IIllegaler{
110
void Robbing( Resident r);
111
}
112
113
public interface IStealing:IIllegaler{
114
void Stealing( Resident r);
115
}
116
117
public interface IArrest{
118
void Arrest( IIllegaler r);
119
}
120
121
public 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
129
public 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
170
public class T{
171
public 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
using System;2
using System.Collections;3
4
namespace cs{5

6
public class Name{7
private string strFirstName="李";8
private string strLastName;9
public string FirstName{10
get{ return strFirstName;}11
set{ strFirstName = value;}12
}13
public string LastName{14
get{ return 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
}//class26

27
public 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
}39
public override string ToString(){40
return strState + strCity;41
}42
}//class43

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

52
public class Resident : Human{53
private Address addr;54
PoliceStation ps;55
private IIllegaler attacker;56
public IIllegaler Attacker{57
get{return attacker;}58
}59
public Address Address{60
get{return addr;}61
set{addr = value;}62
}//Address63

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 Resident84

85
public class Robber:Resident,IRobbing{86
public string IllegalCode{87
get{ return "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
}94
public class Theaf:Resident,IStealing{95
public string IllegalCode{96
get{ return "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

104
public interface IIllegaler{105
string IllegalCode{106
get;107
}108
}109
public interface IRobbing:IIllegaler{110
void Robbing( Resident r); 111
}112

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

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

121
public 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

129
public 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

170
public class T{171
public 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 namespace192

193

执行结果:


浙公网安备 33010602011771号