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();
    }
    }
  • 相关阅读:
    Input(Checkbox)全选删除
    DropDownList查询&Input(Checkbox)查询
    分页(二)
    webform---组合查询&流水号生成
    (转)C#生成中文汉字验证码源码(webform)
    文件上传
    [转]用C#如何实现大文件的断点上传
    DOM操作----open()
    经典背包问题的探讨
    贪心算法----正整数分解问题 和相同,乘积最大
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6524151.html
Copyright © 2011-2022 走看看