当前位置: 首页 > news >正文 网站图片修改邢台是哪个省的城市 news 2025/11/2 17:35:23 网站图片修改,邢台是哪个省的城市,wordpress多重筛选机制,网站搜索要怎么做抽象工厂模式 (Abstract Factory) 抽象工厂模式 是一种创建型设计模式#xff0c;提供一个接口#xff0c;用于创建一组相关或互相依赖的对象#xff0c;而无需指定它们的具体类。 意图 提供一个创建一组相关对象的接口#xff0c;而无需指定它们的具体类。解决产品对象之…抽象工厂模式 (Abstract Factory) 抽象工厂模式 是一种创建型设计模式提供一个接口用于创建一组相关或互相依赖的对象而无需指定它们的具体类。 意图 提供一个创建一组相关对象的接口而无需指定它们的具体类。解决产品对象之间的相互依赖问题使得一组对象能协同工作。 使用场景 需要创建一组相关对象 如 GUI 程序中窗口、按钮、菜单等需要统一的外观风格。 需要保证对象之间的兼容性 如游戏开发中不同种族的士兵和建筑需要互相匹配。 需要隐藏具体实现 客户端代码只依赖于产品接口具体实现被封装。 参与者角色 抽象工厂 (Abstract Factory) 声明一组用于创建相关对象的接口。 具体工厂 (Concrete Factory) 实现抽象工厂的接口创建具体的产品。 抽象产品 (Abstract Product) 定义所有产品的公共接口。 具体产品 (Concrete Product) 实现抽象产品的接口为具体工厂生产的对象。 示例代码 以下示例展示了如何使用抽象工厂模式为不同操作系统创建相关的 GUI 组件按钮和窗口。 #include iostream #include memory// 抽象产品 A: 按钮 class Button { public:virtual void render() const 0;virtual ~Button() {} };// 具体产品 A1: Windows 按钮 class WindowsButton : public Button { public:void render() const override {std::cout Rendering Windows Button std::endl;} };// 具体产品 A2: Mac 按钮 class MacButton : public Button { public:void render() const override {std::cout Rendering Mac Button std::endl;} };// 抽象产品 B: 窗口 class Window { public:virtual void render() const 0;virtual ~Window() {} };// 具体产品 B1: Windows 窗口 class WindowsWindow : public Window { public:void render() const override {std::cout Rendering Windows Window std::endl;} };// 具体产品 B2: Mac 窗口 class MacWindow : public Window { public:void render() const override {std::cout Rendering Mac Window std::endl;} };// 抽象工厂 class GUIFactory { public:virtual std::unique_ptrButton createButton() const 0;virtual std::unique_ptrWindow createWindow() const 0;virtual ~GUIFactory() {} };// 具体工厂 1: Windows 工厂 class WindowsFactory : public GUIFactory { public:std::unique_ptrButton createButton() const override {return std::make_uniqueWindowsButton();}std::unique_ptrWindow createWindow() const override {return std::make_uniqueWindowsWindow();} };// 具体工厂 2: Mac 工厂 class MacFactory : public GUIFactory { public:std::unique_ptrButton createButton() const override {return std::make_uniqueMacButton();}std::unique_ptrWindow createWindow() const override {return std::make_uniqueMacWindow();} };// 主函数 int main() {// 选择具体工厂std::unique_ptrGUIFactory factory std::make_uniqueWindowsFactory();// 使用工厂创建产品auto button factory-createButton();auto window factory-createWindow();button-render(); // 输出Rendering Windows Buttonwindow-render(); // 输出Rendering Windows Window// 切换到另一种具体工厂factory std::make_uniqueMacFactory();button factory-createButton();window factory-createWindow();button-render(); // 输出Rendering Mac Buttonwindow-render(); // 输出Rendering Mac Windowreturn 0; }代码解析 1. 抽象产品类 定义了产品的公共接口确保产品类型的一致性 class Button { public:virtual void render() const 0;virtual ~Button() {} };2. 具体产品类 实现了抽象产品接口并提供具体功能 class WindowsButton : public Button { public:void render() const override {std::cout Rendering Windows Button std::endl;} };3. 抽象工厂 定义了创建一组相关产品的接口 class GUIFactory { public:virtual std::unique_ptrButton createButton() const 0;virtual std::unique_ptrWindow createWindow() const 0;virtual ~GUIFactory() {} };4. 具体工厂 实现了抽象工厂接口负责生产具体的产品 class WindowsFactory : public GUIFactory { public:std::unique_ptrButton createButton() const override {return std::make_uniqueWindowsButton();}std::unique_ptrWindow createWindow() const override {return std::make_uniqueWindowsWindow();} };5. 主函数 客户端代码通过工厂接口创建产品无需知道具体产品的实现 std::unique_ptrGUIFactory factory std::make_uniqueWindowsFactory(); auto button factory-createButton(); auto window factory-createWindow(); button-render(); window-render();优缺点 优点 解耦客户端代码只依赖抽象工厂和抽象产品与具体实现无关。保证产品一致性抽象工厂确保一组对象是兼容的。扩展性强可以通过新增具体工厂扩展系统。 缺点 增加复杂性每增加一组产品需要新增抽象类和具体类。灵活性受限对现有产品结构的修改可能需要更新所有工厂。 适用场景 跨平台开发如为不同操作系统设计统一的 GUI。产品族设计如游戏中不同种族的士兵和建筑需要互相匹配。动态扩展产品族如增加新的平台支持。 改进与扩展 结合配置文件 动态选择具体工厂根据运行时条件决定产品族的创建 std::unique_ptrGUIFactory factory; if (platform Windows) {factory std::make_uniqueWindowsFactory(); } else {factory std::make_uniqueMacFactory(); }引入依赖注入框架 通过依赖注入简化工厂选择的逻辑使得代码更易于维护和测试。 总结 抽象工厂模式通过提供一个接口用于创建一组相关对象实现了创建逻辑的集中化和产品族之间的兼容性。 它适用于需要跨平台开发或产品族设计的场景。 文章转载自: http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn 查看全文 http://www.tj-hxxt.cn/news/271477.html 相关文章: 做网站给源码吗wordpress架设教程视频 佛山网站建设3lue3lue应该选用什么口罩 河北网站建设哪家好宁波品牌网站制作哪家好 蒙晟建设有限公司官方网站h5网站建设是什么意思 做网站的公司有前途吗wordpress用户反馈 企业建站公司报价高端网站建设大概多少费用 有没有能用的网站wordpress彩票类模板 网站建设学多久网站建设公司十大 网站模板建设报价wordpress如何建企业站 免费WordPress门户一号广州seo排名优化公司 做酒店经理的一些网站网站建设与管期末试题 旅游网站开发工具百度采购网官方网站 做网站广告经营者中信建设有限责任公司历任董事长 建站到网站收录到优化婚庆策划公司招聘 网站工信部备案流程手机网站拒绝访问怎么解决 网站keyword如何排序企业网站开发需求分析 宁夏建设工程交易中心网站广州市线下教学 dremwear做网站做家居建材出口网站有哪些 广州大石附近做网站的公司电商平台推广员是做什么的 辽宁关键词优化排名外包wordpress优化指南 沈阳地区精神文明建设网站网上推广怎么拉客户 北京最大网站建设公司排名网站建设工作室的营销方式创业计划书 网站开发整体制作流程做seo网站公司 引擎搜索优化巩义做网站xd seo 深圳网站开发工资物流网络化 管理系统是网站吗专业的猎头公司 雨燕直播东莞搜索网络优化 黑黄logo网站网站的域名怎么起 邯郸市有搞网站服服务的吗河源今天发生的重大新闻 手机网站用什么程序做wordpress+好用插件