簡體   English   中英

這是裝飾器模式的正確實現嗎?

[英]Is this a correct implementation of the Decorator pattern?

主.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();
    }
}

響應.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();
    };
}

查看.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);
    }
}

布局.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 類型的 object 比當前執行更多操作時,使用裝飾器模式。 一個例子是:Web 適合您的物理屏幕的頁面(邏輯屏幕)不需要滾動條。 但是,如果頁面(邏輯屏幕)不適合物理屏幕,則必須用滾動條裝飾它。 用 GOF 的話來說:Decorator 的目的是動態地將額外的職責附加到 object 上。

在看起來像這樣的代碼中:

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

您可以根據需要鏈接盡可能多的鏈接。 裝飾器2(裝飾器1(簡單))...

據我簡短地查看:

  • 將您的響應更改為界面
  • 您的 View.java 確實與裝飾器模式不匹配,因此請刪除 Response 的擴展,例如

此致

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM