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

网页设计与网站建设论文南昌网站seo外包服务

网页设计与网站建设论文,南昌网站seo外包服务,企业建立网站需要,西安建筑信息平台前言 在软件工程领域,设计模式是解决常见问题的经典方案。本文将深入探讨两种创建型模式:工厂方法模式和抽象工厂模式,通过理论解析与实战代码示例,帮助开发者掌握这两种模式的精髓。 一、工厂方法模式(Factory Metho…

前言

在软件工程领域,设计模式是解决常见问题的经典方案。本文将深入探讨两种创建型模式:工厂方法模式抽象工厂模式,通过理论解析与实战代码示例,帮助开发者掌握这两种模式的精髓。


一、工厂方法模式(Factory Method Pattern)

1.1 模式思想

工厂方法模式的核心在于将对象的创建延迟到子类,通过定义创建对象的接口,让子类决定实例化哪个类。这种模式完美体现了依赖倒置原则

1.2 模式结构

  • Product:抽象产品接口
  • ConcreteProduct:具体产品实现
  • Creator:抽象创建者
  • ConcreteCreator:具体创建者

1.3 代码示例

#include <iostream>
#include <string>class Shape {
public:virtual void draw() = 0;
};class Circle : public Shape {void draw() {std::cout << "Circle::draw()" << std::endl;}
};class Rectangle : public Shape {void draw() {std::cout << "Rectangle::draw()" << std::endl;}
};class Square : public Shape {void draw() {std::cout << "Square::draw()" << std::endl;}
};class ShapeFactory {
public:Shape* getShape(const std::string& shapeType) {if (shapeType.empty()) {return nullptr;}if (shapeType == "CIRCLE") {return new Circle();} else if (shapeType == "RECTANGLE") {return new Rectangle();} else if (shapeType == "SQUARE") {return new Square();}return nullptr;}
};
#include "factory_mode.h"int main(int argc, char const* argv[]) {ShapeFactory* shapeFactory = new ShapeFactory();Shape* shape1 = shapeFactory->getShape("CIRCLE");shape1->draw();Shape* shape2 = shapeFactory->getShape("RECTANGLE");shape2->draw();Shape* shape3 = shapeFactory->getShape("SQUARE");shape3->draw();delete shapeFactory;return 0;
}

1.4 运行结果

1.5 适用场景

  • 需要灵活扩展产品类型
  • 创建过程需要封装处理逻辑
  • 需要解耦客户端与具体产品类

二、抽象工厂模式(Abstract Factory Pattern)

2.1 模式思想

抽象工厂模式通过创建相关对象族来提升系统的扩展性,强调产品系列的概念。它比工厂方法模式更高层次的抽象。

2.2 模式结构

  • AbstractFactory:抽象工厂接口
  • ConcreteFactory:具体工厂实现
  • AbstractProduct:抽象产品接口
  • ConcreteProduct:具体产品实现

2.3 代码示例

产品定义:

#include <iostream>
#include <string>
// 
class Color {
public:virtual void fill() = 0;
};class Red : public Color {
public:void fill() {std::cout << "Red::fill()" << std::endl;}
};class Green : public Color {
public:void fill() {std::cout << "Green::fill()" << std::endl;}
};class Blue : public Color {
public:void fill() {std::cout << "Blue::fill()" << std::endl;}
};
// 形状产品
class Shape {
public:virtual void draw() = 0;
};class Circle : public Shape {void draw() {std::cout << "Circle::draw()" << std::endl;}
};class Rectangle : public Shape {void draw() {std::cout << "Rectangle::draw()" << std::endl;}
};class Square : public Shape {void draw() {std::cout << "Square::draw()" << std::endl;}
};

 抽象工厂

#include "color.h"
#include "shape.h"
// 抽象工厂
class AbstractFactory {
public:virtual Shape* getShape(const std::string& shapeType) = 0;virtual Color* getColor(const std::string& colorType) = 0;
};

 实体工厂

#include "abstract_factory.h"
// 实体工厂类// 形状工厂
class ShapeFactory : public AbstractFactory {
public:Shape* getShape(const std::string& shapeType) {if (shapeType.empty()) {return nullptr;}if (shapeType == "CIRCLE") {return new Circle();} else if (shapeType == "RECTANGLE") {return new Rectangle();} else if (shapeType == "SQUARE") {return new Square();}return nullptr;}Color* getColor(const std::string& colorType) {return nullptr;}
};// 颜色工厂
class ColorFactory : public AbstractFactory {
public:Color* getColor(const std::string& colorType) {if (colorType.empty()) {return nullptr;}if (colorType == "RED") {return new Red();} else if (colorType == "GREEN") {return new Green();} else if (colorType == "BLUE") {return new Blue();}return nullptr;}Shape* getShape(const std::string& shapeType) {return nullptr;}
};

 生产商:

#include "factory.h"class FactoryProducer {
public:static AbstractFactory* getFactory(const std::string& choice) {if (choice == "SHAPE") {return new ShapeFactory();} else if (choice == "COLOR") {return new ColorFactory();}return nullptr;}
};

main:

