装饰者模式

作者: 周文洪 | 来源:发表于2013-08-19 16:35 被阅读220次

【风趣的解释】

Decorator Mode

家里又让我相亲,说是这个美眉是我的菜,人长得漂亮,心地又好。嘿嘿,那就准备下呗!换个新发型、新衣服、新鞋子,好好的装饰一下自己。

【正式的解释】

建造者模式

装饰者提供比继承更有弹性、更灵活的替代方案。可以动态给一个对象添加新的功能,也可以动态的取消。

【Python版】

#-*- coding:utf-8 -*-

#装饰者
class Decorator(object):
    def __init__(self, *args):
        if args:
            self.obj = args[0]
        else:
            self.obj = None

    def getPrice(self):
        if self.obj:
            return self.obj.getPrice() + ', ' + self.name + ': ' + str(self.price)
        else:
            return self.name + ': ' + str(self.price)

    def getCost(self):
        if self.obj:
            return float(self.obj.getCost()) + float(self.price)
        else:
            return float(self.price)
#新发型
class NewHair(Decorator):
    def __init__(self, *args):
        self.name = "new hair style"
        self.price = "20"
        super(self.__class__, self).__init__(*args)
#新衣服
class NewCloth(Decorator):
    def __init__(self, *args):
        self.name = "new cloth"
        self.price = "200"
        super(self.__class__, self).__init__(*args)
#新鞋子
class NewShoes(Decorator):
    def __init__(self, *args):
        self.name = "new shoes"
        self.price = "130"
        super(self.__class__, self).__init__(*args)

if __name__ == "__main__":
    #我的所有装备
    equipment = NewHair()
    equipment = NewCloth(equipment)
    equipment = NewShoes(equipment)

    print equipment.getPrice()
    print "total cost: ", equipment.getCost()

"""print out

new hair style: 20, new cloth: 200, new shoes: 130
total cost:  350.0
"""

【JS版】

//装饰者
function Decorator(){

}
Decorator.prototype.getPrice = function(){
    if(typeof this.obj !== 'undefined'){
        return this.obj.getPrice() + ', ' + this.name + ': ' + this.price;
    }else{
        return this.name + ': ' + this.price;
    }
};
Decorator.prototype.getCost = function(){
    if(typeof this.obj !== 'undefined'){
        return parseFloat(this.obj.getCost()) + parseFloat(this.price);
    }else{
        return parseFloat(this.price);
    }
};

//新发型
function NewHair(obj){
    this.name = "new hair style";
    this.price = "20";
    this.obj = obj;
}
NewHair.prototype = Decorator.prototype;

//新衣服
function NewCloth(obj){
    this.name = "new cloth";
    this.price = "200";
    this.obj = obj;
}
NewCloth.prototype = Decorator.prototype;

//新鞋子
function NewShoes(obj){
    this.name = "new shoes";
    this.price = "130";
    this.obj = obj;
}
NewShoes.prototype = Decorator.prototype;

//我的所有装备
var equipment = new NewHair();
equipment = new NewCloth(equipment);
equipment = new NewShoes(equipment);

console.log(equipment.getPrice());
console.log('total cost:' + equipment.getCost());

/*console out

new hair style: 20, new cloth: 200, new shoes: 130
total:350
*/

相关文章

  • 如何利用装饰者模式在不改变原有对象的基础上扩展功能

    目录 什么是装饰者模式 普通示例 装饰者模式示例 类图关系 装饰者模式使用场景 装饰者模式优点 装饰者模式缺点 什...

  • 装饰者模式

    装饰者模式 装饰者模式和适配器模式对比 装饰者模式 是一种特别的适配器模式 装饰者与被装饰者都要实现同一个接口,主...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式-装饰者模式

    装饰者模式概念: 装饰者模式又名包装(Wrapper)模式。装饰者模式以对客户端透明的方式扩展对象的功能,是继承关...

  • java - 装饰者模式

    装饰者模式 装饰者模式:动态将责任添加到对象上。如果需要扩展功能,装饰者提供了比继承更有弹性的解决方案。装饰者模式...

  • 设计模式之装饰者模式(Decorator Pattern)

    What: 装饰者模式又名包装(Wrapper)模式。装饰者模式动态地将责任附加到对象身上。若要扩展功能,装饰者提...

  • 装饰者(Decorator)模式

    装饰者(Decorator)模式装饰模式又名包装(Wrapper)模式。装饰模式是继承关系的一个替代方案。装饰模式...

  • 2、装饰者模式

    装饰者模式 一、基本概念 二、结构 三、案例1、装饰者模式案例2、JavaIO中使用装饰者模式 四、总结 一、基本...

  • PHP的设计模式-装饰者模式

    装饰者模式 装饰者模式 装饰者模式类似蛋糕,有草莓味、奶酪等种类,但是它们的核心都是蛋糕。 不断地将对象添加装饰的...

  • 设计模式 | 装饰者模式及典型应用

    前言 本文的主要内容: 介绍装饰者模式 示例 源码分析装饰者模式的典型应用Java I/O 中的装饰者模式spri...

网友评论

    本文标题:装饰者模式

    本文链接:https://www.haomeiwen.com/subject/wnmftttx.html