python与设计模式之二 装饰器模式(以dota为例)
来源:互联网 发布:网络钟点工网站 编辑:程序博客网 时间:2024/05/10 20:13
装饰器模式: 先感叹下windows live writer,竟然把所有的缩进都忽略了,这样python代码就郁闷了,害的我都重新粘一次。
一. 大话上的例子Python 实现:
#!/usr/bin/env python3.0
import sys
class Component(object):
def Operation(self):
pass
class ConcreteComponent(Component):
def Operation(self):
print("具体对象的操作")
class Decorator(Component):
def SetComponent(self, component):
self.component = component
def Operation(self):
self.component.Operation()
class ConcreteDecoratorA(Decorator):
def Operation(self):
super(ConcreteDecoratorA, self).Operation() #注意super的用法
self.addState = "New State"
print("具体装饰对象A的操作")
class ConcreteDecoratorB(Decorator):
def Operation(self):
super(ConcreteDecoratorB, self).Operation()
self.AddedBehavior()
print("具体装饰对象B的操作")
def AddedBehavior(self):
print("B's AddedBehavior")
if __name__ == "__main__":
c = ConcreteComponent()
d1 = ConcreteDecoratorA()
d2 = ConcreteDecoratorB()
d1.SetComponent(c)
d2.SetComponent(d1)
d2.Operation()
二 dota的例子
#!/usr/bin/env python3.0
class Person(object):
def __init__(self, name):
self.name = name
self.InitState()
def InitState(self):
self.armor = 0
self.attack = 0
self.ultimate = 0.0
self.skill = {}
def Show(self):
print("装备的%s"%(self.name))
# print("状态为 甲:%s 攻:%s 大招威力:%s"%(self.armor, self.attack, self.ultimate) )
class Luna(Person):
def __init__(self, name):
self.name = name
self.armor = 5
self.attack = 50
self.ultimate = 1.0
self.skill = {'月光':'召唤几道月光对对手进行打击'}
class Finery(Person):
def __init__(self):
self.InitState()
def Decorate(self, compoent):
self.compoent = compoent
self.name = compoent.name
self.armor = compoent.armor
self.attack = compoent.attack
self.ultimate = compoent.ultimate
self.skill = compoent.skill
def Show(self):
self.compoent.Show()
# self.ShowState()
def ShowState(self):
print("装备后状态为 甲:%s 攻:%s 大招威力:%s"%(self.armor, self.attack, self.ultimate) )
print("装备后技能:/n%s "%(self.skill))
class TShirts(Finery):
def Decorate(self, compoent):
super(TShirts, self).Decorate(compoent)
self.armor = self.armor + 20
def Show(self):
print("强袭 ",end='')
super(TShirts, self).Show()
class BigTrouser(Finery):
def Decorate(self, compoent):
super(BigTrouser, self).Decorate(compoent)
self.attack = self.attack + 40
def Show(self):
print("大炮 ",end='')
super(BigTrouser, self).Show()
class Sneakers(Finery):
def Decorate(self, compoent):
super(Sneakers, self).Decorate(compoent)
self.attack = self.attack + 30
def Show(self):
print("蝴蝶 ",end='')
super(Sneakers, self).Show()
class LeatherShoes(Finery):
def Decorate(self, compoent):
super(LeatherShoes, self).Decorate(compoent)
self.skill["变羊"] = "将对手变成一只羊,不能使用技能,甲减回初始值"
def Show(self):
print("羊刀 ",end='')
# self.skill = self.compoent.skill
super(LeatherShoes, self).Show()
class Tie(Finery):
def Decorate(self, compoent):
super(Tie, self).Decorate(compoent)
self.ultimate = self.ultimate + 0.2
def Show(self):
print("蓝杖 ",end='')
super(Tie, self).Show()
class Suit(Finery):
def Decorate(self, compoent):
super(Suit, self).Decorate(compoent)
self.skill["刷新"] = '大招CD恢复为0'
def Show(self):
print("刷新 ",end='')
# self.skill = self.compoent.skill
super(Suit, self).Show()
if __name__ == "__main__":
xc = Luna("月骑")
print("第一种装备:")
pqx = Sneakers()
kk = BigTrouser()
dtx = TShirts()
pqx.Decorate(xc)
kk.Decorate(pqx)
dtx.Decorate(kk)
dtx.Show()
dtx.ShowState()
print("第二种装备:")
px = LeatherShoes()
# px.Show()
px.ShowState()
ld = Tie()
# ld.Show()
# ld.ShowState()
xz = Suit()
px.Decorate(xc)
px.ShowState()
ld.Decorate(px)
xz.Decorate(ld)
xz.Show()
xz.ShowState()
运行结果:
第一种装备:
强袭 大炮 蝴蝶 装备的月骑
装备后状态为 甲:25 攻:120 大招威力:1.0
装备后技能:
{'月光': '召唤几道月光对对手进行打击'}
第二种装备:
装备后状态为 甲:0 攻:0 大招威力:0.0
装备后技能:
{}
装备后状态为 甲:5 攻:50 大招威力:1.0
装备后技能:
{'变羊': '将对手变成一只羊,不能使用技能,甲减回初始值', '月光': '召唤几道月光对对手进行打击'}
刷新 蓝杖 羊刀 装备的月骑
装备后状态为 甲:5 攻:50 大招威力:1.2
装备后技能:
{'变羊': '将对手变成一只羊,不能使用技能,甲减回初始值', '月光': '召唤几道月光对对手进行打击', '刷新': '大招CD恢复为0'}
在这里的搞笑的意义大于实际的意义,在实际游戏中应该不能这样设计,因为实际上相当于每穿上件装备,对外的对象就变成了最新的装饰器对象,比如两个英雄互殴,一个穿上件装备后,攻击力变化的是装备对象,英雄本身对象没有变,这样应该是不合适。
- python与设计模式之二 装饰器模式(以dota为例)
- python设计模式之装饰器模式
- python设计模式之装饰器模式
- 设计模式(二)之装饰器模式
- 【设计模式】之装饰器模式(二)-PHP
- 设计模式(二):装饰器模式
- 【Python学习日记】【设计模式】装饰器(装饰模式)
- Java设计模式之二装饰模式
- 设计模式之二:装饰者模式
- 设计模式之二装饰者模式
- 8、python设计模式之装饰模式
- android设计模式之装饰模式(二)
- 设计模式之python版(三)--装饰模式
- IOS设计模式之二(门面模式,装饰器模式)
- iOS设计模式之二(门面模式,装饰器模式)
- iOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- 标签使用问题
- Shell中脚本变量和函数变量的作用域
- alt提示换行
- iframe标签问题
- js数组不同浏览器下问题
- python与设计模式之二 装饰器模式(以dota为例)
- Flash 与分布学构想:心形分布
- js 取出json数组中的值
- PHP中post与get的区别
- 静态库的创建及使用
- 《浪潮之巅》读后感(转)
- 其实不甘心...
- POJ_1694_An Old Stone Game
- C++中文件操作EOF的问题