#include "producer.h"// 抽象工厂 demo
int main(int argc, char const* argv[]) {AbstractFactory* shapeFactory = FactoryProducer::getFactory("SHAPE");Shape* shape1 = shapeFactory->getShape("CIRCLE");shape1->draw();Shape* shape2 = shapeFactory->getShape("RECTANGLE");shape2->draw();Shape* shape3 = shapeFactory->getShape("SQUARE");shape3->draw();AbstractFactory* colorFactory = FactoryProducer::getFactory("COLOR");Color* color1 = colorFactory->getColor("RED");color1->fill();Color* color2 = colorFactory->getColor("GREEN");color2->fill();Color* color3 = colorFactory->getColor("BLUE");color3->fill();delete shapeFactory;delete colorFactory;return 0;
}

2.4 运行结果

2.5 适用场景

  • 需要创建多个相关对象组成的系列
  • 系统需要支持不同产品族的切换
  • 产品对象之间存在约束关系

三、核心差异对比

维度工厂方法模式抽象工厂模式
抽象层次单个产品创建产品族创建
扩展方向垂直扩展(新增产品类型)水平扩展(新增产品族)
实现方式继承组合
系统复杂度简单复杂
典型应用场景日志记录器、支付方式跨平台UI组件、数据库访问

四、模式选择指南

  • 选择工厂方法模式当:

    • 需要解耦客户端与具体产品类
    • 系统需要支持多种同类产品的创建
    • 产品类型相对单一
  • 选择抽象工厂模式当:

    • 需要创建多个相互关联的产品
    • 需要保证产品之间的兼容性
    • 系统需要支持不同产品族的切换

五、最佳实践建议

  1. 优先使用工厂方法:当产品结构简单时,避免过度设计
  2. 注意开闭原则:通过扩展而非修改来增加新产品
  3. 使用依赖注入:结合Spring等框架实现更灵活的工厂管理
  4. 文档化产品族:明确各产品之间的约束关系
  5. 性能考量:复杂工厂实现需要考虑对象池等优化手段

结语

        掌握工厂模式是成为架构师的重要阶梯。工厂方法模式像专业工匠,专注单一产品的精雕细琢;抽象工厂模式如生产总监,统筹协调整个产品家族。理解它们的差异,才能在系统设计中做出最合适的选择。 


文章转载自:

http://Kn0jyY0I.gqdsm.cn
http://VRVrx4DD.gqdsm.cn
http://ijbU1b7A.gqdsm.cn
http://mxuBw5RP.gqdsm.cn
http://VwgCB3I4.gqdsm.cn
http://pYPHlsFn.gqdsm.cn
http://MxJmYzfs.gqdsm.cn
http://JKkFtGen.gqdsm.cn
http://y8LNYC00.gqdsm.cn
http://5t40FbnF.gqdsm.cn
http://7ClksCmH.gqdsm.cn
http://zX4ZJCiX.gqdsm.cn
http://6zy8Ddo3.gqdsm.cn
http://43qrvJc4.gqdsm.cn
http://89Qy4n8V.gqdsm.cn
http://oV8NpEcx.gqdsm.cn
http://xiXPKmoR.gqdsm.cn
http://FV8vvwS3.gqdsm.cn
http://N6i6z3uC.gqdsm.cn
http://dMt1EAlA.gqdsm.cn
http://LSg3hccL.gqdsm.cn
http://gIh6ZQmM.gqdsm.cn
http://iiJxKDuB.gqdsm.cn
http://p2KCGMV5.gqdsm.cn
http://EBf5xApd.gqdsm.cn
http://AYpT3y6W.gqdsm.cn
http://isE4Bmep.gqdsm.cn
http://Ct9XbqpP.gqdsm.cn
http://QiSSZWwY.gqdsm.cn
http://T4xvw1M2.gqdsm.cn
http://www.dtcms.com/wzjs/695663.html

相关文章:

  • 万达做的电商网站建设工程招标网站
  • 网站建设做网站怎么做wordpress在哪登陆
  • 恒基建设集团网站地址湖南衡阳市建设工程造价网站
  • 建立网站要多少钱WordPress数据库授权
  • 网站开发需要多久企业网站建设 调研
  • 花都区营销型网站建设设计商标
  • 提升审美网站电商的运营模式有几种
  • 网站qq链接怎么做做教案找资料有哪些网站
  • 如何做网络推广网站哪里有网站建设官网
  • 网站的建设与运营专业好的营销网站设计公司
  • 做的网站缩小内容就全乱了wordpress安装后慢的不行
  • 吉林网站建设代理渠道义乌建站
  • 龙岩网站建设极速建站wordpress上传logo
  • 网站制作公司中怎么把网站排名优化
  • 目前做的比较好的法律网站有哪些上海制造业企业100强
  • 昆山网站优化微信手机网站
  • 品牌网站开发背景清明节ppt模板免费下载
  • 深圳市盐田区住房和建设局网站济南官网
  • 求个国外在线网站杭州 网站开发
  • 好网站网络推广员压力大吗
  • 门窗网站模板百度快速排名优化技术
  • 青岛建设银行网站首页深圳做网站d
  • 商城网站的基本功能做网站济南西
  • 网站节点加速如何做app软件开发
  • 辽宁网站建设论坛计算机网站开发是什么专业
  • 江宁网站建设价位大公司网站开发
  • 做公司网站有没有必要数字货币网站开发需求
  • 京东商城网站建设分析秦皇岛金洋建设集团网站
  • 有没有专业做steam创客的网站个人网站开发的意义
  • 全网营销一站式推广网站页面设计怎么分析