[DesignPattern生涯實例回顧02]Composite mode,Template Method

[DesignPattern生涯實例回顧02]Composite mode,Template Method

接續上文[DesignPattern生涯實例回顧01]Strategy mode,Interpreter mode,Chain of Responsibility mode,Facade mode,Bridge mode,Adapter mode,Factory mode,Builder mode,繼續介紹在工作生涯中曾經用過的design pattern

8. Composite mode
wiki:The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies
其實就是做管理資訊系統(不論是web based or non-web based)的系統功能樹狀圖,一般都是透過db資料表的設計出母功能跟子功能的關係,並且在程式碼透過遞迴的方式將功能表的樹狀圖呈現在畫面上,任何做管理資訊系統的應該都懂,就不多說囉

9.Template Method
wiki:In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses.[1] It lets one redefine certain steps of an algorithm without changing the algorithm's structure.
之前在做爬蟲專案的時候,基本上的爬資料流程都是先取得該網頁的html然後再來剖析html裡面的關鍵資訊,所以常常是在父類別裡面定義好兩個method,一個是GetHtml(string url),另外一個是ParseHtml(),然後不同的網站再透過不同的子類別進行繼承的動作即可,子類別可以自行重新定義GetHtml(string url)以及ParseHtml()的細節,GetHtml(string url)可透過基本的httpRequest或是利用selenium套件以取得html,ParseHtml()可以用HtmlAgility套件或是直接用正規運算式去剖析html……等等。口語上來說就是父類別定義好相關流程,子類別再自行定義流程的細節




參考資料:
[DesignPattern生涯實例回顧01]Strategy mode,Interpreter mode,Chain of Responsibility mode,Facade mode,Bridge mode,Adapter mode,Factory mode,Builder mode
https://dotblogs.com.tw/kevinya/2018/10/15/093609
Learning Design Pattern in 30 real-case practices 系列 -KarateJB - 2018 iT 邦幫忙鐵人賽(這邊要特別感謝此文章作者KarateJB,提供了相當易懂的範例)
https://ithelp.ithome.com.tw/users/20103220/ironman/1343