zoukankan      html  css  js  c++  java
  • Groovy 设计模式 -- 装饰器模式

    http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern

    装饰器模式, 起到美化原始对象的作用。

    一个被装饰的对象, 可以被用在原始对象出现的任何地方。

    装饰器对象,并不修改任何原始对象的源码。

    The Decorator Pattern provides a mechanism to embellish the behaviour of an object without changing its essential interface. A decorated object should be able to be substituted wherever the original (non-decorated) object was expected. Decoration typically does not involve modifying the source code of the original object and decorators should be able to be combined in flexible ways to produce objects with several embellishments.

    例子

    class Logger {
        def log(String message) {
            println message
        }
    }




    class TimeStampingLogger extends Logger {
        private Logger logger
        TimeStampingLogger(logger) {
            this.logger = logger
        }
        def log(String message) {
            def now = Calendar.instance
            logger.log("$now.time: $message")
        }
    }
    
    class UpperLogger extends Logger {
        private Logger logger
        UpperLogger(logger) {
            this.logger = logger
        }
        def log(String message) {
            logger.log(message.toUpperCase())
        }
    }


    def logger = new UpperLogger(new TimeStampingLogger(new Logger()))
    logger.log("G'day Mate")
    // => Tue May 22 07:13:50 EST 2007: G'DAY MATE
     
  • 相关阅读:
    2014-3-10 时间都去哪了,还没好好感受年轻就老了
    2014-3-4 思杨昨天已经顺利到老家 --------- 回忆思杨之2--“叫你不穿鞋鞋”
    2014-3-4 鬼脸笑笑的思杨
    路由
    视图
    请求与响应
    序列化组件
    APIView源码分析
    CBV源码分析
    DRF入门规范
  • 原文地址:https://www.cnblogs.com/lightsong/p/8724429.html
Copyright © 2011-2022 走看看