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

多用,多看

要求

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

思路

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

好像开始慢慢上手了

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

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

友情链接更多精彩内容