diff --git a/patterns/structural/3-tier.py b/patterns/structural/3-tier.py index 4b5b6157..7b98b982 100644 --- a/patterns/structural/3-tier.py +++ b/patterns/structural/3-tier.py @@ -3,7 +3,14 @@ Separates presentation, application processing, and data management functions. """ +# 3层 +# 数据层、应用层、表现层 +# 数据层管数据如何存储 +# 应用层管api +# 表现层关如何展示数据 + +# 数据层 class Data: """ Data Store Class """ @@ -17,7 +24,7 @@ def __get__(self, obj, klas): print("(Fetching from Data Store)") return {'products': self.products} - +# 业务层 class BusinessLogic: """ Business logic holding data store instances """ @@ -29,7 +36,7 @@ def product_list(self): def product_information(self, product): return self.data['products'].get(product, None) - +# 表现层 class Ui: """ UI interaction class """ @@ -38,12 +45,14 @@ def __init__(self): def get_product_list(self): print('PRODUCT LIST:') + for product in self.business_logic.product_list(): print(product) print('') def get_product_information(self, product): product_info = self.business_logic.product_information(product) + if product_info: print('PRODUCT INFORMATION:') print( diff --git a/patterns/structural/adapter.py b/patterns/structural/adapter.py index 99314a2a..36bb0410 100644 --- a/patterns/structural/adapter.py +++ b/patterns/structural/adapter.py @@ -28,6 +28,8 @@ Allows the interface of an existing class to be used as another interface. """ +# 适配模式:已有很多类,类似但不统一,加个适配类 +# 实现:设置函数映射表 class Dog: def __init__(self): diff --git a/patterns/structural/bridge.py b/patterns/structural/bridge.py index 64b4b422..02d73185 100644 --- a/patterns/structural/bridge.py +++ b/patterns/structural/bridge.py @@ -6,6 +6,8 @@ Decouples an abstraction from its implementation. """ +# 已有多个统一格式的类,使用方选一个使用,可以搭个桥连接起来 +# 实现:接口类 # ConcreteImplementor 1/2 class DrawingAPI1: diff --git a/patterns/structural/composite.py b/patterns/structural/composite.py index 2f2e6da9..28873668 100644 --- a/patterns/structural/composite.py +++ b/patterns/structural/composite.py @@ -26,27 +26,34 @@ Describes a group of objects that is treated as a single instance. """ +# 定义了标准,各有各的实现,但又有组合的需求时,画图里面使用比较多 +# 实现:抽象类+实现类+组合类 +# 抽象组件 class Graphic: + # 定义需要实现的函数 def render(self): raise NotImplementedError("You should implement this.") +# 合成组件 class CompositeGraphic(Graphic): def __init__(self): self.graphics = [] + // 合成组件渲染 def render(self): for graphic in self.graphics: graphic.render() + # 增加与删除组件 def add(self, graphic): self.graphics.append(graphic) - def remove(self, graphic): self.graphics.remove(graphic) +# 椭圆 class Ellipse(Graphic): def __init__(self, name): self.name = name diff --git a/patterns/structural/decorator.py b/patterns/structural/decorator.py index b94c0527..738f3747 100644 --- a/patterns/structural/decorator.py +++ b/patterns/structural/decorator.py @@ -24,7 +24,10 @@ Adds behaviour to object without affecting its class. """ +# 装饰器,拿到原文后,需要装饰一下,如加粗、斜体等 +# 实现:拿到原文后,再处理一把 +# 文本 class TextTag: """Represents a base text tag""" @@ -34,7 +37,7 @@ def __init__(self, text): def render(self): return self._text - +# 粗体 class BoldWrapper(TextTag): """Wraps a tag in """ @@ -44,7 +47,7 @@ def __init__(self, wrapped): def render(self): return "{}".format(self._wrapped.render()) - +# 斜体 class ItalicWrapper(TextTag): """Wraps a tag in """ diff --git a/patterns/structural/facade.py b/patterns/structural/facade.py index 6c04c472..ec412626 100644 --- a/patterns/structural/facade.py +++ b/patterns/structural/facade.py @@ -28,6 +28,9 @@ Provides a simpler unified interface to a complex system. """ +# 门面模式 +# 有很多组件,需要组合运行,内部流程可能很复杂,封装成简单的接口 +# 实现:多个组件+管理类 # Complex computer parts class CPU: diff --git a/patterns/structural/front_controller.py b/patterns/structural/front_controller.py index 346392e4..3b59799d 100644 --- a/patterns/structural/front_controller.py +++ b/patterns/structural/front_controller.py @@ -5,17 +5,21 @@ Provides a centralized entry point that controls and manages request handling. """ +# 前端控制器 +# 将特定请求分配到请求分配器上,分配器根据请求做不同操作 + +# 手机视图 class MobileView: def show_index_page(self): print('Displaying mobile index page') - +# 表视图 class TabletView: def show_index_page(self): print('Displaying tablet index page') - +# 分配器 class Dispatcher: def __init__(self): self.mobile_view = MobileView() @@ -29,7 +33,7 @@ def dispatch(self, request): else: print('cant dispatch the request') - +# 请求控制器 class RequestController: """ front controller """ diff --git a/patterns/structural/mvc.py b/patterns/structural/mvc.py index ff22ea59..5a5fceb8 100644 --- a/patterns/structural/mvc.py +++ b/patterns/structural/mvc.py @@ -3,7 +3,12 @@ Separates data in GUIs from the ways it is presented, and accepted. """ +# model,存储数据 +# view,基于模型api,写视图 +# 控制器,不同模型不同视图 + +# 模型 class Model: def __iter__(self): raise NotImplementedError @@ -18,6 +23,7 @@ def item_type(self): raise NotImplementedError +# 产品模型 class ProductModel(Model): class Price(float): """A polymorphic way to pass a float with a particular @@ -44,7 +50,7 @@ def get(self, product): except KeyError as e: raise KeyError(str(e) + " not in the model's item list.") - +# 抽象视图 class View: def show_item_list(self, item_type, item_list): raise NotImplementedError @@ -58,6 +64,7 @@ def item_not_found(self, item_type, item_name): raise NotImplementedError +# 终端视图 class ConsoleView(View): def show_item_list(self, item_type, item_list): print(item_type.upper() + ' LIST:') @@ -80,7 +87,7 @@ def show_item_information(self, item_type, item_name, item_info): def item_not_found(self, item_type, item_name): print('That {} "{}" does not exist in the records'.format(item_type, item_name)) - +# 控制器,有模型和视图 class Controller: def __init__(self, model, view): self.model = model diff --git a/patterns/structural/proxy.py b/patterns/structural/proxy.py index 560643ee..bd8dd309 100644 --- a/patterns/structural/proxy.py +++ b/patterns/structural/proxy.py @@ -15,7 +15,10 @@ without changing its interface. """ +# 通过代理模式去使用最终类,中途可以做很多事 + +# 订阅接口 class Subject: """ As mentioned in the document, interfaces of both RealSubject and Proxy should @@ -29,7 +32,7 @@ class Subject: def do_the_job(self, user): raise NotImplementedError() - +# 订阅实现 class RealSubject(Subject): """ This is the main job doer. External services like payment gateways can be a @@ -39,7 +42,7 @@ class RealSubject(Subject): def do_the_job(self, user): print(f'I am doing the job for {user}') - +# 代理 class Proxy(Subject): def __init__(self): self._real_subject = RealSubject()