抽象类,接口,委托与事件
抽象类:
是事物的本质,可以有属性,可以有默认实现.
可以当做共性抽象出来的.
接口:
是事务的行为,不可以有属性,也没有默认实现.
只能是行为的共性抽象出来.
委托:
类似于c中指针的概念,允许程序以方法为作为参数
事件:
是基于委托的,是对象产生的消息.具有多路广播功能.
配合委托完成多种设计模式.
比如一个带报警功能的车门,那么门的大小尺寸,厚度,车门状态是属性,开关是默认行为,开门,关门,报警都是行为.车主和小偷是开门的主体.
那么在设计的时候就应该是
代码
1 //开门的委托方法
2 public delegate string OpenDoor();
3
4 /// <summary>
5 /// 门共有属性类
6 /// </summary>
7 public abstract class Door
8 {
9 //车门的属性在此处无用,仅为表示
10 static int Size
11 {
12 get;
13 set;
14 }
15 static int Thickness
16 {
17 get;
18 set;
19 }
20
21 //车门状态
22 public bool State
23 {
24 get;
25 set;
26 }
27
28 //此处使用事件定义开关门方法
29 public abstract void Open();
30 public abstract void Close();
31
32
33 }
34
35
36 abstract public class ILock
37 {
38 public abstract bool OnLock();
39 }
40
41 /// <summary>
42 /// 报警功能接口
43 /// </summary>
44 public interface IAlarm
45 {
46 void Alarm();
47 }
48
49
50 public class CarLock : ILock
51 {
52
53 public override bool OnLock()
54 {
55 return true;
56 }
57 }
58
59 /// <summary>
60 /// 带有报警功能的车门
61 /// </summary>
62 public class CarDoor : Door,IAlarm
63 {
64 public string myowner;
65 //开门者
66 public event OpenDoor opener;
67 public CarDoor()
68 {
69 this.myowner = "CarOwner";
70
71 }
72
73 /// <summary>
74 /// 开门的时候智能车门会辨别车主名字,这里假设贼和车主都报真名^^
75 /// </summary>
76 public override void Open()
77 {
78
79 if (opener != null)
80 {
81 if (State)
82 {
83 return;
84 }
85 foreach (OpenDoor temp in opener.GetInvocationList())
86 {
87 string ot = temp();
88 if (ot != myowner)
89 {
90 Alarm();
91 return;
92 }
93 Console.WriteLine(temp());
94 }
95 State = true;
96 }
97
98 }
99
100 public override void Close()
101 {
102 State = false;
103 }
104
105
106
107 #region IAlarm 成员
108
109 public void Alarm()
110 {
111 Console.WriteLine(" 报警方式: tel to 110 !");
112 }
113
114 #endregion
115 }
116
117 public class Main
118 {
119 Thief thief = new Thief();
120 CarOwner carowner = new CarOwner();
121 public Main()
122 {
123 CarDoor cardoor = new CarDoor();
124 //监听贼开车门
125 cardoor.opener += new OpenDoor(thief.OpenDoor);
126 //监听车主开门
127 cardoor.opener += new OpenDoor(carowner.OpenDoor);
128 cardoor.Open();
129 }
130 }
131
132 /// <summary>
133 /// 抽象的人
134 /// </summary>
135 public abstract class IPeople
136 {
137 public string Name;
138 public string OpenDoor()
139 {
140 return this.Name;
141 }
142 }
143
144 /// <summary>
145 /// 偷车贼
146 /// </summary>
147 public class Thief : IPeople
148 {
149 public Thief()
150 {
151 this.Name = "Thief";
152 }
153 }
154
155 /// <summary>
156 /// 车主
157 /// </summary>
158 public class CarOwner : IPeople
159 {
160 public CarOwner()
161 {
162 this.Name = "CarOwner";
163 }
164 }
2 public delegate string OpenDoor();
3
4 /// <summary>
5 /// 门共有属性类
6 /// </summary>
7 public abstract class Door
8 {
9 //车门的属性在此处无用,仅为表示
10 static int Size
11 {
12 get;
13 set;
14 }
15 static int Thickness
16 {
17 get;
18 set;
19 }
20
21 //车门状态
22 public bool State
23 {
24 get;
25 set;
26 }
27
28 //此处使用事件定义开关门方法
29 public abstract void Open();
30 public abstract void Close();
31
32
33 }
34
35
36 abstract public class ILock
37 {
38 public abstract bool OnLock();
39 }
40
41 /// <summary>
42 /// 报警功能接口
43 /// </summary>
44 public interface IAlarm
45 {
46 void Alarm();
47 }
48
49
50 public class CarLock : ILock
51 {
52
53 public override bool OnLock()
54 {
55 return true;
56 }
57 }
58
59 /// <summary>
60 /// 带有报警功能的车门
61 /// </summary>
62 public class CarDoor : Door,IAlarm
63 {
64 public string myowner;
65 //开门者
66 public event OpenDoor opener;
67 public CarDoor()
68 {
69 this.myowner = "CarOwner";
70
71 }
72
73 /// <summary>
74 /// 开门的时候智能车门会辨别车主名字,这里假设贼和车主都报真名^^
75 /// </summary>
76 public override void Open()
77 {
78
79 if (opener != null)
80 {
81 if (State)
82 {
83 return;
84 }
85 foreach (OpenDoor temp in opener.GetInvocationList())
86 {
87 string ot = temp();
88 if (ot != myowner)
89 {
90 Alarm();
91 return;
92 }
93 Console.WriteLine(temp());
94 }
95 State = true;
96 }
97
98 }
99
100 public override void Close()
101 {
102 State = false;
103 }
104
105
106
107 #region IAlarm 成员
108
109 public void Alarm()
110 {
111 Console.WriteLine(" 报警方式: tel to 110 !");
112 }
113
114 #endregion
115 }
116
117 public class Main
118 {
119 Thief thief = new Thief();
120 CarOwner carowner = new CarOwner();
121 public Main()
122 {
123 CarDoor cardoor = new CarDoor();
124 //监听贼开车门
125 cardoor.opener += new OpenDoor(thief.OpenDoor);
126 //监听车主开门
127 cardoor.opener += new OpenDoor(carowner.OpenDoor);
128 cardoor.Open();
129 }
130 }
131
132 /// <summary>
133 /// 抽象的人
134 /// </summary>
135 public abstract class IPeople
136 {
137 public string Name;
138 public string OpenDoor()
139 {
140 return this.Name;
141 }
142 }
143
144 /// <summary>
145 /// 偷车贼
146 /// </summary>
147 public class Thief : IPeople
148 {
149 public Thief()
150 {
151 this.Name = "Thief";
152 }
153 }
154
155 /// <summary>
156 /// 车主
157 /// </summary>
158 public class CarOwner : IPeople
159 {
160 public CarOwner()
161 {
162 this.Name = "CarOwner";
163 }
164 }
实现的是一个抽象类,一个接口,而不是一个抽象类三个接口.因为开门和关门都是车门的共性,而报警器功能则不是每个门都有的.
车门会判断车主的名称,如果不是车主就会触发报警.所以将开门的人设置为车门监听的对象.