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

上海做网站搜索一下马来西亚的网络营销专业好就业吗

上海做网站搜索一下马来西亚的,网络营销专业好就业吗,wordpress移除自豪的使用,电脑网页制作模板工厂模式 定义一个创建对象的接口,让子类决定实例化哪个类,而对象的创建统一交由工厂去生产。 工厂模式大致可以分为三类:简单工厂模式、工厂方法模式、抽象工厂模式。 简单工厂模式 简单工厂模式提供一个工厂类,根据传入的参…

工厂模式

定义一个创建对象的接口,让子类决定实例化哪个类,而对象的创建统一交由工厂去生产。

工厂模式大致可以分为三类:简单工厂模式、工厂方法模式、抽象工厂模式

简单工厂模式

简单工厂模式提供一个工厂类,根据传入的参数来创建不同类型的对象,而客户端代码无需了解对象的创建过程。

#include <iostream>// 基类,所有图形对象的父类
class Shape {
public:virtual void draw() = 0;
};// 具体的圆形类
class Circle : public Shape {
public:void draw() {std::cout << "Draw a circle" << std::endl;}
};// 具体的矩形类
class Rectangle : public Shape {
public:void draw() {std::cout << "Draw a rectangle" << std::endl;}
};// 简单工厂类
class ShapeFactory {
public:// 根据传入的参数创建不同类型的图形对象Shape* createShape(const std::string& shapeType) {if (shapeType == "circle") {return new Circle();} else if (shapeType == "rectangle") {return new Rectangle();} else {return nullptr; // 可以添加错误处理逻辑}}
};int main() {ShapeFactory factory;// 创建圆形对象Shape* circle = factory.createShape("circle");if (circle) {circle->draw();delete circle;}return 0;
}

缺点:增加新类型时需要修改工厂类,违反开闭原则。

工厂方法模式

工厂方法模式定义了一个创建对象的接口,但将具体对象的创建交给子类来实现。

#include <iostream>
#include <string>// 抽象产品类:文档
class Document {
public:virtual void open() = 0;virtual void save() = 0;
};// 具体产品类:PDF 文档
class PDFDocument : public Document {
public:void open() {std::cout << "Opening a PDF document" << std::endl;}void save() {std::cout << "Saving a PDF document" << std::endl;}
};// 具体产品类:文本文档
class TextDocument : public Document {
public:void open() {std::cout << "Opening a Text document" << std::endl;}void save() {std::cout << "Saving a Text document" << std::endl;}
};// 抽象工厂类
class DocumentFactory {
public:virtual Document* createDocument() = 0;
};// 具体工厂类:PDF 文档工厂
class PDFDocumentFactory : public DocumentFactory {
public:Document* createDocument() {return new PDFDocument();}
};// 具体工厂类:文本文档工厂
class TextDocumentFactory : public DocumentFactory {
public:Document* createDocument() {return new TextDocument();}
};int main() {DocumentFactory* factory = nullptr;Document* doc = nullptr;// 创建 PDF 文档factory = new PDFDocumentFactory();doc = factory->createDocument();doc->open();doc->save();delete doc;delete factory;// 创建文本文档factory = new TextDocumentFactory();doc = factory->createDocument();doc->open();doc->save();delete doc;delete factory;return 0;
}

优点: 扩展性好,符合了开闭原则, 新增一种产品时,只需增加改对应的产品类和对应的工厂子类即可。

缺点:每增加一种新类型,就需要增加一个对象的工厂; 相比简单工厂模式,工厂方法模式需要更多的类定义。

抽象工厂模式

抽象工厂模式允许你创建一系列相关的对象,而不指定具体的类。

#include <iostream>
#include <string>// 抽象产品类:按钮
class Button {
public:virtual void render() = 0;
};// 具体产品类:Windows按钮
class WindowsButton : public Button {
public:void render() {std::cout << "Rendering a Windows button" << std::endl;}
};// 具体产品类:Linux按钮
class LinuxButton : public Button {
public:void render() {std::cout << "Rendering a Linux button" << std::endl;}
};// 抽象产品类:窗口
class Window {
public:virtual void createButton() = 0;
};// 具体产品类:Windows窗口
class WindowsWindow : public Window {
public:void createButton() {Button* button = new WindowsButton();button->render();}
};// 具体产品类:Linux窗口
class LinuxWindow : public Window {
public:void createButton() {Button* button = new LinuxButton();button->render();}
};// 抽象工厂类
class GUIFactory {
public:virtual Window* createWindow() = 0;
};// 具体工厂类:Windows工厂
class WindowsGUIFactory : public GUIFactory {
public:Window* createWindow() {return new WindowsWindow();}
};// 具体工厂类:Linux工厂
class LinuxGUIFactory : public GUIFactory {
public:Window* createWindow() {return new LinuxWindow();}
};int main() {GUIFactory* factory = nullptr;Window* window = nullptr;// 创建Windows风格的窗口和按钮factory = new WindowsGUIFactory();window = factory->createWindow();delete factory;// 创建Linux风格的窗口和按钮factory = new LinuxGUIFactory();window = factory->createWindow();delete factory;return 0;
}

优点: 工厂抽象类创建了多个类型的产品,当有需求时,可以创建相关产品子类和子工厂类来获取。

缺点: 需要在抽象工厂类中提前确定可能需要的产品种类,以满足不同型号的多种产品的需求;如果我们需要的产品种类并没有在抽象工厂类中提前确定,则需要修改抽象工厂类了,以及所有的工厂子类。

http://www.15wanjia.com/news/9496.html

相关文章:

  • ASP做旅游网站代码三只松鼠网络营销策略
  • 网站建设免备案免费空间南京seo网站管理
  • 温州网站建设温州网站制作一键优化大师下载
  • 什么是品牌vi设计江门seo外包公司
  • centos7系统做网站网站备案
  • 个人做二次元网站怎么赚钱如何做推广
  • 学校诗歌网站建设推广费用一般多少钱
  • 外贸网站论文seo技术网网
  • 苏州网站建设品牌韶山百度seo
  • 网站建设中跳转页面源码竞价是什么意思
  • 深圳网站建设公司pestl分析软件培训机构有哪些?哪个比较好
  • 房地产网站素材重庆官网seo分析
  • 中山做展示型网站seo网站推广的主要目的
  • 建站时候源码有验证怎么办郑州seo推广优化
  • 网站设计公司市场容量网站建设培训机构
  • phpcmsv9手机网站源码最新的销售平台
  • 站长之家ping检测免费网页制作平台
  • 织梦网站怎么居中企业培训平台
  • 网站效果图怎么做的营销网站推荐
  • 免费做网站软件下载企业网站推广方法
  • 网站的风格设计佛山抖音seo
  • 国外一家做乳胶衣视频的网站百度平台营销软件
  • 网站建设企业排名柳市网站制作
  • 做网站需要字体授权广告网址
  • c做项目的网站广告策划
  • 网站三要素关键词 描述怎么做app推广引流方法
  • 做游戏模板下载网站有哪些人力资源和社会保障部
  • 男女情感类网站十大骗子教育培训机构
  • 网站建设灵寿免费网站流量统计
  • 网站互动功能微信管理系统登录入口