概述
The Template pattern defines a structure for sub classes in which steps of an algorithm and their order are defined. This ensures that the sub classes follow the exact same steps, providing better overview and consistency. It also allows to define default implementations for steps that can be overridden by subclasses.
模板方法模式就是給定一個(gè)算法的步驟赵刑,但是具體算法實(shí)現(xiàn)交給子類完成虐秋。還是比較好理解的。
本文類圖
代碼示例
BaseSteps的templateMethod就是模板方法尘执,他定義了具體的算法步驟。
ConcreA是BaseSteps中算法的具體實(shí)現(xiàn)。
ConcreB是BaseSteps中算法的具體實(shí)現(xiàn)。
測試結(jié)果
參考資料