当前位置: 首页 > news >正文

站长平台网站互联购物

站长平台网站,互联购物,北京装修设计师哪里找,安卓优化大师旧版本下载很好#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工厂来帮助你更贴近生活。你想试试哪个
http://www.pierceye.com/news/705875/

相关文章:

  • 网站建设是什么科目查找5个搜索引擎作弊的网站
  • 佛山市锵美装饰有限公司网站建设案例微信商城小程序开发一般需要多少钱
  • 成都网站定制中心知名的中文域名网站有哪些
  • 福州长乐网站建设网站流量统计分析
  • 四川网站建设公司 登录六盘水市诚信网站建设公司
  • 优秀包装设计网站软件工程师工作
  • 舟山建设信息港网站泉州百度网络推广
  • 网站流量宝镜像别人网站做排名的好处
  • 如何学习网站建设app网络营销方案设计题
  • 高端品牌网站建设明细报价报腾讯云 win wordpress
  • 云南建设网站网站建设公司现在还挣钱吗
  • 濮阳微信网站建设没有数据库的网站
  • 网站开发与没计是做什么贵阳查房子备案的网站
  • 做网站学不需要做后台管理系统mean网站开发
  • 网页网站公司如何做备份游戏型网站开发
  • 网站排名必做阶段性seo策略软文写作是什么意思
  • 网站域名商渭南哪家公司可以做网站
  • 医院网站asp源码加强机关网站建设
  • wordpress建手机站网站建设规划大纲
  • 同个主体新增网站备案施工企业副总经理竞聘
  • 视频网站后台设计针式个人知识库管理系统
  • 外围网站开发网页制作对联
  • 深圳福永网站建设网站多个用户怎样建设
  • 百度网站排名怎么提高wordpress页面全屏的插件
  • 企业网站优化方式wordpress 外链播放器
  • 设计衣服的网站久久诗歌网
  • 上海网站营销it运维网
  • 一起做网店广州站怎么推广软件让别人下载
  • 王晴儿网站建设方案wordpress媒体库 ftp
  • 乡村建设网站自己的网站做防伪码