From ddfa55ab56a6ca887fd704459ee4ce3e780c08a3 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 7 Aug 2015 11:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E8=BF=9B=E5=BA=A6=EF=BC=9A3t?= =?UTF-8?q?ier,=20adapter,=20builder=20NEXT:=20chain,=20command,=20composi?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3-tier.py | 9 ++++++--- adapter.py | 26 ++++++++++++++++++++++++-- builder.py | 16 ++++++++++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/3-tier.py b/3-tier.py index 625870a3..3985e283 100644 --- a/3-tier.py +++ b/3-tier.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +#DAL 数据访问层 +#提供数据的添加,删除,修改,更新,查找 class Data(object): """ Data Store Class """ @@ -15,7 +16,8 @@ def __get__(self, obj, klas): print("(Fetching from Data Store)") return {'products': self.products} - +#BLL 业务逻辑层 +#针对具体问题的操作,对数据层的操作,针对数据进行逻辑处理 class BusinessLogic(object): """ Business logic holding data store instances """ @@ -27,7 +29,8 @@ def product_list(self): def product_information(self, product): return self.data['products'].get(product, None) - +#UI 表现层 +#展现给用户界面,在用户看到系统的时候所见即所得 class Ui(object): """ UI interaction class """ diff --git a/adapter.py b/adapter.py index 374d01fb..1939f851 100644 --- a/adapter.py +++ b/adapter.py @@ -3,10 +3,18 @@ """http://ginstrom.com/scribbles/2008/11/06/generic-adapter-class-in-python/""" - +#适配器模式 adapter pattern +#把一个类的结构变成客户端所期待的另外一种借口 +#使原来因为接口不兼容的两个类现在可以协同工作 +#分为两种 +#1.类的适配器:适配器继承自已经实现的类 +#2.对象适配器:适配器容纳一个它包裹的类的实例 +#本代码实现的是第二种 class Dog(object): def __init__(self): self.name = "Dog" + #add a common attribute for all objects + self.age = 1 def bark(self): return "woof!" @@ -15,6 +23,7 @@ def bark(self): class Cat(object): def __init__(self): self.name = "Cat" + self.age = 2 def meow(self): return "meow!" @@ -23,6 +32,7 @@ def meow(self): class Human(object): def __init__(self): self.name = "Human" + self.age = 3 def speak(self): return "'hello'" @@ -31,6 +41,7 @@ def speak(self): class Car(object): def __init__(self): self.name = "Car" + self.age = 4 def make_noise(self, octane_level): return "vroom{0}".format("!" * octane_level) @@ -66,9 +77,16 @@ def __init__(self, obj, **adapted_methods): """We set the adapted methods in the object's dict""" self.obj = obj self.__dict__.update(adapted_methods) + #执行过后 + #self.obj = obj + #self.make_noise = obj.some_method def __getattr__(self, attr): """All non-adapted calls are passed to the object""" + #getattr(obj, attr) + #把attr当作一个变量去处理 + #return self.obj.attr + #调用obj自身的attr的值 return getattr(self.obj, attr) @@ -76,6 +94,10 @@ def main(): objects = [] dog = Dog() objects.append(Adapter(dog, make_noise=dog.bark)) + print objects[-1].__dict__ + #obj.obj = dog + #obj.make_noice() = dog.bark() + #是之前实例化之后的dog的bark() cat = Cat() objects.append(Adapter(cat, make_noise=cat.meow)) human = Human() @@ -84,7 +106,7 @@ def main(): objects.append(Adapter(car, make_noise=lambda: car.make_noise(3))) for obj in objects: - print("A {0} goes {1}".format(obj.name, obj.make_noise())) + print("A {0} goes {1}, its age is {2}".format(obj.name, obj.make_noise(), obj.age)) if __name__ == "__main__": diff --git a/builder.py b/builder.py index 3b04412d..668bd81b 100644 --- a/builder.py +++ b/builder.py @@ -6,15 +6,24 @@ https://gist.github.com/420905#file_builder_python.py """ - +#Builder模式 +#把一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示 +#适用性: +#当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式时 +#当构造过程必须允许被构造的对象有不同的表示时 # Director +# Director是用来构造一个使用Build接口的对象 + class Director(object): def __init__(self): self.builder = None + # Director创建和装配对象过程 def construct_building(self): + #调用Abstract Build来build self.builder.new_building() + #调用Concrete Builder来完成build的流程 self.builder.build_floor() self.builder.build_size() @@ -34,7 +43,7 @@ def new_building(self): # Concrete Builder class BuilderHouse(Builder): - + #Concrete Build继承Abstract Builder来完成某一部分的build def build_floor(self): self.building.floor = 'One' @@ -52,6 +61,7 @@ def build_size(self): # Product +# Build的产品 class Building(object): def __init__(self): @@ -65,7 +75,9 @@ def __repr__(self): # Client if __name__ == "__main__": director = Director() + #给Director一个builder director.builder = BuilderHouse() + #进行build director.construct_building() building = director.get_building() print(building)