十二、Decorator 装饰器模式

设计:

代码清单:

Display

public abstract class Display {
    public abstract int getColumns();
    public abstract int getRows();
    public abstract String getRowText(int row);
    public final void show(){
        for(int i=0;i<getRows();i++){
            System.out.println(getRowText(i));
        }
    }
}

StringDisolay

public class StringDisplay extends Display{
    private String string;
    public StringDisplay(String string){
        this.string = string;
    }
    @Override
    public int getColumns() {
        return string.getBytes().length;
    }

    @Override
    public int getRows() {
        return 1;
    }

    @Override
    public String getRowText(int row) {
        if(row == 0){
            return string;
        }else{
            return null;
        }
    }
}

Border

public abstract class Border extends Display{
    protected  Display display;
    protected  Border(Display display){
        this.display = display;
    }
}

FullBorder

public class FullBorder extends Border{
    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){
        StringBuffer buf = new StringBuffer();
        for(int i=0;i<count;i++){
            buf.append(ch);
        }
        return buf.toString();
    }
}

SideBorder

public class SideBorder extends Border{
    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;
    }
}

原文地址:https://www.cnblogs.com/baizhuang/p/10476249.html

时间: 2024-08-02 14:13:55

十二、Decorator 装饰器模式的相关文章

Decorator(装饰器模式)

装饰器模式允许我们根据运行时不同的情景动态地为某个对象调用前后添加不同的行为动作. <?php class HtmlTemplate { // any parent class methods } class Template1 extends HtmlTemplate { protected $_html; public function __construct() { $this->_html = "<p>__text__</p>"; } pub

十二、装饰器进阶

执行函数的时候* 打散 定义函数的时候* 聚合 from functools import wraps def wrapper(f): # f = func1 @wraps(f) def inner(*args,**kwargs): #聚合 #args (1,2,3) '''执行函数之前的相关操作''' ret = f(*args,**kwargs) # 打散 1,2,3 '''执行函数之后的相关操作''' return ret return inner # 函数的执行时,*打散. # 函数的定

java设计模式之七装饰器模式(Decorator)

顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个装饰类,可以为Source类动态的添加一些功能,代码如下: [java] view plaincopy public interface Sourceable { public void method(); } [java] view plaincopy public class Source im

适配器模式 &amp; 装饰器模式

一.适配器模式:简单来讲,就是为了方便使用,完成从 一个接口 到 另一个接口 的 转换,这个负责转换的就是 适配器例如:Reader ——> InputStreamReader 通过类内部组合 StreamDecoder 接收 InputStream 作为参数 完成适配器模式 InputStreamReader inputStreamReader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStrea

PHP设计模式 五 (代理模式 装饰器模式)

代理模式 在客户端和实体之间建立一个代理对象,客户端对实体的操作全部委派给代理对象,隐藏实体具体实现细节. Proxy还可以与业务代码分离,部署到另外的服务器,业务代码中通过RPC来委派任务. 代理Proxy.php: <?php namespace Components\Proxy; class Proxy implements IUserProxy { function get($id) { $db = \Components\Register::get('slave'); $db->qu

Java设计模式(四) 装饰器模式 代理器模式

(七)装饰器模式 Decorator 装饰器模式是为了动态的给一个对象增加一些新功能.装饰对象与被装饰的对象需要实现同一个接口,装饰对象持有被装饰对象的实例. interface DecoratorSourceable{ public void method(); } //被装饰类 class DecoratorSource implements DecoratorSourceable{ public void method(){ System.out.println("Source"

20181122_C#中AOP初探_装饰器模式的AOP_Remoting实现AOP_Castle实现AOP

一.   什么是AOP: a)         AOP是面向切面编程; 就像oop一样, 它也是一种编程思想; i.    Oop思想→一切皆对象, 对象交互组成功能, 功能叠加组成模块, 模块叠加组成系统; 如果把一个个的类比喻成一个个砖头, 那么系统就是一个房子; 房子是由一块块砖头构成, 所以面向对象非常适合做大型系统; 但是面向对象的在应对系统扩展的时候, 就显得力不从心; 如果砖块需要发生变化, 则就会牵扯到很多地方; 因为面向对象是静态的, 内部就是强耦合的关系; 虽然设计模式可以解

Java设计模式——装饰器模式(Decorator)

孙悟空有七十二般变化,他的每一种变化都给他带来一种附加本领.而不管孙悟空怎么变化在二郎神眼里,他永远是那只猢狲. 装饰器模式以对客户透明的方式动态的给一个对象附加上更多的责任. 在孙悟空的例子里,老孙变成的鱼儿相当于老孙的子类. 装饰模式的类图如下: 装饰模式的角色介绍: 抽象构件角色(ComponentDec):给出一个抽象接口,以规范准备接收附加责任的对象 具体构件(Concrete ComponentDec):定义一个将要接收附加责任的类 装饰角色(Decortor):持有一个构件对象的实

IOS设计模式之二(门面模式,装饰器模式)

本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq)翻译,如果你发现有什么错误,请与我联系谢谢. 门面(Facade)模式(译者注:facade有些书籍译为门面,有些书籍译为外观,此处译为门面) 门面模式针对复杂的子系统提供了单一的接口,不需要暴漏一些列的类和API给用户,你仅仅暴漏一个简单统一的API. 下面的图解释了这个概念: 这个API的使用者