C#職責(zé)鏈模式實(shí)例詳解
本文實(shí)例講述了C#職責(zé)鏈模式。分享給大家供大家參考。具體如下:
ConcreteHandler1.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class ConcreteHandler1:Handler { public override void HandRequest(int request) { if(request>0&&request<10) { Console.WriteLine("{0} 處理請(qǐng)求 {1}",this.GetType().Name,request); } else if(successor!=null) { successor.HandRequest(request); } } } }
ConcreteHandler2.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class ConcreteHandler2:Handler { public override void HandRequest(int request) { if (request > 10 && request < 20) { Console.WriteLine("{0} 處理請(qǐng)求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandRequest(request); } } } }
ConcreteHandler3.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class ConcreteHandler3:Handler { public override void HandRequest(int request) { if (request > 20 && request < 30) { Console.WriteLine("{0} 處理請(qǐng)求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandRequest(request); } } } }
Handler.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public abstract class Handler { protected Handler successor; public void SetSuccessor(Handler successor) { this.successor = successor; } public abstract void HandRequest(int request); } }
Program.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler2(); Handler h3 = new ConcreteHandler3(); h1.SetSuccessor(h2); h2.SetSuccessor(h3); int[] requests = {2,5,14,22,18,3,27,20}; foreach(int request in requests) { h1.HandRequest(request); } Console.ReadKey(); } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法示例
這篇文章主要介紹了C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式分析了C#基于ODBC與OLEDB實(shí)現(xiàn)數(shù)據(jù)庫連接操作簡單操作技巧,需要的朋友可以參考下2017-05-05c#關(guān)于JWT跨域身份驗(yàn)證的實(shí)現(xiàn)代碼
這篇文章主要介紹了c#關(guān)于JWT跨域身份驗(yàn)證的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10C#表達(dá)式中的動(dòng)態(tài)查詢?cè)斀狻咀g】
這篇文章主要給大家介紹了關(guān)于C#表達(dá)式中動(dòng)態(tài)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于C#實(shí)現(xiàn)簡單離線注冊(cè)碼生成與驗(yàn)證
本文使用RSA非對(duì)稱加密和Base64簡單地實(shí)現(xiàn)離線注冊(cè)碼的生成與驗(yàn)證功能。感興趣的朋友跟著小編一起學(xué)習(xí)吧2015-09-09C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12