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();
    }
    }
  • 相关阅读:
    SQL SERVER备份数据库存储过程.
    JMail组件使用中文文档
    Oracle,SQL Server,Access万能数据库通用类!
    快速幂的理解及使用
    关于地图坐标问题转换
    ref和依赖注入
    Unity3D 中的程序后台运行
    Unity中创建双面材质
    Unity3d 移动平台中文显示问题
    Unity3D 4.0中使用传统动画
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6524151.html
Copyright © 2011-2022 走看看