外观:简化工作流与真实事务
统一入口协调子系统,失败与补偿也应有明确结果。
本页展示最近一次实际本地Python命名测试结果,不是在线Python解释器。外部服务、真实硬件及完整来源语义仍分别验收。
| 检查项 | 实际结果 | 预期结果 |
|---|---|---|
| ordered start | ["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频"] | ["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频"] |
| cannot double start | "RuntimeError" | "RuntimeError" |
| end idempotent | ["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频", "player.stop", "amplifier.off", "projector.off", "screen.up", "lights.on"] | ["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频", "player.stop", "amplifier.off", "projector.off", "screen.up", "lights.on"] |
| final device model | "off" | "off" |
| transaction failure surfaced | "IntegrityError" | "IntegrityError" |
| actual rollback of first op | [[1, "first"]] | [[1, "first"]] |
| real file workflow | "MBSKY.COM" | "MBSKY.COM" |
| actual output file | "MBSKY.COM" | "MBSKY.COM" |
| missing file propagates | "FileNotFoundError" | "FileNotFoundError" |
| music does not activate projector | false | false |
| game actual model state | "game" | "game" |
| order facade actual stock | 1 | 1 |
| order notification local record | "recorded" | "recorded" |
本章源码与生成文件
查看当前核心代码
from pathlib import Path
import json,sys
from pattern_cases import *
from pattern_extensions import *
def facade(c,out):
theater=TheaterFacade();theater.watch("学习视频")
c.eq("ordered start",theater.events,["lights.dim","screen.down","projector.on","amplifier.on","player.play:学习视频"])
c.raises("cannot double start",RuntimeError,lambda:theater.watch("other"));theater.end();before=list(theater.events);theater.end();c.eq("end idempotent",theater.events,before)
c.eq("final device model",theater.state,"off")
db=DatabaseFacade(out/"facade.sqlite");db.transaction([("CREATE TABLE items(id INTEGER PRIMARY KEY,title TEXT NOT NULL)",())])
db.transaction([("INSERT INTO items VALUES(?,?)",(1,"first"))])
c.raises("transaction failure surfaced",sqlite3.IntegrityError,lambda:db.transaction([("INSERT INTO items VALUES(?,?)",(2,"second")),("INSERT INTO items VALUES(?,?)",(1,"duplicate"))]))
c.eq("actual rollback of first op",db.query("SELECT id,title FROM items ORDER BY id"),[(1,"first")])
input_file=out/"input.txt";output_file=out/"output.txt";input_file.write_text(" mbsky.com ",encoding="utf8")
def process_file(src,dst):
text=src.read_text(encoding="utf8");value=text.strip().upper();dst.write_text(value,encoding="utf8");return value
c.eq("real file workflow",process_file(input_file,output_file),"MBSKY.COM")
c.eq("actual output file",output_file.read_text(encoding="utf8"),"MBSKY.COM")
c.raises("missing file propagates",FileNotFoundError,lambda:process_file(out/"missing.txt",output_file))
def extra_facade(c,out):
class Entertainment(TheaterFacade):
def music(self,title):
if self.state!="off":raise RuntimeError("already active")
self.events.extend(["amplifier.on","audio.play:"+title]);self.state="music"
def game(self,title):
if self.state!="off":raise RuntimeError("already active")
self.events.extend(["screen.down","projector.on","game.play:"+title]);self.state="game"
music=Entertainment();music.music("学习背景音乐");c.eq("music does not activate projector","projector.on"in music.events,False);game=Entertainment();game.game("本地游戏模型");c.eq("game actual model state",game.state,"game")
order=OrderCoordinator();result=order.place("facade-demo",1);c.eq("order facade actual stock",order.stock,1);c.eq("order notification local record",result["notification"],"recorded")
def run_case(out):
c=Checks()
facade(c,out)
extra_facade(c,out)
return {"chapter":'python-facade',"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))