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 }


  • 相关阅读:
    空间换时间之反范式设计之路/合理冗余/去除外键
    WebAPI接口设计:SwaggerUI文档 / 统一响应格式 / 统一异常处理 / 统一权限验证
    开放api接口签名验证
    EasyUI开发踩过的坑(EasyUI开发笔记)
    nuget挂了吗?
    C# 实体/集合差异比较,比较两个实体或集合值是否一样,将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
    从应用的角度讲创业公司该如何选择域名?
    疑似easyui本身bug:easyui时间控件问题,试了几个版本都不行
    工作三年对程序的理解特来求证
    控制器读取视图表单中的数据的几种方式
  • 原文地址:https://www.cnblogs.com/yakun/p/2285112.html
Copyright © 2011-2022 走看看