【设计模式】装饰模式之小菜扮靓

多用,多看

要求

写一个可以给人搭配不同的服饰的系统

思路

Iter1 初始草稿

#!/usr/bin/python
# coding:utf-8

class Person:
    def __init__(self, name):
        self.name = name

    def wearTShirts(self):
        print "大T恤",

    def wearBigTrouser(self):
        print "垮裤",

    def wearSneakers(self):
        print "破球鞋",

    def wearSuit(self):
        print "西装",

    def wearTie(self):
        print "领带",

    def wearLeatherShoes(self):
        print "皮鞋",

    def show(self):
        print "装扮的%s" %(self.name), "\n"


if __name__ == "__main__":
    xc = Person("小菜")
    print "第一种装扮"
    xc.wearTShirts()
    xc.wearBigTrouser()
    xc.wearSneakers()
    xc.show()

    print "第二种装扮"
    xc.wearSuit()
    xc.wearTie()
    xc.wearLeatherShoes()
    xc.show()
问题:
  • 如果需要增加一种装扮,如超人装扮,如何做?

开放-封闭原则:软件实体(类,模块,函数)应当可以扩展,但是不可修改。

Iter2 松耦合

#!/usr/bin/python
# coding:utf-8

class Person:
    def __init__(self, name):
        self.name = name

    def show(self):
        print "装扮的%s" % (self.name)


class Fienry:
    def show(self):
        pass


class TShirts(Fienry):
    def show(self):
        print "大T恤",


class BigTrouser(Fienry):
    def show(self):
        print "垮裤",


class Sneakers(Fienry):
    def show(self):
        print "破球鞋",


class Suit(Fienry):
    def show(self):
        print "西装",


class Tie(Fienry):
    def show(self):
        print "领带",


class LeatherShoes(Fienry):
    def show(self):
        print "皮鞋",


if __name__ == "__main__":
    xc = Person("小菜")
    print "第一种装扮"
    dtx = TShirts()
    kk = BigTrouser()
    pqx = Sneakers()

    dtx.show()
    kk.show()
    pqx.show()
    xc.show()

    print "第二种装扮"
    xz = Suit()
    ld = Tie()
    px = LeatherShoes()
    xz.show()
    ld.show()
    px.show()
    xc.show()

问题:
  • 怎么才能将所需的功能按照正确的顺序串联起来进行控制?

Iter3 装饰模式

装饰模式:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

#!/usr/python
#encoding:utf-8


class Component:
    def operation(self):
        return

class ConcreteComponent(Component):
    def operation(self):
        print "具体对象的操作"

class Decorator(Component):
    def __init__(self):
        self.component = None
    def setcomponent(self, component):
        self.component = component

    def operation(self):
        if self.component:
            self.component.operation()

class ConcreteDecoratorA(Decorator):
    def operation(self):
        self.component.operation()
        print "具体装饰对象A的操作"

class ConcreteDecoratorB(Decorator):
    def operation(self):
        self.component.operation()
        print "具体装饰对象B的操作"

if __name__ == "__main__":
    c = ConcreteComponent()
    d1 = ConcreteDecoratorA()
    d2 = ConcreteDecoratorB()

    d1.setcomponent(c)
    d2.setcomponent(d1)
    d2.operation()
    

Iter4 利用装饰模式来实现

#!/usr/bin/python
# coding:utf-8

class Person:
    def __init__(self, name):
        self.name = name

    def show(self):
        print "装扮好的%s" % (self.name)


class Fienry(Person):
    def __init__(self):
        self.component = None

    def setcomponent(self, component):
        self.component = component

    def show(self):
        pass


class TShirts(Fienry):
    def show(self):
        print "大T恤"
        self.component.show()


class BigTrouser(Fienry):
    def show(self):
        print "垮裤"
        self.component.show()


class Sneakers(Fienry):
    def show(self):
        print "破球鞋"
        self.component.show()


class Suit(Fienry):
    def show(self):
        print "西装"
        self.component.show()


class Tie(Fienry):
    def show(self):
        print "领带"
        self.component.show()


class LeatherShoes(Fienry):
    def show(self):
        print "皮鞋"
        self.component.show()


if __name__ == "__main__":
    xc = Person("小菜")
    print "第一种装扮"
    dtx = TShirts()
    kk = BigTrouser()
    pqx = Sneakers()

    dtx.setcomponent(xc)
    kk.setcomponent(dtx)
    pqx.setcomponent(kk)
    pqx.show()
    print ""

    print "第二种装扮"
    xz = Suit()
    ld = Tie()
    px = LeatherShoes()
    xz.setcomponent(xc)
    ld.setcomponent(xz)
    px.setcomponent(ld)
    px.show()

装饰模式是为已有的功能动态地添加更多功能的一种方式。

有效地把类的核心职责和装饰功能区分开,并且可以去除相关类中重复的装饰逻辑。

UML图

Decorator Pattern

Fienry Decorator

细碎python

  • abstract method
  • 装饰器 装饰模式

PS

好像开始慢慢上手了

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容

  • 本文首发于个人博客:Lam's Blog - 谈谈23种设计模式在Android源码及项目中的应用,文章由Mark...
    格子林ll阅读 4,650评论 1 105
  • 设计模式汇总 一、基础知识 1. 设计模式概述 定义:设计模式(Design Pattern)是一套被反复使用、多...
    MinoyJet阅读 3,948评论 1 15
  • 参考资料:菜鸟教程之设计模式 设计模式概述 设计模式(Design pattern)代表了最佳的实践,通常被有经验...
    Steven1997阅读 1,175评论 1 12
  • 1.《被禁的书》亥特女士:《爱丽斯漫游奇境记》中译本于1931年在湖南省被禁,理由是“书中鸟兽昆虫皆作人言,而且与...
    小鸡萝卜阅读 909评论 0 0
  • 今 天早晨刚打开办公室的门,突然发现,那盆水仙花开的真迷人,我轻轻凑过去散发着淡淡的清香,我不由得感叹,真...
    良简之阅读 366评论 1 2