zoukankan      html  css  js  c++  java
  • linkin大话设计模式--模板方法模式

    linkin大话设计模式--模板方法模式


    准备一个抽象类,将部分逻辑以具体方法的形式实现,然后申明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现。抽象模板角色:
    1.定义了一个或多个抽象操作,以便让子类实现。
    2.定义并实现了一个模板方法。

    具体模板角色:
    1.实现父类所定义的一个或多个抽象方法。
    2.每一个抽象模板角色都可以有任意多个具体模板角色与之对应,而每一个具体模板角色都可以给出这些抽象方法的不同实现。

    代码如下:
    //模板抽象类
    abstract class Template {
        protected String pcType;
        
        public Template(String pcType){
            this.pcType=pcType;
        }
        
        //定义3个抽象方法,推迟到子类实现
        abstract protected void makeCPU(String pcType);
        abstract protected void makeMainBorad(String pcType);
        abstract protected void makeHD(String pcType);
        
        //定义一个基本方法
        public final void makeOver(String pcType){
            System.out.println(pcType+"造好了...");
        }
        
        //模板方法,造电脑的方法
        public final void makePC(){
            makeCPU(pcType);
            makeMainBorad(pcType);
            makeHD(pcType);
            makeOver(pcType);
        }
    }
    
    //2个不同的子类  实现了上面的模板中定义的抽象方法
    class NotePC extends Template{
    
        public NotePC(String pcType) {
            super(pcType);
        }
    
        @Override
        protected void makeCPU(String pcType) {
            System.out.println(pcType+"的CPU造好了...");
        }
    
        @Override
        protected void makeMainBorad(String pcType) {
            System.out.println(pcType+"的主板造好了...");
        }
    
        @Override
        protected void makeHD(String pcType) {
            System.out.println(pcType+"的硬盘造好了...");
        }
        
    }
    
    class PC extends Template{
    
        public PC(String pcType) {
            super(pcType);
        }
    
        @Override
        protected void makeCPU(String pcType) {
            System.out.println(pcType+"的CPU造好了呢...");
        }
    
        @Override
        protected void makeMainBorad(String pcType) {
            System.out.println(pcType+"的主板造好了呢...");
        }
    
        @Override
        protected void makeHD(String pcType) {
            System.out.println(pcType+"的硬盘造好了呢...");
        }
        
    }
    
    public class TemplateTest {
        
        public static void main(String[] args) {
            Template template = new NotePC("笔记本");
            template.makePC();
            Template template1 = new PC("台式机");
            template1.makePC();
        }
        
    
    }
    


    在这里有必要诉述下抽象类和接口的区别:
    1,接口类似于整个系统的总纲,它制定了系统各模块应该遵守的标准,因此一个系统的接口不应该经常改变。接口一旦变化,系统中的大部分类都需要重写
    2,抽象类不一样,抽象类作为系统中多个子类的共同父类,它所体现的是一种模板式设计。它可以当做系统实现过程中的中间产品,这个中间产品已经实现了系统的部分功能,但是这个产品任然不是最终产品,必须要有更进一步的完善,具体的推迟到子类实现。

    Java语言里面使用过的模板方法模式:HttpServlet技术

    HttpServlet类提供了一个service()方法。这个方法调用了一个或者几个do方法,完成对客户端调用的处理。这些do方法则要由具体的HttpServler类提供。那么这里的service()方法就是模版方法。其中service方法代码如下:
     protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
      {
        String method = req.getMethod();
    
        if (method.equals("GET")) {
          long lastModified = getLastModified(req);
          if (lastModified == -1L)
          {
            doGet(req, resp); } else {
            long ifModifiedSince;
            try {
              ifModifiedSince = req.getDateHeader("If-Modified-Since");
            }
            catch (IllegalArgumentException iae) {
              ifModifiedSince = -1L;
            }
            if (ifModifiedSince < lastModified / 1000L * 1000L)
            {
              maybeSetLastModified(resp, lastModified);
              doGet(req, resp);
            } else {
              resp.setStatus(304);
            }
          }
        }
        else if (method.equals("HEAD")) {
          long lastModified = getLastModified(req);
          maybeSetLastModified(resp, lastModified);
          doHead(req, resp);
        }
        else if (method.equals("POST")) {
          doPost(req, resp);
        }
        else if (method.equals("PUT")) {
          doPut(req, resp);
        }
        else if (method.equals("DELETE")) {
          doDelete(req, resp);
        }
        else if (method.equals("OPTIONS")) {
          doOptions(req, resp);
        }
        else if (method.equals("TRACE")) {
          doTrace(req, resp);
        }
        else
        {
          String errMsg = lStrings.getString("http.method_not_implemented");
          Object[] errArgs = new Object[1];
          errArgs[0] = method;
          errMsg = MessageFormat.format(errMsg, errArgs);
    
          resp.sendError(501, errMsg);
        }
      }


  • 相关阅读:
    2018百度之星初赛B轮 rect
    八数码问题(九宫格重排) 利用康托展开 判重
    2018百度之星初赛A轮 度度熊拼三角
    2018百度之星初赛A轮 度度熊学队列
    MongoDB(课时22 唯一索引)
    MongoDB(课时21 索引)
    MongoDB(课时20 游标)
    MongoDB(课时19 数据删除)
    MongoDB(课时18 修改器)
    MongoDB(课时17 更新函数)
  • 原文地址:https://www.cnblogs.com/LinkinPark/p/5233184.html
Copyright © 2011-2022 走看看