加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:26:07 所属栏目:Java 来源:网络整理
导读:Main. java package com.example.decorator;public class Main { public static void main(String[] args) { Response response = new Response(); View head = new View("titleHello,world!/title"); View body = new View("h1Hello,world!/h1"); response.
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>");
    }
}

解决方法

当您希望类型(接口)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(简单))…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读