日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

用C#委托实现哨兵和敌人的观察者模式!

發(fā)布時(shí)間:2025/3/15 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用C#委托实现哨兵和敌人的观察者模式! 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?1.代碼如下:

?1?using?System;
?2?using?System.Collections.Generic;
?3?using?System.Text;
?4?
?5?namespace?DelegateImplementObserverPattern
?6?{
?7?????public?class?War
?8?????{
?9?????????static?void?Main(string[]?args)
10?????????{
11?????????????Enemy?enemy?=?new?Enemy();
12?
13?????????????//Using?multiplecast?delegates
14?????????????enemy.FiringEvent?+=?new?FiringEventHandler(Sentry1.Fire);
15?????????????enemy.FiringEvent?+=?new?FiringEventHandler(Sentry2.Fire);

16?
17?????????????enemy.Move();
18?????????}
19?????}
20?
21?????public?delegate?void?FiringEventHandler();
22?
23?????///?<summary>
24?????///?Enemy
25?????///?</summary>
26?????public?class?Enemy
27?????{
28?????????public?event?FiringEventHandler?FiringEvent;
29?
30?????????public?void?Move()
31?????????{
32?????????????FiringEvent();
33?????????}
34?????}
35?
36?????///?<summary>
37?????///?Sentry1
38?????///?</summary>
39?????public?class?Sentry1
40?????{
41?????????///?<summary>
42?????????///?Fire
43?????????///?</summary>
44?????????public?static?void?Fire()
45?????????{
46?????????????Console.WriteLine("Sentry1?firing?at?enemy");
47?????????}
48?????}
49?
50?????///?<summary>
51?????///?Sentry2
52?????///?</summary>
53?????public?class?Sentry2
54?????{
55?????????///?<summary>
56?????????///?Fire
57?????????///?</summary>
58?????????public?static?void?Fire()
59?????????{
60?????????????Console.WriteLine("Sentry2?firing?at?enemy");
61?????????}
62?????}
63?}
64?

2.?代碼如下:

?

?1??namespace?param
?2?????{
?3?????????public?delegate?string?MyEventHandler(string?message);
?4?
?5?????????class?Bridge
?6?????????{
?7?????????????[STAThread]
?8?????????????static?void?Main(string[]?args)
?9?????????????{
10?????????????????MyEventCaller?mec?=?new?MyEventCaller();
11?????????????????MyEventCallee?mee?=?new?MyEventCallee();
12?????????????????mec.MyEvent?+=?new?MyEventHandler(mee.CalleeResponse);
13?
14?????????????????mec.CallerRequest();??
15?????????????}
16??????????}
17?
18?????????public?class?MyEventCaller
19?????????{
20?????????????public?event?MyEventHandler?MyEvent;
21?
22?????????????public?void?CallerRequest()
23?????????????{
24?????????????????MyEvent("Yup");
25?????????????}
26?
27?????????}
28?
29?????????public?class?MyEventCallee
30?????????{
31?????????????public?string?CalleeResponse(string?msg)
32?????????????{
33?????????????????return?"It?is?wonderful";
34?????????????}
35?
36?????????}
37?}

轉(zhuǎn)載于:https://www.cnblogs.com/xuxiaoguang/archive/2008/07/04/1235431.html

總結(jié)

以上是生活随笔為你收集整理的用C#委托实现哨兵和敌人的观察者模式!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。