zoukankan      html  css  js  c++  java
  • Decorator Pattern

    character:the original object's function is OK,but can not be used into new situation.so it needs additional opearions.

                    the new object with something new and includes the original object is called decorator.

    as to the realization,it can be carried out in three steps:

    1.the original object with some basic functions

    1.1 abstract object

    public abstract class Component {
    public abstract void operation();
    }

    1.2 concrete object

    public class ConcreteCompment extends Component{

    public void operation(){
    System.out.println("It's in operation.");
    }
    }

    2.the decorator object with additional functions

    2.1 abstract decorator(define the operation)

    public class Decorator {
    protected Component component;
    public Decorator(Component component){
    this.component=component;
    }
    public void operation(){
    component.operation();
    }
    }

    2.2 concrete decorator(add additional operations)

    public class ConcreteDecorator extends Decorator{
    public ConcreteDecorator(Component component){
    super(component);
    }
    private void operationA(){
    System.out.println("It's in operationA.");
    }
    private void operationZ(){
    System.out.println("It's in operationZ.");
    }

    public void operation(){
    operationA();
    super.operation();
    operationZ();
    }
    }

    3.the usage of decorator

    public class Client {
    public static void main(String[] args) {
    Component custom=new ConcreteCompment();
    Decorator decorator=new ConcreteDecorator(custom);
    decorator.operation();
    }
    }
  • 相关阅读:
    SVN使用教程总结
    学习duilib库过程中的笔记
    duilib库使用(一)-- 编译生成依赖库
    在Windows服务进程中启动需管理员权限(带盾牌图标)的应用程序
    如何在Windows服务程序中读写HKEY_CURRENT_USER注册表
    vs2015 编译boost库
    NSIS 打包工具使用
    C 读文件
    常用的字符转化的方法
    C# 中对于json的解析小结
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6524151.html
Copyright © 2011-2022 走看看