模板模式
1.利用多态将若干抽象方法组成一个步骤(称之为算法骨架结构),让子类具体实现。
2.代码:
C++
1 //template c++ 2 #include <iostream> 3 using namespace std; 4 5 class Dog 6 { 7 public: 8 virtual void introduce() = 0; 9 10 }; 11 12 class Alaska : public Dog{ 13 void introduce(){ 14 cout<<"Alaska is a huge dog,balabla..."<<endl; 15 } 16 }; 17 18 class Golden : public Dog{ 19 void introduce(){ 20 cout<<"Golden is a nice guy,balabla..."<<endl; 21 } 22 }; 23 24 int main(){ 25 Dog * p = NULL; 26 p = new Alaska(); 27 p->introduce(); 28 delete p; 29 30 p = new Golden(); 31 p->introduce(); 32 33 return 0; 34 }
Java:
1 //template java 2 import java.io.*; 3 4 abstract class CaffeineBeverageWithHook{ 5 void prepareRecipe(){ 6 boilWater(); 7 brew(); 8 pourInCup(); 9 if(customerWantsCondiments()){ //hook 10 addCondiments(); 11 } 12 } 13 abstract void brew(); 14 abstract void addCondiments(); 15 void boilWater(){ 16 System.out.println("Boiling water"); 17 } 18 void pourInCup(){ 19 System.out.println("Pouring into cup"); 20 } 21 boolean customerWantsCondiments(){ 22 return true; 23 } 24 } 25 26 class CoffeeWithHook extends CaffeineBeverageWithHook{ 27 public void brew(){ 28 System.out.println("Dipping Coffee through filter"); 29 } 30 public void addCondiments(){ 31 System.out.println("Adding Sugar and milk"); 32 } 33 public boolean customerWantsCondiments(){ 34 String answer = getUserInput(); 35 if(answer.toLowerCase().startsWith("y")){ 36 return true; 37 }else { 38 return false; 39 } 40 } 41 private String getUserInput(){ 42 String answer = null; 43 System.out.print("Would you like milk and sugar add (y/n)?"); 44 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 45 try{ 46 answer = in.readLine(); 47 }catch(IOException ioe){ 48 System.err.println("IO error"); 49 } 50 if(null == answer) 51 return "no"; 52 return answer; 53 } 54 } 55 56 public class template{ 57 public static void main(String[] args){ 58 CoffeeWithHook coffeeHook = new CoffeeWithHook(); 59 System.out.println("Making coffee..."); 60 coffeeHook.prepareRecipe(); 61 } 62 }
(2)
1 //template duckComparable java 2 import java.util.Arrays; 3 4 class Duck implements Comparable{ 5 String name; 6 int weight; 7 public Duck(String name,int weight){ 8 this.name = name; 9 this.weight = weight; 10 } 11 public String toString(){ 12 return name + " weights " + weight; 13 } 14 public int compareTo(Object object){ 15 Duck otherDuck = (Duck)object; 16 if(this.weight < otherDuck.weight){ 17 return -1; 18 }else if(this.weight == otherDuck.weight){ 19 return 0; 20 }else { 21 return 1; 22 } 23 } 24 } 25 26 public class duckComparable{ 27 public static void main(String[] args){ 28 Duck[] ducks = { 29 new Duck("kaoya",4), 30 new Duck("shaoya",6), 31 new Duck("dunya",3), 32 new Duck("zhaya",7) 33 }; 34 System.out.println("Before sort:"); 35 display(ducks); 36 37 Arrays.sort(ducks); 38 39 System.out.println("\nAfter sort:"); 40 display(ducks); 41 } 42 public static void display(Duck[] ducks){ 43 for(Duck d : ducks) 44 System.out.println(d); 45 } 46 }
3.杂记:
(1)好莱坞原则:高层组件对待低层组件,别调用我们,我们会调用你
(2)依赖倒置:依赖于抽象接口,不依赖于实现;A依赖virtual B;C1 extends B,C2 extends B;
倒置(inverse): 翻译比较坑,理解为换一种实现方式(思考方式)个人感觉更恰当。
(3)模板方法:结构不变,由子类实现算法中的某步骤,实例化;
策略模式:封装可互换的行为,然后使用委托来决定要采用哪一个行为;
通过对象组合让客户选择(算法族,族内可互换),有弹性,可在运行时改变
(4)钩子(hook)函数:if(a) {} void a(){return true;}
(5)steeping sea 浸泡
brew 冲泡
dripping coffee 滴流
prepare recipe 食谱
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通