美文网首页
【设计模式】装饰模式之小菜扮靓

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

作者: flowerAO | 来源:发表于2018-05-05 15:40 被阅读0次

多用,多看

要求

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

思路

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

好像开始慢慢上手了

相关文章

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

    多用,多看 要求 写一个可以给人搭配不同的服饰的系统 思路 Iter1 初始草稿 问题: 如果需要增加一种装扮,...

  • JavaScript 设计模式核⼼原理与应⽤实践 之 结构型设计

    JavaScript 设计模式核⼼原理与应⽤实践 之 结构型设计模式 装饰器模式,又名装饰者模式。它的定义是“在不...

  • 聊聊Context

    在上篇文章《设计模式之装饰模式》中我们谈到了装饰模式,在 Android 中关于 Context 的设计就用到了装...

  • 9、结构型模式-装饰器设计模式

    1、如虎添翼的设计模式-装饰器设计模式 简介:讲解-装饰器设计模式介绍和应用场景 装饰器设计模式(Decorato...

  • Java设计模式之装饰器模式

    Java设计模式之装饰器模式 本文仅是个人观点,如有错误请指正 简介 装饰器模式(Decorator Patter...

  • Golang 设计模式之-装饰模式

    Golang 设计模式之-装饰模式 最近在温习设计模式,虽然面向对象不是go的特长,但用go实现的设计模式,比ja...

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • 修饰模式

    维基百科-修饰模式 php 设计模式 之 装饰模式 hero博客——装饰模式 动态的给一个对象添加一些额外的职责,...

  • Java设计模式之 —— 装饰者(Decorator) — 点

    Java设计模式之 —— 装饰者(Decorator) — 点炒饭 下面会用做炒饭的例子来描述一下装饰者设计模式,...

  • 设计模式之-装饰模式

    好了,昨天我们说完了单利模式,趁着现在想写博客就多写几篇吧. 下面开始说这个装饰模式 装饰模式其实还有做成一个类,...

网友评论

      本文标题:【设计模式】装饰模式之小菜扮靓

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