站长平台网站,互联购物,北京装修设计师哪里找,安卓优化大师旧版本下载很好#xff01;你现在已经开始接触设计模式了#xff0c;而**抽象工厂模式#xff08;Abstract Factory Pattern#xff09;是一种常用于“创建一系列相关产品”**的经典设计模式。
我会一步步帮你理解#xff1a; #x1f9e0; 一句话解释 抽象工厂模式#xff1a;提…很好你现在已经开始接触设计模式了而**抽象工厂模式Abstract Factory Pattern是一种常用于“创建一系列相关产品”**的经典设计模式。
我会一步步帮你理解 一句话解释 抽象工厂模式提供一个接口用于创建一系列相关或相互依赖的对象而无需指定它们的具体类。 为什么需要它
当你面对以下场景时它就很有用 “我有多个产品按钮、窗口、菜单而且这些产品有不同的风格比如 Mac 风格、Windows 风格希望能在不改代码的前提下切换整套产品风格。” ✅ 举个生活类比 你要布置房间选一整套家具。你不能桌子 IKEA 风、椅子中式风格应该风格统一。 工厂1生产一整套「现代风」家具现代风沙发 现代风桌子工厂2生产一整套「中式风」家具中式风沙发 中式风桌子
→ 你只选工厂不关心家具内部怎么造。 Python 完整代码示例 Step 1: 抽象产品类
from abc import ABC, abstractmethodclass Button(ABC):abstractmethoddef click(self):passclass Window(ABC):abstractmethoddef open(self):passStep 2: 具体产品类
class MacButton(Button):def click(self):print(Mac Button clicked!)class WinButton(Button):def click(self):print(Windows Button clicked!)class MacWindow(Window):def open(self):print(Mac Window opened!)class WinWindow(Window):def open(self):print(Windows Window opened!)️ Step 3: 抽象工厂类
class GUIFactory(ABC):abstractmethoddef create_button(self) - Button:passabstractmethoddef create_window(self) - Window:passStep 4: 具体工厂类
class MacFactory(GUIFactory):def create_button(self):return MacButton()def create_window(self):return MacWindow()class WinFactory(GUIFactory):def create_button(self):return WinButton()def create_window(self):return WinWindow()️ Step 5: 客户端代码
def render_ui(factory: GUIFactory):button factory.create_button()window factory.create_window()button.click()window.open()# 使用 Mac 风格
render_ui(MacFactory())
# 使用 Windows 风格
render_ui(WinFactory())流程图Mermaid #mermaid-svg-HdC442c8VtOVhELe {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-HdC442c8VtOVhELe .error-icon{fill:#552222;}#mermaid-svg-HdC442c8VtOVhELe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-HdC442c8VtOVhELe .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-HdC442c8VtOVhELe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-HdC442c8VtOVhELe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-HdC442c8VtOVhELe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-HdC442c8VtOVhELe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-HdC442c8VtOVhELe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-HdC442c8VtOVhELe .marker.cross{stroke:#333333;}#mermaid-svg-HdC442c8VtOVhELe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-HdC442c8VtOVhELe g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-HdC442c8VtOVhELe g.classGroup text .title{font-weight:bolder;}#mermaid-svg-HdC442c8VtOVhELe .nodeLabel,#mermaid-svg-HdC442c8VtOVhELe .edgeLabel{color:#131300;}#mermaid-svg-HdC442c8VtOVhELe .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-HdC442c8VtOVhELe .label text{fill:#131300;}#mermaid-svg-HdC442c8VtOVhELe .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-HdC442c8VtOVhELe .classTitle{font-weight:bolder;}#mermaid-svg-HdC442c8VtOVhELe .node rect,#mermaid-svg-HdC442c8VtOVhELe .node circle,#mermaid-svg-HdC442c8VtOVhELe .node ellipse,#mermaid-svg-HdC442c8VtOVhELe .node polygon,#mermaid-svg-HdC442c8VtOVhELe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-HdC442c8VtOVhELe .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-HdC442c8VtOVhELe g.clickable{cursor:pointer;}#mermaid-svg-HdC442c8VtOVhELe g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-HdC442c8VtOVhELe g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-HdC442c8VtOVhELe .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-HdC442c8VtOVhELe .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-HdC442c8VtOVhELe .dashed-line{stroke-dasharray:3;}#mermaid-svg-HdC442c8VtOVhELe #compositionStart,#mermaid-svg-HdC442c8VtOVhELe .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #compositionEnd,#mermaid-svg-HdC442c8VtOVhELe .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #dependencyStart,#mermaid-svg-HdC442c8VtOVhELe .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #dependencyStart,#mermaid-svg-HdC442c8VtOVhELe .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #extensionStart,#mermaid-svg-HdC442c8VtOVhELe .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #extensionEnd,#mermaid-svg-HdC442c8VtOVhELe .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #aggregationStart,#mermaid-svg-HdC442c8VtOVhELe .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe #aggregationEnd,#mermaid-svg-HdC442c8VtOVhELe .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-HdC442c8VtOVhELe .edgeTerminals{font-size:11px;}#mermaid-svg-HdC442c8VtOVhELe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} GUIFactory create_button() create_window() MacFactory create_button() create_window() WinFactory create_button() create_window() Button click() Window open() MacButton click() WinButton click() MacWindow open() WinWindow open() ✅ 优点
✅ 保证产品族之间的一致性按钮 窗口风格一致✅ 封装创建逻辑符合开闭原则✅ 易于切换产品系列换工厂即可 ❌ 缺点
❌ 类和接口数量增加每种产品都要写抽象 实现❌ 扩展“新产品”不方便比如新增菜单→ 要修改所有工厂 总结口诀 ✅ 抽象工厂负责生产“产品系列”只换工厂不动逻辑。适用于“同一风格的多组件系统”。 如果你希望我可以把这个例子替换成游戏皮肤工厂、汽车工厂、手机UI工厂来帮助你更贴近生活。你想试试哪个