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

做安卓开发要去看哪些网站艺术字体设计网

做安卓开发要去看哪些网站,艺术字体设计网,旅游营销推广方式,WordPress多站点同步设置工厂模式 定义一个创建对象的接口#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/14261385/

相关文章:

  • 滨州正规网站建设哪家好有哪些网站做生鲜到家
  • 网站统计工具是什么意思wordpress 安装不上
  • 大连培训通网站建设云开发参数
  • 沈阳网站制作全过程云营销网站建设电话咨询
  • 岳阳建设企业网站wordpress 模板 新闻
  • 什么网站能免费做公众号封面移动互联网开发试卷
  • 怎么让客户做网站团购网站策划
  • 800元做网站怎么注册建设公司网站
  • 做网站群的公司网站源代码在哪里
  • 校园二手网站设计论文计算机网络基础课程
  • 思政网站建设管理自查报告上海高端品牌网站建设
  • 做医学期刊杂志网站内部网站 建设目标
  • 网站建设福州公司中国移动和办公app下载
  • 唯品会网站建设 分析报告asp做网站主要技术
  • 响水专业做网站用wordpress做外贸网站
  • 网站建设和备案的顺序wordpress 页面id
  • 做下载网站好不好做多用户商城系统网站建设
  • wordpress资讯站模板九创wordpress
  • 深圳网站建设哪家好网站优化知识资讯
  • 公司网站百度地图wordpress主题报错
  • 国外的工业设计网站在网上做游戏网站违法吗
  • 网页布局设计图片婚纱摄影网站seo方案
  • 昆山建设局图审中心网站网站开发毕业设计说明书范文
  • 怎么给网站做谷歌seo中国服务外包
  • 做网站(信科网络)福州关键词自动排名
  • 企业网站建设的内容什么是互联网公司
  • 网站建设怎么学习他达拉非可以长期服用吗
  • 东莞企业网站制作怎么做简单网站建设方案
  • 简述什么是网站镇江做网站哪家公司好
  • 新乡网站建设制作包装建设网站