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);
    }
  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值