Skip to content

Commit e471a46

Browse files
committed
代码学习
1 parent 697deba commit e471a46

File tree

9 files changed

+52
-12
lines changed

9 files changed

+52
-12
lines changed

patterns/structural/3-tier.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
Separates presentation, application processing, and data management functions.
44
"""
55

6+
# 3层
7+
# 数据层、应用层、表现层
8+
# 数据层管数据如何存储
9+
# 应用层管api
10+
# 表现层关如何展示数据
611

12+
13+
# 数据层
714
class Data:
815
""" Data Store Class """
916

@@ -17,7 +24,7 @@ def __get__(self, obj, klas):
1724
print("(Fetching from Data Store)")
1825
return {'products': self.products}
1926

20-
27+
# 业务层
2128
class BusinessLogic:
2229
""" Business logic holding data store instances """
2330

@@ -29,7 +36,7 @@ def product_list(self):
2936
def product_information(self, product):
3037
return self.data['products'].get(product, None)
3138

32-
39+
# 表现层
3340
class Ui:
3441
""" UI interaction class """
3542

@@ -38,12 +45,14 @@ def __init__(self):
3845

3946
def get_product_list(self):
4047
print('PRODUCT LIST:')
48+
4149
for product in self.business_logic.product_list():
4250
print(product)
4351
print('')
4452

4553
def get_product_information(self, product):
4654
product_info = self.business_logic.product_information(product)
55+
4756
if product_info:
4857
print('PRODUCT INFORMATION:')
4958
print(

patterns/structural/adapter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
Allows the interface of an existing class to be used as another interface.
2929
"""
3030

31+
# 适配模式:已有很多类,类似但不统一,加个适配类
32+
# 实现:设置函数映射表
3133

3234
class Dog:
3335
def __init__(self):

patterns/structural/bridge.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
Decouples an abstraction from its implementation.
77
"""
88

9+
# 已有多个统一格式的类,使用方选一个使用,可以搭个桥连接起来
10+
# 实现:接口类
911

1012
# ConcreteImplementor 1/2
1113
class DrawingAPI1:

patterns/structural/composite.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,34 @@
2626
Describes a group of objects that is treated as a single instance.
2727
"""
2828

29+
# 定义了标准,各有各的实现,但又有组合的需求时,画图里面使用比较多
30+
# 实现:抽象类+实现类+组合类
2931

32+
# 抽象组件
3033
class Graphic:
34+
# 定义需要实现的函数
3135
def render(self):
3236
raise NotImplementedError("You should implement this.")
3337

3438

39+
# 合成组件
3540
class CompositeGraphic(Graphic):
3641
def __init__(self):
3742
self.graphics = []
3843

44+
// 合成组件渲染
3945
def render(self):
4046
for graphic in self.graphics:
4147
graphic.render()
4248

49+
# 增加与删除组件
4350
def add(self, graphic):
4451
self.graphics.append(graphic)
45-
4652
def remove(self, graphic):
4753
self.graphics.remove(graphic)
4854

4955

56+
# 椭圆
5057
class Ellipse(Graphic):
5158
def __init__(self, name):
5259
self.name = name

patterns/structural/decorator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
Adds behaviour to object without affecting its class.
2525
"""
2626

27+
# 装饰器,拿到原文后,需要装饰一下,如加粗、斜体等
28+
# 实现:拿到原文后,再处理一把
2729

30+
# 文本
2831
class TextTag:
2932
"""Represents a base text tag"""
3033

@@ -34,7 +37,7 @@ def __init__(self, text):
3437
def render(self):
3538
return self._text
3639

37-
40+
# 粗体
3841
class BoldWrapper(TextTag):
3942
"""Wraps a tag in <b>"""
4043

@@ -44,7 +47,7 @@ def __init__(self, wrapped):
4447
def render(self):
4548
return "<b>{}</b>".format(self._wrapped.render())
4649

47-
50+
# 斜体
4851
class ItalicWrapper(TextTag):
4952
"""Wraps a tag in <i>"""
5053

patterns/structural/facade.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
Provides a simpler unified interface to a complex system.
2929
"""
3030

31+
# 门面模式
32+
# 有很多组件,需要组合运行,内部流程可能很复杂,封装成简单的接口
33+
# 实现:多个组件+管理类
3134

3235
# Complex computer parts
3336
class CPU:

patterns/structural/front_controller.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
Provides a centralized entry point that controls and manages request handling.
66
"""
77

8+
# 前端控制器
9+
# 将特定请求分配到请求分配器上,分配器根据请求做不同操作
810

11+
12+
# 手机视图
913
class MobileView:
1014
def show_index_page(self):
1115
print('Displaying mobile index page')
1216

13-
17+
# 表视图
1418
class TabletView:
1519
def show_index_page(self):
1620
print('Displaying tablet index page')
1721

18-
22+
# 分配器
1923
class Dispatcher:
2024
def __init__(self):
2125
self.mobile_view = MobileView()
@@ -29,7 +33,7 @@ def dispatch(self, request):
2933
else:
3034
print('cant dispatch the request')
3135

32-
36+
# 请求控制器
3337
class RequestController:
3438
""" front controller """
3539

patterns/structural/mvc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
Separates data in GUIs from the ways it is presented, and accepted.
44
"""
55

6+
# model,存储数据
7+
# view,基于模型api,写视图
8+
# 控制器,不同模型不同视图
69

10+
11+
# 模型
712
class Model:
813
def __iter__(self):
914
raise NotImplementedError
@@ -18,6 +23,7 @@ def item_type(self):
1823
raise NotImplementedError
1924

2025

26+
# 产品模型
2127
class ProductModel(Model):
2228
class Price(float):
2329
"""A polymorphic way to pass a float with a particular
@@ -44,7 +50,7 @@ def get(self, product):
4450
except KeyError as e:
4551
raise KeyError(str(e) + " not in the model's item list.")
4652

47-
53+
# 抽象视图
4854
class View:
4955
def show_item_list(self, item_type, item_list):
5056
raise NotImplementedError
@@ -58,6 +64,7 @@ def item_not_found(self, item_type, item_name):
5864
raise NotImplementedError
5965

6066

67+
# 终端视图
6168
class ConsoleView(View):
6269
def show_item_list(self, item_type, item_list):
6370
print(item_type.upper() + ' LIST:')
@@ -80,7 +87,7 @@ def show_item_information(self, item_type, item_name, item_info):
8087
def item_not_found(self, item_type, item_name):
8188
print('That {} "{}" does not exist in the records'.format(item_type, item_name))
8289

83-
90+
# 控制器,有模型和视图
8491
class Controller:
8592
def __init__(self, model, view):
8693
self.model = model

patterns/structural/proxy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
without changing its interface.
1616
"""
1717

18+
# 通过代理模式去使用最终类,中途可以做很多事
1819

20+
21+
# 订阅接口
1922
class Subject:
2023
"""
2124
As mentioned in the document, interfaces of both RealSubject and Proxy should
@@ -29,7 +32,7 @@ class Subject:
2932
def do_the_job(self, user):
3033
raise NotImplementedError()
3134

32-
35+
# 订阅实现
3336
class RealSubject(Subject):
3437
"""
3538
This is the main job doer. External services like payment gateways can be a
@@ -39,7 +42,7 @@ class RealSubject(Subject):
3942
def do_the_job(self, user):
4043
print(f'I am doing the job for {user}')
4144

42-
45+
# 代理
4346
class Proxy(Subject):
4447
def __init__(self):
4548
self._real_subject = RealSubject()

0 commit comments

Comments
 (0)