国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#職責鏈模式實例詳解

2019-10-29 21:40:51
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#職責鏈模式,以實例形式完整分析了C#職責鏈模式的相關技巧與實現方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#職責鏈模式。分享給大家供大家參考。具體如下:

ConcreteHandler1.cs如下:

 

 
  1. ?using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. public class ConcreteHandler1:Handler 
  7. public override void HandRequest(int request) 
  8. if(request>0&&request<10) 
  9. Console.WriteLine("{0} 處理請求 {1}",this.GetType().Name,request); 
  10. else if(successor!=null
  11. successor.HandRequest(request); 

ConcreteHandler2.cs如下:

 

 
  1. ?using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. public class ConcreteHandler2:Handler 
  7. public override void HandRequest(int request) 
  8. if (request > 10 && request < 20) 
  9. Console.WriteLine("{0} 處理請求 {1}"this.GetType().Name, request); 
  10. else if (successor != null
  11. successor.HandRequest(request); 

ConcreteHandler3.cs如下:

 

 
  1. ?using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. public class ConcreteHandler3:Handler 
  7. public override void HandRequest(int request) 
  8. if (request > 20 && request < 30) 
  9. Console.WriteLine("{0} 處理請求 {1}"this.GetType().Name, request); 
  10. else if (successor != null
  11. successor.HandRequest(request); 

Handler.cs如下:

 

 
  1. ?using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. public abstract class Handler 
  7. protected Handler successor; 
  8. public void SetSuccessor(Handler successor)  
  9. this.successor = successor; 
  10. public abstract void HandRequest(int request); 

Program.cs如下:

 

 
  1. ?using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. class Program 
  7. static void Main(string[] args) 
  8. Handler h1 = new ConcreteHandler1(); 
  9. Handler h2 = new ConcreteHandler2(); 
  10. Handler h3 = new ConcreteHandler3(); 
  11. h1.SetSuccessor(h2); 
  12. h2.SetSuccessor(h3); 
  13. int[] requests = {2,5,14,22,18,3,27,20}; 
  14. foreach(int request in requests) 
  15. h1.HandRequest(request); 
  16. Console.ReadKey(); 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凌云县| 水城县| 庐江县| 英超| 镇原县| 扬州市| 容城县| 海盐县| 金堂县| 延边| 崇义县| 贵阳市| 高青县| 萍乡市| 利津县| 桓仁| 华阴市| 益阳市| 宁德市| 平阳县| 榆中县| 保靖县| 堆龙德庆县| 竹山县| 炉霍县| 岑巩县| 嘉峪关市| 科技| 阜宁县| 繁峙县| 龙口市| 龙井市| 江都市| 玛纳斯县| 濉溪县| 南城县| 安新县| 丰县| 泰安市| 德江县| 隆子县|