【问题标题】:Is this a correct implementation of the Decorator pattern?这是装饰器模式的正确实现吗?
【发布时间】:2011-09-08 03:12:10
【问题描述】:

Main.java

package com.example.decorator;

public class Main {
    public static void main(String[] args) {
        Response response = new Response();
        View head = new View("<title>Hello, world!</title>");
        View body = new View("<h1>Hello, world!</h1>");
        response.setContent(new HtmlLayout(head, body));
        response.render();
    }
}

Response.java

package com.example.decorator;

public class Response {
    private Response content;
    public Response () {}
    public <T extends Response> void setContent(T content) {
        this.content = content;
    }
    public void render() {
        this.content.render();
    };
}

View.java

package com.example.decorator;

public class View extends Response {
    private String content;
    public View(String content) {
        this.content = content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getContent() {
        return this.content;
    }
    public void render() {
        System.out.println(this.content);
    }
}

Layout.java

package com.example.decorator;

public class Layout extends Response {
    private Response view;
    public <T extends Response> Layout(T view) {
        this.view = view;
    }
    public void render() {
        this.view.render();
    }
}

HtmlLayout.java

package com.example.decorator;

public class HtmlLayout extends Response {
    private Response head;
    private Response body;
    public <T extends Response> HtmlLayout(T head, T body) {
        this.head = head;
        this.body = body;
    }
    public void render() {
        System.out.println("<!doctype html>");
        System.out.println("<html>");
        System.out.println("<head>");
        this.head.render();
        System.out.println("</head>");
        System.out.println("<body>");
        this.body.render();
        System.out.println("</body>");
        System.out.println("</html>");
    }
}

【问题讨论】:

    标签: java design-patterns decorator


    【解决方案1】:

    就我所看到的简短而言:

    • 将您的响应更改为界面
    • 您的 View.java 确实与装饰器模式不匹配,因此请删除 Response 的扩展,例如

    最好的问候

    【讨论】:

      【解决方案2】:

      当您希望类型(接口)A 的对象比当前做的更多时,使用装饰器模式。一个例子是:适合您的物理屏幕的网页(逻辑屏幕)不需要滚动条。但是,如果页面(逻辑屏幕)不适合物理屏幕,则必须用滚动条装饰它。 用 GOF 的话来说:Decorator 的目的是动态地为对象附加额外的职责。

      在如下代码中:

      interface LogicalScreen {
        void render(String physical );
      }
      

      一个实现:

      class SimpleScreen implements LogicalScreen {
        public void render(String physical) {
          // render itself
        }
      }
      

      装饰器的实现:

      class ScreenWithScrollbar implements LogicalScreen {
        private final LogicalScreen decoratd;
      
        public ScreenWithScrollbar(LogicalScreen decorated) {
          this.decoratd = decorated;
        }
      
        public void render(String physical) {
          // render scroll bar
          // ...
          // render the decorated
          decoratd.render(physical);
          // eventually do some more stuff
        }
      
        public doScroll() {}
      }
      

      如何接线:

      public class WhatIsDecorator {
        public static void main(String[] args) {
          LogicalScreen l1 = new SimpleScreen();
          LogicalScreen ds = new ScreenWithScrollbar(l1);
      
          ds.render("MyMonitor");
        }
      }
      

      您可以根据需要进行尽可能多的链接。 Decorator2(Decorator1(Simple)) ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-04
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2012-02-16
        • 2017-03-15
        • 2018-03-10
        • 2016-02-24
        相关资源
        最近更新 更多