蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

设计原则:可替换的协作者与明确的边界:独立运行与结果核对

PYTHONWindows/Python3.12.14标准库;静态浏览器页面使用独立Edge会话,不在线执行Pythonpython-design-pattern-intro.py
python-design-pattern-intro.py30 行
from pathlib import Path
import json,sys
from pattern_cases import *
from pattern_extensions import *

def intro(c,out):
    class Checkout:
        def __init__(self,pay): self.pay=pay
        def buy(self,cents):
            if type(cents) is not int or cents<=0: raise ValueError("positive integer cents")
            return self.pay(cents)
    calls=[]
    c.eq("injected callable",Checkout(lambda cents: calls.append(cents) or "recorded").buy(123),"recorded")
    c.eq("actual recording transport",calls,[123])
    c.raises("invalid amount does not call collaborator",ValueError,lambda:Checkout(calls.append).buy(0))
    c.eq("invalid input leaves calls unchanged",calls,[123])
    c.raises("abstract contract not instantiable",TypeError,Product)
    c.eq("substitute concrete creator",ReportCreator().publish(),"预览:学习报告")



def run_case(out):
    c=Checks()
    intro(c,out)
    return {"chapter":'python-design-pattern-intro',"checks":c.rows,"passed":True,"scope":"named owned all24chapter named cases only; full source coverage pending"}

if __name__ == "__main__":
    output=Path(sys.argv[1]);output.mkdir(parents=True,exist_ok=False)
    print(json.dumps(run_case(output),ensure_ascii=False,default=repr))
本例需在 Windows/Python3.12.14标准库;静态浏览器页面使用独立Edge会话,不在线执行Python 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows/Python3.12.14标准库;静态浏览器页面使用独立Edge会话,不在线执行Python

入口文件:projects/patterns-lab/python-design-pattern-intro.py

预期结果与验证条件观察提示
  • 当前固定输入命名检查符合明确实际结果及失败策略
  • 每章输出、源码和预期分别对应;本地模型不冒充生产系统,未验能力继续单列

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Python 设计模式

课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202611a526.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习