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

烟台官网首页无锡网站搜索引擎优化

烟台官网首页,无锡网站搜索引擎优化,中通物流企业网站建设书,昆明网站建设解决方案前言 创建型为了创建东西才是有用的#xff0c;创建型设计模式使用的场景#xff1a; 1、创建一个东西#xff1b; 2、可重复利用#xff1b; 3、灵活性高#xff0c;代码可因地制宜。 Factory Method(工厂模式) 工厂模式将目的将创建对象的具体过程屏蔽隔离起来#…前言 创建型为了创建东西才是有用的创建型设计模式使用的场景 1、创建一个东西 2、可重复利用 3、灵活性高代码可因地制宜。 Factory Method(工厂模式) 工厂模式将目的将创建对象的具体过程屏蔽隔离起来从而达到更高的灵活性工厂模式可以分为三类 简单工厂模式(Simple Factory)工厂方法模式(Factory Method)抽象工厂模式(Abstract Factory) 这三种模式从上到下逐步抽象并且更具一般性。《设计模式》一书中将工厂模式分为两类工厂方法模式与抽象工厂模式。将简单工厂模式看为工厂方法模式的一种特例两者归为一类。 Simple Factory(简单工厂) 主要用于创建对象。新添加类时不会影响以前的系统代码。核心思想是用一个工厂来根据输入的条件产生不同的类然后根据不同类的 virtual 函数得到不同的结果。 GOOD适用于不同情况创建不同的类时 BUG客户端必须要知道基类和工厂类耦合性差 simpleFactory.h #ifndef CLION_TEST_SIMPLEFACTORY_H #define CLION_TEST_SIMPLEFACTORY_H//基类 class COperation { public:int m_nFirst;int m_nSecond;virtual double GetResult() {double dResult 0;return dResult;} };//加法 class AddOperation : public COperation { public:double GetResult() final {return m_nFirst m_nSecond;} };//减法 class SubOperation : public COperation { public:double GetResult() final {return m_nFirst - m_nSecond;} };//工厂类 class CCalculatorFactory { public:static COperation *Create(char cOperator) {COperation *oper;switch (cOperator) {case :oper new AddOperation();break;case -:oper new SubOperation();break;default:oper new AddOperation();break;}return oper;} };#endif //CLION_TEST_SIMPLEFACTORY_Hmain.cpp #include iostream #include simpleFactory.husing namespace std;int main() {int a 1;int b 2;COperation * opCCalculatorFactory::Create(-);op-m_nFirsta;op-m_nSecondb;coutop-GetResult()endl;return 0; }Factory Method(工厂方法) GOOD修正了简单工厂模式中不遵守开放封闭原则。工厂方法模式把选择判断移到了客户端去实现如果想添加新功能就不用修改原来的类直接修改客户端即可。 一个产品对应一个工厂类。 factorymethod.h #ifndef CLION_TEST_FACTORYMETHOD_H #define CLION_TEST_FACTORYMETHOD_H#include iostream #include stringusing namespace std;// 实例基类相当于Product class LeiFeng { public:virtual void Sweep() {cout 雷锋扫地 endl;} };// 学雷锋的大学生相当于ConcreteProduct class Student : public LeiFeng { public:void Sweep() final {cout 大学生扫地 endl;} };// 学雷锋的志愿者相当于ConcreteProduct class Volenter : public LeiFeng { public:void Sweep() final {cout 志愿者 endl;} };// 工厂基类 Creator class LeiFengFactory { public:virtual LeiFeng *CreateLeiFeng() {return new LeiFeng();} };// 工厂具体类 class StudentFactory : public LeiFengFactory { public:LeiFeng *CreateLeiFeng() final {return new Student();} };class VolenterFactory : public LeiFengFactory { public:LeiFeng* CreateLeiFeng() final {return new Volenter();} };#endif //CLION_TEST_FACTORYMETHOD_Hmain.cpp #include factorymethod.husing namespace std;int main() {// 工厂方法LeiFengFactory *sf new StudentFactory();LeiFeng *s sf-CreateLeiFeng();s-Sweep();delete sf;delete s;sf nullptr;s nullptr;return 0; } Abstract Factory(抽象工厂) GOOD定义了一个创建一系列相关或相互依赖的接口而无需指定它们的具体类。 用于交换产品系列如 ACCESS­SQL SERVER 产品的具体类名被具体工厂的实现分离 abstractfactory.h #ifndef CLION_TEST_ABSTRACTFACTORY_H #define CLION_TEST_ABSTRACTFACTORY_H#include iostreamusing namespace std;// 用户抽象接口 class IUser { public:virtual void GetUser() 0;virtual void InsertUser() 0; };// 部门抽象接口 class IDepartment { public:virtual void GetDepartment() 0;virtual void InsertDepartment() 0; };// ACCESS用户 class CAccessUser : public IUser { public:void GetUser() final {cout Access GetUser endl;}void InsertUser() final {cout Access InsertUser endl;} };// ACCESS部门 class CAccessDepartment : public IDepartment { public:void GetDepartment() final {cout Access GetDepartment endl;}void InsertDepartment() final {cout Access InsertDepartment endl;} };// SQL用户 class CSqlUser : public IUser { public:void GetUser() final {cout Sql User endl;}void InsertUser() final {cout Sql User endl;} };// SQL部门类 class CSqlDepartment : public IDepartment { public:void GetDepartment() final {cout sql getDepartment endl;}void InsertDepartment() final {cout sql insertdepartment endl;} };// 抽象工厂 class IFactory { public:virtual IUser *CreateUser() 0;virtual IDepartment *CreateDepartment() 0; };// ACCESS工厂 class AccessFactory : public IFactory { public:IUser *CreateUser() final {return new CAccessUser();}IDepartment *CreateDepartment() final {return new CAccessDepartment();} };// SQL工厂 class SqlFactory : public IFactory { public:IUser *CreateUser() final {return new CSqlUser();}IDepartment *CreateDepartment() final {return new CSqlDepartment();} };#endif //CLION_TEST_ABSTRACTFACTORY_Hmain.cpp #include abstractfactory.husing namespace std;int main() {system(chcp 65001);// 抽象工厂模式///coutSQL用户endl;IFactory* factory new SqlFactory();IUser* user factory-CreateUser();IDepartment* department factory-CreateDepartment();user-GetUser();department-GetDepartment();///coutACCESS用户endl;factory new AccessFactory();user factory-CreateUser();department factory-CreateDepartment();user-GetUser();department-GetDepartment();return 0; } Builder(建造者) GOOD在当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时适用。 builder.h #ifndef CLION_TEST_BUILDER_H #define CLION_TEST_BUILDER_H#include string #include iostream #include vectorusing namespace std;// 最终的产品类 class Product { private:vectorstring m_product; public:void Add(string strtemp) {m_product.push_back(strtemp);}void Show() {for (auto p m_product.begin(); p ! m_product.end(); p) {cout *p endl;}} };// 建设者基类 class Builder { public:virtual void BuilderA() 0;virtual void BuilderB() 0;virtual Product *GetResult() 0; };// 第一种建造方式 class ConcreteBuilder1 : public Builder { private:Product *m_proudct; public:ConcreteBuilder1() {m_proudct new Product();}void BuilderA() final {m_proudct-Add(one);}void BuilderB() final {m_proudct-Add(two);}Product *GetResult() final {return m_proudct;} };// 第二种建造方式 class ConcreteBuilder2 : public Builder { private:Product *m_proudct; public:ConcreteBuilder2() {m_proudct new Product();}void BuilderA() final {m_proudct-Add(A);}void BuilderB() final {m_proudct-Add(B);}Product *GetResult() final {return m_proudct;} };// 指挥者类 class Direct { public:void Construct(Builder *temp) {temp-BuilderA();temp-BuilderB();} };#endif //CLION_TEST_BUILDER_Hmain.cpp #include builder.h using namespace std;int main() {system(chcp 65001);// 建造者模式Direct *p new Direct();Builder* b1 new ConcreteBuilder1();Builder* b2 new ConcreteBuilder2();p-Construct(b1); // 调用第一种方式Product* pb1 b1-GetResult();pb1-Show();p-Construct(b2); // 调用第二种方式Product* pb2 b2-GetResult();pb2-Show();return 0; }Prototype(原型) GOOD从一个对象再创建另外一个可定制的对象而无需知道任何创建的细节。并能提高创建的性能。 说白了就 COPY 技术把一个对象完整的 COPY 出一份。 注此处书写的是浅拷贝当需要复制的内容更改时不影响原先的内容需要进行深拷贝好奇地是原型模式直接使用拷贝赋值或拷贝构造函数不就可以了嘛。 prototype.h #ifndef CLION_TEST_PROTOTYPE_H #define CLION_TEST_PROTOTYPE_H#include iostream #include vector #include stringusing namespace std;// 抽象基类 class Prototype { private:string m_strName; public:Prototype(string strName) { m_strName strName; }Prototype() { m_strName ; }void Show() {cout m_strName endl;}virtual Prototype *Clone() 0; };// class ConcretePrototype1 class ConcretePrototype1 : public Prototype { public:ConcretePrototype1(string strName) : Prototype(strName) {}ConcretePrototype1() {}Prototype *Clone() final {ConcretePrototype1 *p new ConcretePrototype1();*p *this; // 复制对象return p;} };// class ConcretePrototype2 class ConcretePrototype2 : public Prototype { public:ConcretePrototype2(string strName) : Prototype(strName) {}ConcretePrototype2() {}Prototype *Clone() final {ConcretePrototype2 *p new ConcretePrototype2();*p *this; // 复制对象return p;} };#endif //CLION_TEST_PROTOTYPE_Hmain.cpp #include iostream #include prototype.husing namespace std;int main() {system(chcp 65001);// 客户端ConcretePrototype1* test1 new ConcretePrototype1(小王);ConcretePrototype2* test2 (ConcretePrototype2*)test1-Clone();test1-Show();test2-Show();return 0; }Singleton(单例) 后记
http://www.pierceye.com/news/200484/

