裝飾器模式-Decorator(Java實現)

來源:互聯網
上載者:User

標籤:技術   har   under   column   tostring   ide   指定   技術分享   nbsp   

裝飾器模式-Decorator(Java實現)

裝飾器模式允許向一個現有的對象添加新的功能, 同時又不改變其結構。

其中 "現有對象"在本文中是StringDisplay類.

添加新的功能的對象在本文中是: SideBorder類 和 FullBorder類

Display介面
public interface Display {    int getColumns();    int getRows();    String getRowText(int row);    default void show() {        for (int i = 0; i < getRows(); i++) {            System.out.println(getRowText(i));        }    }}
StringDisplay類

基礎功能類, 用於列印字串.

public class StringDisplay implements Display {    private String string;    public StringDisplay(String s) {        string = s;    }    @Override    public int getColumns() {        return string.length();    }    @Override    public int getRows() {        return 1;    }    @Override    public String getRowText(int row) {        if (row == 0) {            return string;        } else {            return null;        }    }}

使用方式如下:

Display b1 = new StringDisplay("Hello, world.");b1.show();

 

裝飾器類AbstractBorder抽象類別

這是具體裝飾器類的抽象定義, 為了裝飾後也能統一對外的介面, 這裡也繼承了Display介面

public abstract class AbstractBorder implements Display {    Display display;    public AbstractBorder(Display display) {        this.display = display;    }}
SideBorder類

本類是一個裝飾器

public class SideBorder extends AbstractBorder {    private char borderchar;    public SideBorder(Display display, char ch) {        super(display);        this.borderchar = ch;    }    @Override    public int getColumns() {        return 1 + display.getColumns() + 1;    }    @Override    public int getRows() {        return display.getRows();    }    @Override    public String getRowText(int row) {        return borderchar + display.getRowText(row) + borderchar;    }}

使用方式如下:

把一個SideBorder裝飾器添加到StringDisplay上, 於是乎"Hello, world." 就變成了 "#Hello, world.#"

        Display b1 = new StringDisplay("Hello, world.");        Display b2 = new SideBorder(b1, ‘#‘);        b2.show();

FullBorder類

本類是一個裝飾器

public class FullBorder extends AbstractBorder {    public FullBorder(Display display) {        super(display);    }    @Override    public int getColumns() {        return 1 + display.getColumns() + 1;    }    @Override    public int getRows() {        return 1 + display.getRows() + 1;    }    @Override    public String getRowText(int row) {         // 指定的那一行的字串        if (row == 0) {                                                 // 上邊框            return "+" + makeLine(‘-‘, display.getColumns()) + "+";        } else if (row == display.getRows() + 1) {                      // 下邊框            return "+" + makeLine(‘-‘, display.getColumns()) + "+";        } else {                                                        // 其他邊框            return "|" + display.getRowText(row - 1) + "|";        }    }    private String makeLine(char ch, int count) {         // 產生一個重複count次字元ch的字串        StringBuffer buf = new StringBuffer();        for (int i = 0; i < count; i++) {            buf.append(ch);        }        return buf.toString();    }}

使用方式如下:

把一個FullBorder裝飾器添加到StringDisplay上, 於是乎"Hello, world." 就變成了->

+------ --+

|Hello, world.|

+---------+

        Display b1 = new StringDisplay("Hello, world.");        Display b3 = new FullBorder(b1);        b3.show();

Main

運行測試

public class Main {    public static void main(String[] args) {        Display b4 =                new SideBorder(                        new FullBorder(                                new FullBorder(                                        new SideBorder(                                                new FullBorder(                                                        new StringDisplay("1234567")                                                ), ‘*‘                                        )                                )                        ), ‘/‘                );        b4.show();    }}

 

裝飾器模式-Decorator(Java實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.