zoukankan      html  css  js  c++  java
  • 设计模式之三 装饰模式

    
    

    
    
     1 /**
    2 * 文件名:Decorator.java
    3 *
    4 * 版本信息:
    5 * 日期:2011-12-12
    6 * Copyright 陈亚坤 Corporation 2011
    7 * 版权所有
    8 *
    9 */
    10 package byME;
    11
    12 /**
    13 *
    14 * 项目名称:Design Pattern 类名称:decorator 类描述: 创建人:chenyakun 创建时间:2011-12-12
    15 * 下午05:45:13 修改人:chenyakun 修改时间:2011-12-12 下午05:45:13 修改备注:
    16 *
    17 * @version
    18 *
    19 */
    20 // 组件类
    21 abstract class Component {
    22
    23 public abstract void Operation();
    24 }
    25
    26 // ConcreteComponent类
    27 class ConcreteComponent extends Component {
    28
    29 public void Operation() {
    30 System.out.println("具体对象的操作");
    31 }
    32
    33 }
    34
    35 public class Decorator extends Component {
    36
    37 protected Component component;
    38
    39 public void setComponent(Component component) { // 设置Component
    40 this.component = component;
    41 }
    42
    43 @Override
    44 public void Operation() { // 重写Operation(),实际上是Componet的Operation()
    45 if (component != null) {
    46 component.Operation();
    47 }
    48 }
    49
    50 }
    51
    52 class ConcreteDecoratorA extends Decorator {
    53
    54 private String propety; // 相当于给Component的一种装饰。
    55
    56 @Override
    57 public void Operation() {
    58 super.Operation();
    59 propety = "Some values fromA";
    60 System.out.println(propety);
    61 }
    62
    63 }
    64
    65 class ConcreteDecoratorB extends Decorator {
    66
    67 private void AddedBehavior() { // 相当于对Component进行装饰
    68 System.out.println("from B");
    69 }
    70
    71 @Override
    72 public void Operation() {
    73 super.Operation();
    74 AddedBehavior();
    75 }
    76
    77 }
    78
    79 class DecoratorClient {
    80
    81 public static void main(String[] args) {
    82 ConcreteComponent nakedComponent = new ConcreteComponent(); // 赤裸的组件,还没被装饰(类似一个未穿衣服的人)
    83 ConcreteDecoratorA firstDecorated = new ConcreteDecoratorA(); // 装饰组件A
    84 ConcreteDecoratorB secondDecorated = new ConcreteDecoratorB(); // 装饰组件B
    85
    86 firstDecorated.setComponent(nakedComponent);
    87 secondDecorated.setComponent(firstDecorated);
    88 secondDecorated.Operation();
    89 /*
    90 * 简要介绍下Operation的执行过程:
    91 * 执行secondDecorated.Operation(),因为ConcreteDecorateB的Operation()中有super.Operation(),实际上是执行Decorator中的Operaton(),
    92 *
    93 * 因为component在上一步装饰成了DecoratorA,即执行DecoratorA.component.Operation(),又因这个Operation中仍有super.operation();结果继续类似执行。
    94 *
    95 * 概括来说就是先装饰在客户端上最先”装饰“的component,在这里为nakedComponent, 这个过程类似于队列,先进先出。
    96 */
    97 }
    98 }


  • 相关阅读:
    Promises-小程序购物车结算
    微信小程序支付
    微信企业付款到零钱
    Ubuntu16 远程连接MySQL
    MySQL数据库中文变问号
    Ubuntu开启ApacheRewrite功能
    大数据2018:云存储已在客观层面扮演数据湖角色
    地平线发布兼具本地端抓拍与识别功能的嵌入式AI摄像机
    肥皂遇上黑科技!异味统统都走开!
    LG新专利或用于移动VR,可通过外部旋钮调节显示屏与透镜
  • 原文地址:https://www.cnblogs.com/yakun/p/2285112.html
Copyright © 2011-2022 走看看