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

多用,多看

要求

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

思路

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

好像开始慢慢上手了

你可能感兴趣的:(【设计模式】装饰模式之小菜扮靓)