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();
    }
    }
  • 相关阅读:
    leetcode 1. 两数之和
    leetcode 671. 二叉树中第二小的节点
    leetcode 100. 相同的树
    leetcode 110. 平衡二叉树
    leetcode 144. 二叉树的前序遍历
    1066. Root of AVL Tree (25)
    leetcode 100 相同的树
    leeCode 515 在每个树行中找最大值
    LeetCode 31.下一个排列
    面向对象UML中类关系
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6524151.html
Copyright © 2011-2022 走看看