这是Decorator模式的正确实现吗?

时间:2011-06-03 21:15:37

标签: java design-patterns decorator

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

2 个答案:

答案 0 :(得分: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(简单))......

答案 1 :(得分:-1)

据我所知,简短来说:

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

祝你好运