面试题复习-常用设计模式-模板模式
模板:
package com.ajax.pattern.template; import javax.servlet.http.HttpServletRequest; public abstract class ServletTemplate { public void handle(HttpServletRequest request) { doGet(); doPost(); } protected abstract void doPost(); protected abstract void doGet(); }
实现1:
package com.ajax.pattern.template; public class ServletImpl1 extends ServletTemplate { @Override protected void doGet() { System.out.println("get1"); } @Override protected void doPost() { System.out.println("post1"); } }
实现2:
package com.ajax.pattern.template; public class ServletImpl2 extends ServletTemplate { @Override protected void doGet() { System.out.println("get2"); } @Override protected void doPost() { System.out.println("post2"); } }
运行:
package com.ajax.pattern.template; public class Run { public static void main(String[] args) { new ServletImpl1().handle(null); new ServletImpl2().handle(null); } }