相关文章:

  • 怎么给钓鱼网站做防红wordpress插件合集
  • 骆驼网站建设is_category wordpress
  • 网站中链接怎么做的怎么做资源网站
  • 石家庄建站模板搭建cdr做网站分辨率
  • 学校网站建设有限公司长春网站设计策划书
  • 大连网站建设流程图龙信建设集团网站
  • 徐州好点的做网站的公司深圳做商城网站建设
  • 上海龙象建设集团公司网站网站浮动咨询代码
  • 网站制作培训学校手机网站可以做动态吗
  • 企业推广网站网站开发页面怎么进
  • 嘉兴平湖网站建设网站的底部导航栏怎么做
  • 景安 怎么把网站做别名山东新华电脑学院学网站开发
  • 网站开发好还是app好wordpress 禁用修订
  • win7云主机怎么做网站贵州建设监理网站培训通知栏
  • 制作网站免费建站成都设计公司deanzhang
  • 10个网站用户体验优化的研究结果免费图片设计
  • 做明星网站打广告新闻20条摘抄大全
  • 佛山提供网站设计方案公司wordpress 2.0漏洞
  • wordpress建站教程视频教程百度推广登录首页
  • dede织梦php文章图片网站源码 完整后台 带在线音乐做企业网站进行推广要多少钱
  • 网站正在建设中手机版基于wordpress论文
  • 建设培训网站查询战网
  • 正能量网站下载做网站沧州
  • 网站维护需要什么技能wordpress博客评论删除
  • 行业网站设计师招聘广州番禺网站建设公司推荐
  • 正规网站模板设计软件工程学科评估
  • 网站集约化建设 要求惠州做棋牌网站建设哪家技术好
  • c#如何做公司网站做网站背景图怎么插
  • 国外做耳机贸易的平台网站定制网站
  • seo做的最好的十个网站加工订单网