Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PyCon2013China_ZhuHai_laiyonghao
Search
Zoom.Quiet
December 08, 2013
Technology
2
850
PyCon2013China_ZhuHai_laiyonghao
PyCon2013China 珠海场 | GDG Livin ZhuHai Life;-)
http://zhgdg.gitcafe.com/2013-12/et-pycon/
Zoom.Quiet
December 08, 2013
Tweet
Share
More Decks by Zoom.Quiet
See All by Zoom.Quiet
PyCon2014China-Zhuhai-high performance
zoomquiet
0
120
PyCon2014China-Zhuhai-meta programming
zoomquiet
1
88
PyCon2014China-Zhuhai-bpm.py
zoomquiet
0
64
PyCon2014China-Zhuhai-luna kv db
zoomquiet
0
66
PyCon2014China-Zhuhai-seed studio
zoomquiet
0
45
PyCon2014China-Zhuhai-Docker Registry Build By Python
zoomquiet
0
54
PyCon2014China-Zhuhai-jeff
zoomquiet
0
46
PyCon2014China-Zhuhai-pythonic front-end
zoomquiet
0
60
DevFest2014-Zhuhai-Polymer
zoomquiet
0
300
Other Decks in Technology
See All in Technology
新卒1年目がプロジェクトを進めるときにコケたポイント
ryunakayama
1
100
Node-AI のリッチな WEB フロントエンドを支える技術
nenonaninu
2
970
OpenStack再入門「アーキテクチャ編」
kajinamit
0
240
Azure AI サービス全体像と Prompt flow 紹介 - Forkwell Library
shohei1029
1
590
あなたの知らないバグバウンティの世界
eurekaberry
1
1.4k
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
4
34k
SmartHR プロダクトエンジニア求人ガイド 2024上期
smarthr
0
130
プレイヤーとしてのチームのテスト力UP/Improving team skills for testing
goyoki
2
220
SSMエージェントはIAMロールの夢を見るか/ Do SSM Agents Dream Of IAM Roles?
yukihirochiba
0
1.4k
進化する事業とデータ構造 ~Cloudbaseの場合~
tockn
2
230
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
1.8k
Proposal for a fictitious company presented by JAWS-UG DE&I team 'Naniwa Musume'
hiroramos4
PRO
0
120
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
512
39k
GraphQLとの向き合い方2022年版
quramy
28
12k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
242
20k
Large-scale JavaScript Application Architecture
addyosmani
501
110k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
153
14k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
124
32k
Mobile First: as difficult as doing things right
swwweet
215
8.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
14
2.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
225
51k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3k
GitHub's CSS Performance
jonrohan
1023
450k
Build your cross-platform service in a week with App Engine
jlugia
223
17k
Transcript
+ 论 Python 与设计模式 赖勇浩(http://laiyonghao.com) 2013-‐12-‐8
珠海
+ 设计模式?
+ 自我介绍 n 赖勇浩 n 从业 8
年多,主要编程语言是 Python n game -‐> webgame -‐> web n 常在珠三角技术沙龙出没 n http://laiyonghao.com 3
+ PyCon 的老朋友 4
+ 好,正式开始吧! 5
+ 先热热场子…… n 写 Python 代码赚钱的,有没有? 6
+ 先热热场子…… n 写 Python 代码赚钱的,有没有? n
写 Python 超过 1 年的,有没有? 7
+ 再热热场子…… n 读过《设计模式——可复用面向对 象软件的基础》这本书的有没有? 8
+ 再热热场子…… n 读过《设计模式——可复用面向对 象软件的基础》这本书的有没有? n 读过《Head
First 设计模式》 的有没有? 9
10 一个观点
+ 我也希望是这样…… 但事实是…… 11
+ 先看事实:工厂函数(方法) int(…) float(…) type(name, bases,
dict) >>> class X(object): ... a = 1 ... >>> X = type('X', (object,), dict(a=1)) collections.namedtuple() datatime.fromtimestamp(…) Decimal.from_float(…) Fraction.from_float(…) Fraction.from_decimal(…)
+ 先看事实:享元(FlyWeight) >>> i = 10 >>>
j = 5 + 5 >>> id(i) 140479275503872 >>> id(j) 140479275503872 >>> a = 'ab' >>> b = 'a' + 'b' >>> id(a) == id(b) True 13
+ 先看事实:享元(FlyWeight) >>> i = 10 >>>
j = 5 + 5 >>> id(i) 140479275503872 >>> id(j) 140479275503872 >>> a = 'ab' >>> b = 'a' + 'b' >>> id(a) == id(b) True >>> a = a * 10 >>> intern(a) 'abababababababababab' >>> b = 'abababababababababab’ >>> c = 'abababababababababa' + 'b’ >>> d = 'ab' * 10 >>> id(a) == id(b) == id(c) == id(d) True 14
+ 先看事实:适配器 import SocketServer class ThreadedTCPServer(SocketServer.ThreadingMixIn,
SocketServer.TCPServer): pass 15
+ 先看事实:代理模式 >>> import weakref >>> class
A(object):pass ... >>> a = A() >>> a.attr = 1 >>> a.attr 1 >>> a1 = weakref.proxy(a) >>> a1.attr 1 >>> a1 <weakproxy at 0x10dc375d0 to A at 0x10dc3a410> 16
+ 先看事实:模板方法 import SocketServer class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self): self.data = self.request.recv(1024).strip() print "{} wrote:".format(self.client_address[0]) print self.data self.request.sendall(self.data.upper()) 17
+ 先看事实:模板方法 From abc import ABCMeta class
C: __metaclass__ = ABCMeta @abstractmethod def my_abstract_method(self, ...): ... 18
+ 所以,真相是…… n 标准库都在用…… n 标准库都推荐用…… n 怎么可以说不需要?!
19
+ 所以我们不是不需要模式…… 而是要 Pythonic 的模式实现…… 20
+ 不 Pythonic 的设计模式:单例 class Singleton(object):
def __new__(cls, *args, **kw): if not hasattr(cls, '_instance'): orig = super(Singleton, cls) cls._instance = orig.__new__(cls, *args, **kw) return cls._instance 21
+ 单例的三个需求: n 只能有一个实例; n 它必须自行创建这个实例;
n 它必须自行向整个系统提供这个 实例。 22
+ 单例遇上并行 class Singleton(object):
objs = {} objs_locker = threading.Lock() def __new__(cls, *args, **kv): if cls in cls.objs: return cls.objs[cls] 23
+ 单例遇上并行
cls.objs_locker.acquire() try: if cls in cls.objs: return cls.objs[cls] cls.objs[cls] = object.__new__(cls) finally: cls.objs_locker.release() 24
+ Pythonic 的设计模式:单例 n 重新审视 Python 的语法元素,尽量利用已有基础设施。
n 模块 n 所有的变量都会绑定到模块; n 模块只初始化一次; n import 机制是线程安全的(保证了在并发状态下模块也只有一个实 例); n 惯用法: n 文件名首字母大写,如 Moon.py n __all__ 25
+ 不 Pythonic 的设计模式:装饰器 26
+ 代码大概是这样的 class darkroast(Beverage): def cost(self):return
0.99 … class Whip(Beverage): def __init__(self, beverage): self._beverage = beverage def cost(self): return self._beverage.cost() + 0.1 print Whip(Mocha(darkroast())).cost() 27
+ Pythonic 的设计模式:装饰器 def beverage(cost): def
_(orig = 0.0): return orig + cost return _ darkroast = beverage(0.99) whip = beverage(0.1) mocha = beverage(0.2) print whip(mocha(darkroast())) 28
+ 其它设计模式:Borg(MonoState) n 保持对象的唯一性并不重要,只 要共享状态就行 n Alex
Martelli n http:// code.activestate.com/ recipes/66531/ 29
+ 其它设计模式:Borg(MonoState) class Borg(object):
_state = {} def __new__(cls, *args, **kw): ob = super(Borg, cls).__new__(cls, *args, **kw) ob.__dict__ = cls._state return ob 30
+ 动态语言特有的设计模式:mixin n 动态地改变实例的类型的基类,在不修改生成实例过程的情况下, 给实例增加(改变)功能。可用以实现插件框架。 class Bird(object):pass
bird = Bird() class FlyMixin: def fly(self):print 'I can fly.' bird.__class__.__bases__ += (FlyMixin, ) bird.fly() 31
+ 动态语言特有的模式:duck typing n 一个对象有效的语义,不是由继承自特定的类或实现特定的接口, 而是由当前方法和属性的集合决定。 n
当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子, 那么这只鸟就可以被称为鸭子。(James Whitcomb Riley) n 干掉模板方法? n 不,模板方法是想要保证子类实现接口 32
+ 利用设计模式提供更好的接口 n getopt n optparse
n argparse n docopt n Command-‐line interface description language n define interface for your command-‐line app, and n automatically generate parser for it. n 解释器模式 33
+ docopt 34
+ 解释器模式的应用 n GM 指令 player 0
money 10000 player 0 attack 10000 monster 0 die scene monsters die n 过场剧情脚本 monster 0 spawn 0,0 monster 0 moveto 0,-‐10 monster 0 attack monster 0 moveto 0,0 35
+ Q&A
[email protected]
36