您的位置:首页 > 其它

head first design patterns(1):策略模式,观察者模式,工厂模式,抽象工厂模式

2017-01-13 10:39 399 查看

[0]策略模式:

将算法族封装起来,然后利用对象组合的思想来解决问题。比如一只鸭子可能有飞翔,叫两个行为,但是不同种类的鸭子这两个行为是完全不一样的,甚至很多鸭子不具备这两种行为.这时候利用继承是不合适的,更好的做法是将这些行为抽象成一个算法族,然后鸭子可以在运行时动态添加,组合这些行为。

[1]装饰模式:这个思路非常类似python里面的装饰器,只要我们的代码不依赖于特定的类型,那么就可以使用装饰模式来进行一层又一层的封装,使得我们可以在不改变其他代码的前提下直接进行增加一些功能。

[2]观察者模式:

标准的框架是有主题和观察者,主题负责维护信息和通知注册的观察者。经常用于GUI框架。这种设计模式的好处是:对于主题类和观察者类松耦合,观察者可以随时注册或者取消注册,而主题也不需要知道观察者的具体信息。注意到不同的观察者所需要的信息种类可能完全不一样,所以我们可以采取’push’ 或者 ‘pull’两个方式来推送信息。一个是将所有的信息都推送给观察者,一个是让观察者自己来取。

下面这一份代码使用python模拟了一个最简单的天气情况的情景:

class Subject:
def __init__(self):
self.observers = []
self.changed = False
def register_observer(self,obj):
self.observers.append(obj)
def remove_observer(self,obj):
if (len(self.observers)!=0):
try:
self.observers.remove(obj)
except:
print('remove non-exisiting observer ')
def notify_observers(self):
if self.changed:
for observer in self.observers:
observer.update(self,args=None)
self.changed = False
def set_changed(self):
self.changed = True
class Weather_data(Subject):
def __init__(self):
super(Weather_data,self).__init__()
self.temparature = None
self.condition = None
def set_measurement(self,temparature,condition):
self.temparature = temparature
self.condition = condition
self.changed = True
self.notify_observers()

class Observer:
def update(observable):
pass
class Station(Observer):
def __init__(self,observable):

observable.register_observer(self)
self.subject = observable
self.temparature = observable.temparature
self.condition = observable.condition
def update(self,observable,args):
self.temparature = observable.temparature
self.condition = observable.condition
def display(self):
print('temparature is :',self.temparature)
print('condition is :',self.condition)
# import time
# w = Weather_data()
# s = Station(w)
# s2 = Station(w)
# for i in range(100):
# if i==10:w.remove_observer(s2)
# w.set_measurement(i,'good' if i%2 else 'bad')
# s.display()
# s2.display()
# time.sleep(0.5)


[3]工厂模式:这一个模式则代表了将创建对象这件事情抽象出来,使得用到相应对象的代码不需要知道具体的对象是什么。它的基本用法是利用抽象的父类处理客户代码,但是把创建对象的代码交给子类决定。这样就把具体对象的信息推迟到了后面,从而建立了一层抽象。

[5]抽象工厂模式:这种模式多用于创建一系列的产品族,我们选择一个抽象工厂作为父类,它具有一些抽象的方法用来创建一些产品族,然后我们需要建立具体的工厂,每一个具体的工厂都必须实现自己的具体产品族。注意到抽象工厂适合纵向扩展而不适合横向扩展。

下面的代码模拟一个pizza订单系统,有两种不同风格的pizza(纽约口味(NY)和芝加哥(Chacigo)口味),每一种口味又有加蔬菜和芝士两种类型。同时不同的风格需要不同的原材料。在这个例子中,采取了工厂模式用来生产pizza,采取抽象工厂模式用来生产原材料族。

class pizzasotre:
def __init__(self):
self.pizza = None
def order_pizza(self,category):
pizza = self.create_pizza(category)
pizza.prepare()
pizza.bake()
pizza.cut()
return pizza
class ny_pizzasotre(pizzasotre):
def __init__(self):
self.ingredientfactory = nyfactory()
def create_pizza(self,category):
pizza = None
if category == 'cheese':
pizza =  nycheese_pizza(self.ingredientfactory)
elif category == 'veggle':
pizza =  nyveggle_pizza(self.ingredientfactory)
else:
print('unknown pizza')
return pizza
class chacigo_pizzastore(pizzasotre):
def __init__(self):
self.ingredientfactory = chacigofactory()
def create_pizza(self,category):
if category == 'cheese':
self.pizza =  chacigocheese_pizza(self.ingredientfactory)
elif category == 'veggle':
self.pizza =  chacigoioveggle_pizza(self.ingredientfactory)
else:
print('unknown pizza')
class pizza:
def __init__(self,ingredientfactory):
self.daugh = None
self.sauce = None
self.ingredientfactory = ingredientfactory
def bake(self):
print('bake pizza')
def cut(self):
print('cut pizza')
class chacigocheese_pizza(pizza):
def prepare(self):
self.daugh = self.ingredientfactory.get_daugh()
self.sauce = self.ingredientfactory.get_sauce()
class chacigoioveggle_pizza(pizza):
def prepare(self):
self.daugh = self.ingredientfactory.get_daugh()
class nycheese_pizza(pizza):
def prepare(self):
self.sauce = self.ingredientfactory.get_sauce()
class nyveggle_pizza(pizza):
def prepare(self):
pass
class ingredientfactory:
pass
class nyfactory(ingredientfactory):
def get_daugh(self):
return 'ny_daugh'
def get_sauce(self):
return 'ny_sauce'
class chacigofactory(ingredientfactory):
def get_daugh(self):
return 'chacigo_daugh'
def get_sauce(self):
return 'chacigo_sauce'
store = ny_pizzasotre()
p = store.order_pizza('cheese')
print(p.sauce)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