
设计模式
Python设计模式
诗与浪子
这个作者很懒,什么都没留下…
展开
-
【设计模式】装饰器模式
from abc import ABC, abstractmethodclass PriceComputer(ABC): @abstractmethod def compute_price(self): passclass Goods(PriceComputer): def __init__(self, price): self.price = price def compute_price(self): re原创 2022-01-14 12:23:49 · 132 阅读 · 0 评论 -
【设计模式】适配器模式
from abc import ABC, abstractmethod# Adapteeclass Power110V: def output(self): return 110# Adapteeclass Power20V: def output(self): return 20# Targetclass PowerStandard(ABC): @abstractmethod def output220V(self):原创 2022-01-14 17:22:58 · 174 阅读 · 0 评论 -
【设计模式】外观模式
class CPU: def compute(self, instructions): """提供计算功能""" print(eval(instructions))class Disk: def __init__(self): self.data = {} def persist(self, data): """提供数据持久化功能""" self.data.update(data)cla原创 2022-01-14 12:48:57 · 339 阅读 · 0 评论 -
【设计模式】备忘录模式
class Memento: def __init__(self, state): self.state = state def set_state(self, state): self.state = state def get_state(self): return self.stateclass Caretaker: def __init__(self): self.memento_table原创 2022-01-14 14:58:11 · 128 阅读 · 0 评论 -
【设计模式】过滤器模式
from abc import ABC, abstractmethodclass Request: def __init__(self, cookie=None, user_agent=None): self.cookie = cookie self.user_agent = user_agentclass Filter(ABC): @abstractmethod def filter(self, request): p原创 2022-01-14 18:19:43 · 193 阅读 · 0 评论