2017年3月1日 星期三

sealed class 禁止繼承

實做上
通常建立一些管理物件
為了統一控管
不想被多個物件操作
我們可以利用 sealed class 的關鍵字
禁止其他類別繼承 sealed class
藉此操作物件內的資料
以下是 sealed class 的用法

  1. sealed public class S3Mgr
  2. {
  3. ....
  4. }


只要繼承 S3Mgr 就會編譯錯誤







partial class 將 class 分成多個 cs 檔

假設某些專案規模很大
總有些 cs 檔案
常常有人修改
程式碼也很多
每每簽入都要處理衝突
或許重構可以根本解決這樣的問題
但是當時間有限沒有足夠的時間做重構
 partial class 或許可以解燃眉之急
利用 partial class 可以將 class 分成多個 cs 檔案

  1. // A.cs 檔
  2. partial class Program
  3. {
  4. public A _a = new A();
  5. ....
  6. }

  1. // B.cs 檔
  2. partial class Program
  3. {
  4. public B _a = new B();
  5. ....
  6. }

編譯器會將 A.cs 與 B.cs 視作是同一個檔案同一個 class
也就是說 A.cs 可以直接取到 B 類別
 B.cs 可以直接取到 A 類別
就像是在同一個 class 一樣

這樣的手法
可以幫你分類功能
依據你需要修改的內容
移到獨立的 cs 檔案內
避免多人修改同一個 cs 檔
也能方便管理類別
避免類別內過多的程式碼
有助於程式碼的閱讀



C# Composite Pattern 組合模式


組合模式最關鍵的地方是簡單對像和復合對像實現相同的接口

  1. /// <summary>
  2. /// 圖形抽像類
  3. /// </summary>
  4. public abstract class Graphics
  5. {
  6. public string Name { get; set; }
  7. public Graphics(string name)
  8. {
  9. this.Name = name;
  10. }
  11. public abstract void Draw();
  12. public abstract void Add(Graphics g);
  13. public abstract void Remove(Graphics g);
  14. }

簡單圖形

  1. /// <summary>
  2. /// 簡單圖形類——線
  3. /// </summary>
  4. public class Line : Graphics
  5. {
  6. public Line(string name)
  7. : base(name)
  8. { }
  9. // 重寫父類抽像方法
  10. public override void Draw()
  11. {
  12. Console.WriteLine("畫線:" + Name);
  13. }
  14. public override void Add(Graphics g)
  15. {
  16. throw new Exception("不能向簡單圖形Line添加其他圖形");
  17. }
  18. public override void Remove(Graphics g)
  19. {
  20. throw new Exception("不能向簡單圖形Line移除其他圖形");
  21. }
  22. }
  23. /// <summary>
  24. /// 簡單圖形類——圓
  25. /// </summary>
  26. public class Circle : Graphics
  27. {
  28. public Circle(string name)
  29. : base(name)
  30. { }
  31. // 重寫父類抽像方法
  32. public override void Draw()
  33. {
  34. Console.WriteLine("畫圓:" + Name);
  35. }
  36. public override void Add(Graphics g)
  37. {
  38. throw new Exception("不能向簡單圖形Circle添加其他圖形");
  39. }
  40. public override void Remove(Graphics g)
  41. {
  42. throw new Exception("不能向簡單圖形Circle移除其他圖形");
  43. }
  44. }

複雜圖形

  1. /// <summary>
  2. /// 複雜圖形,由一些簡單圖形組成,假設該複雜圖形由兩條線組成
  3. /// </summary>
  4. public class ComplexGraphics : Graphics
  5. {
  6. private ListGraphics complexGraphicsList = new ListGraphics>();
  7. public ComplexGraphics(string name)
  8. : base(name)
  9. { }
  10. /// <summary>
  11. /// 複雜圖形的畫法
  12. /// </summary>
  13. public override void Draw()
  14. {
  15. foreach (Graphics g in complexGraphicsList)
  16. {
  17. g.Draw();
  18. }
  19. }
  20. public override void Add(Graphics g)
  21. {
  22. complexGraphicsList.Add(g);
  23. }
  24. public override void Remove(Graphics g)
  25. {
  26. complexGraphicsList.Remove(g);
  27. }
  28. }

實際執行

  1. class MyClass
  2. {
  3. static void Main(string[] args)
  4. {
  5. ComplexGraphics complexGraphics = new ComplexGraphics("複雜圖形 - 兩條線段組成的複雜圖形");
  6. complexGraphics.Add(new Line("線段A"));
  7. complexGraphics.Add(new Line("線段C"));
  8. // 顯示複雜圖形的畫法
  9. Console.WriteLine("複雜圖形的繪製:");
  10. Console.WriteLine("---------------------");
  11. complexGraphics.Draw();
  12. Console.WriteLine("複雜圖形繪製完成");
  13. Console.WriteLine("---------------------");
  14. }
  15. }


優點:
組合模式介面都一致,可以存在容器統一處理

缺點:複雜度會增加


Visual Studio 2017/2019 推薦的擴充功能與更新

參考文章: 覺得 Google 的 Blogger 不太順手?透過 HTML 的 iframe 移花接木 HackMD