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

免费微信小程序制作模板长沙seo代理

免费微信小程序制作模板,长沙seo代理,wordpress audaz主题,网络营销服务外包工厂模式 定义一个创建对象的接口#xff0c;让子类决定实例化哪个类#xff0c;而对象的创建统一交由工厂去生产。 工厂模式大致可以分为三类#xff1a;简单工厂模式、工厂方法模式、抽象工厂模式。 简单工厂模式 简单工厂模式提供一个工厂类#xff0c;根据传入的参…工厂模式 定义一个创建对象的接口让子类决定实例化哪个类而对象的创建统一交由工厂去生产。 工厂模式大致可以分为三类简单工厂模式、工厂方法模式、抽象工厂模式。 简单工厂模式 简单工厂模式提供一个工厂类根据传入的参数来创建不同类型的对象而客户端代码无需了解对象的创建过程。 #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.hkea.cn/news/14541935/

相关文章:

  • 企业展示网站案例wordpress主题模块添加图片
  • 网站建设参考的文献企业网站效果图
  • 在线支付网站建设青海省安建设管理部门网站
  • 怎么建网站教程视频app网页设计作品
  • wordpress 4.4 火车头seo外包大型公司
  • 一个网站的入口网页又称为设计网名昵称
  • 做网站后期续费是怎么算的项目网格化管理
  • 上海嘉定网站建设网站分享设计
  • seo站长常用工具网络营销论文文献
  • 静态网站管理系统办公用纸网站建设
  • 网站如何导流量怎么查网站有没有做301
  • 建设网站是做手机版好还是pc版好河北省电力建设第一工程公司网站
  • 自助建站什么意思ae做模板下载网站
  • 免费入驻的外贸网站亚马逊中国网站建设目标
  • 手机网站开发 html广州番禺最新发布
  • 电子商务网站建设规划方案论文西安网站建设资讯
  • 网站建站视频教程wordpress 最近访客
  • 国外网站建设现状萍乡的电子商务网站建设公司
  • flash网站案例wordpress自定义搜索文件
  • 软文网站大全网站建设方案.doc
  • 佛山网站建设首选抓取wordpress站点用户
  • 怎么做二维码直接进入网站WordPress怎么加入用户关注
  • 建设网站的发布与推广有关做网站项目的毕业答辩
  • 泰安企业建站公司流程有什么做vi设计的网站
  • 网站视频插件代码棋牌app开发多少钱
  • 怎么制作个人网站dede网站正在维护中应该怎样设置
  • 黄埔区建设局网站简述jsp网站架构
  • 浏览器如何推广自己网站网站设计 联系
  • 佛山新网站建设详细内容购物网站的详细设计
  • 响应式网站做seo怎么样photoshop网站模板