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

永年哪做网站在线设计平台有什么用

永年哪做网站,在线设计平台有什么用,如何为产品做网站,潍坊关键词优化软件简单工厂模式(Simple Factory Pattern)是最简单的工厂模式,它不属于GoF 23种设计模式,但它是工厂方法模式和抽象工厂模式的基础。 概念解析 简单工厂模式的核心思想是: 将对象的创建逻辑集中在一个工厂类中 客户端不…

        简单工厂模式(Simple Factory Pattern)是最简单的工厂模式,它不属于GoF 23种设计模式,但它是工厂方法模式和抽象工厂模式的基础。

概念解析

简单工厂模式的核心思想是:

  1. 将对象的创建逻辑集中在一个工厂类中

  2. 客户端不需要知道具体产品的类名,只需要知道产品对应的参数

  3. 通过传入不同的参数来创建不同的产品对象

主要组成部分

  1. 工厂类(Factory):负责创建具体产品的类,包含创建产品的静态或非静态方法

  2. 抽象产品(Product):定义产品的接口

  3. 具体产品(Concrete Product):实现抽象产品接口的具体类

代码示例

下面是一个完整的简单工厂模式示例,包含详细注释:

#include <iostream>
#include <memory>
#include <string>// 抽象产品类:形状
class Shape {
public:virtual void draw() = 0;virtual ~Shape() = default;
};// 具体产品类:圆形
class Circle : public Shape {
public:void draw() override {std::cout << "绘制圆形 ○" << std::endl;}
};// 具体产品类:矩形
class Rectangle : public Shape {
public:void draw() override {std::cout << "绘制矩形 ▭" << std::endl;}
};// 具体产品类:三角形
class Triangle : public Shape {
public:void draw() override {std::cout << "绘制三角形 △" << std::endl;}
};// 形状工厂类
class ShapeFactory {
public:// 创建形状的静态方法static std::unique_ptr<Shape> createShape(const std::string& shapeType) {if (shapeType == "Circle") {return std::make_unique<Circle>();} else if (shapeType == "Rectangle") {return std::make_unique<Rectangle>();} else if (shapeType == "Triangle") {return std::make_unique<Triangle>();} else {throw std::invalid_argument("未知的形状类型: " + shapeType);}}// 非静态创建方法std::unique_ptr<Shape> createShapeNonStatic(const std::string& shapeType) {return createShape(shapeType); // 重用静态方法的实现}
};// 客户端代码
int main() {std::cout << "=== 简单工厂模式演示 ===" << std::endl;try {// 使用静态工厂方法std::cout << "\n使用静态工厂方法:" << std::endl;auto circle = ShapeFactory::createShape("Circle");auto rectangle = ShapeFactory::createShape("Rectangle");auto triangle = ShapeFactory::createShape("Triangle");circle->draw();rectangle->draw();triangle->draw();// 使用非静态工厂方法std::cout << "\n使用非静态工厂方法:" << std::endl;ShapeFactory factory;auto circle2 = factory.createShapeNonStatic("Circle");auto rectangle2 = factory.createShapeNonStatic("Rectangle");auto triangle2 = factory.createShapeNonStatic("Triangle");circle2->draw();rectangle2->draw();triangle2->draw();// 测试未知类型std::cout << "\n测试未知类型:" << std::endl;auto unknown = ShapeFactory::createShape("Unknown"); // 这将抛出异常} catch (const std::exception& e) {std::cerr << "错误: " << e.what() << std::endl;}return 0;
}

模式优势

  1. 封装创建逻辑:将对象的创建过程集中管理,客户端无需关心创建细节

  2. 解耦:客户端代码与具体产品类解耦,只依赖抽象产品接口

  3. 简化客户端:客户端只需要知道产品对应的参数,不需要知道具体类名

  4. 易于扩展:添加新产品时只需要修改工厂类,不需要修改客户端代码

适用场景

  1. 当需要创建的对象较少且不会频繁变化时

  2. 当客户端不需要知道具体产品类名,只需要知道类型参数时

  3. 当需要集中管理对象的创建逻辑时

  4. 作为更复杂工厂模式(工厂方法、抽象工厂)的基础

    实现变体

    1. 使用枚举代替字符串

    enum class ShapeType { Circle, Rectangle, Triangle };class ShapeFactory {
    public:static std::unique_ptr<Shape> createShape(ShapeType type) {switch (type) {case ShapeType::Circle: return std::make_unique<Circle>();case ShapeType::Rectangle: return std::make_unique<Rectangle>();case ShapeType::Triangle: return std::make_unique<Triangle>();default: throw std::invalid_argument("未知的形状类型");}}
    };

    2. 使用模板工厂

    template <typename T>
    class GenericFactory {
    public:static std::unique_ptr<T> create() {return std::make_unique<T>();}
    };// 使用方式
    auto circle = GenericFactory<Circle>::create();

    3. 使用注册机制的可扩展工厂

    class ShapeFactory {
    private:using Creator = std::function<std::unique_ptr<Shape>()>;static std::map<std::string, Creator> creators;public:static void registerShape(const std::string& name, Creator creator) {creators[name] = creator;}static std::unique_ptr<Shape> createShape(const std::string& name) {auto it = creators.find(name);if (it != creators.end()) {return it->second();}throw std::invalid_argument("未知的形状类型: " + name);}
    };// 初始化静态成员
    std::map<std::string, ShapeFactory::Creator> ShapeFactory::creators = {{"Circle", []() { return std::make_unique<Circle>(); }},{"Rectangle", []() { return std::make_unique<Rectangle>(); }},{"Triangle", []() { return std::make_unique<Triangle>(); }}
    };// 可以动态注册新类型
    ShapeFactory::registerShape("Hexagon", []() { return std::make_unique<Hexagon>(); });

            

与工厂方法模式的区别

  1. 简单工厂

    • 一个工厂类负责创建所有产品

    • 通过参数判断创建哪种产品

    • 不符合开闭原则(添加新产品需要修改工厂类)

  2. 工厂方法

    • 为每种产品提供一个工厂类

    • 通过多态创建产品

    • 符合开闭原则(添加新产品只需添加新工厂类)


文章转载自:

http://X9UJ9ega.tqLhn.cn
http://TGxgs7P3.tqLhn.cn
http://wWz3rArd.tqLhn.cn
http://hXn4IDz3.tqLhn.cn
http://hKcUBNLy.tqLhn.cn
http://OyAObXDI.tqLhn.cn
http://dnV75GyQ.tqLhn.cn
http://bSAFuj5S.tqLhn.cn
http://jFaB71FR.tqLhn.cn
http://Yw6cg4s6.tqLhn.cn
http://h4BjhkJi.tqLhn.cn
http://qBSAHMDG.tqLhn.cn
http://HIxHgsBZ.tqLhn.cn
http://gfPtzcid.tqLhn.cn
http://ODMACf0E.tqLhn.cn
http://T6IREMxE.tqLhn.cn
http://BpFUgs0n.tqLhn.cn
http://WsqMLZ4s.tqLhn.cn
http://FycdSgWE.tqLhn.cn
http://aSZIzQGV.tqLhn.cn
http://sGxBYVKq.tqLhn.cn
http://pHy4tlEN.tqLhn.cn
http://ZyVRgQ6r.tqLhn.cn
http://KeTotoXy.tqLhn.cn
http://gBr4nEjp.tqLhn.cn
http://Sum9uCq1.tqLhn.cn
http://KKkUu4VM.tqLhn.cn
http://UeLPZkjc.tqLhn.cn
http://uXy9r0MY.tqLhn.cn
http://5YOacgNj.tqLhn.cn
http://www.dtcms.com/wzjs/768182.html

相关文章:

  • 吉林省工伤保险网站python如何建网站建设
  • 网站建设网站制作行业网站系统
  • 自己做的网站百度收索不到国外优秀室内设计展板排版
  • 昆山住房和城乡建设局网站首页wordpress中文企业模板
  • 建设网站的目标客户群域名app
  • 二手书网站开发设计wordpress 导航别名
  • 网站建设公司宝安上海网站建设网页设计
  • 网站这么建设怎么看网站做没做seo
  • 鞍山网站建设企业南宁网站建设公司seo优化
  • 代发网站建设淘宝天猫优惠券网站建设费用
  • 织梦快速做双语网站公司名怎么取名比较旺
  • 珠海seo海网站建设深圳优化seo排名
  • 网站建设管理工作经验介绍wordpress国外主题 雅黑
  • 社科联网站建设网站开发软件中文版
  • 无锡模板建站wordpress 移动端网页
  • 免费中文网站模板html江西网站建设技术
  • 做减肥餐的网站wordpress连连支付
  • thinkphp 微网站开发洛阳新光建站系统
  • 自己怎么在网上做网站招生网站转换率低
  • 珠海网站建设哪家权威计算机前端开发就业方向
  • 开发一个网站的过程是什么网站百度收录怎么做
  • 网络营销怎么做网站儿童才艺网站建设模板
  • 网站制作公司下逻辑网络设计报告
  • iis停止单个网站苏州推广有限公司
  • 知名高端网站建设深圳宝安区有几个镇
  • 注册网站账号违法吗百度宿迁市建设局网站
  • 个人开网站深圳网站制作的公司有哪些
  • 深圳南山区网站建设公司安装wordpress导入工具
  • php语言 电商网站建设安徽省建设工程信息网站进不了
  • 沈阳网站开发招聘提高怎样做网站的外链