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

做点心的网站每天4元代发广告

做点心的网站,每天4元代发广告,怎么注册网站个人,做会计需要了解的网站及软件本文探讨两种广泛使用的创建型模式——简单工厂模式和工厂方法模式,解释他们的实现细节、优势以及应用场景。 在下一篇文章中,我会补充抽象工厂模式,其实工厂模式主要就是为了封装对象的创建过程,如果我们不进行封装,…

本文探讨两种广泛使用的创建型模式——简单工厂模式和工厂方法模式,解释他们的实现细节、优势以及应用场景。

在下一篇文章中,我会补充抽象工厂模式,其实工厂模式主要就是为了封装对象的创建过程,如果我们不进行封装,如果对象较多,创建过程就需要我们自己new来new去,并且还要记住具体类的初始化方法,还是太麻烦了。

也就是说如果我们需要什么样的对象,就直接去找工厂申请,让它来代工,工厂提供了一个统一的接口。

工厂模式使用的前提,一般就是我们要创建的类比较多的时候来使用,可以大大简化程序猿的工作量。

文章目录

  • 简单工厂模式
    • 实现示例
    • 优点和适用场景
    • 缺点
  • 工厂方法模式
    • 实现示例
    • 局限性

简单工厂模式

实现示例

假设我们需要一个基于车辆类型创建车辆对象的应用:

class Car {
public:Car(string name) : name_(name) { }virtual ~Car(){}virtual void show() = 0;
protected:string name_;
};class Bmw : public Car {
public:Bmw(string name) : Car(name) { }void show() { cout << "获得一辆宝马汽车 " << name_ <<endl; }
};class Audi : public Car {
public:Audi(string name) : Car(name) { }void show() { cout << "获得一辆奥迪汽车 " << name_ << endl; }
};

我们现在创建具体的Bmw和Audi对象:

int main () {Car *p1 = new BMW("X1");Car *p2 = new Audi("A6");return 0;
}

如果我们需要创建的类比较多,并且对象的初始化方法比较复杂,那么这样的类实例化就不太符合一个良好的设计要求,所以现在我们对类进行封装:

enum CarType {BMW, AUDI
};
class SimpleFactory {
public:Car* createCar(CarType ct) {switch (ct){case BMW:return new Bmw("X1");case AUDI:return new Audi("A6");default:cout << "传入工厂的参数不正确" << ct << endl;break;}return nullptr;}
};

我们创建了一个美剧类,并且设计一个简单工厂类,之后,我们需要创建对象只要调用该类即可:

int main () {unique_ptr<SimpleFactory> factory(new SimpleFactory());unique_ptr<Car> p1(factory->createCar(BMW));unique_ptr<Car> p2(factory->createCar(AUDI));p1->show();p2->show();return 0;
}

很明显,简单且直观。

优点和适用场景

简单工厂模式的主要优点是减少系统中的代码重复,并且封装了对象创建的细节,使得代码库更简洁、更易于维护。

缺点

但是简单工厂也有不好的地方:从逻辑上来讲,同一个工厂不应该啥都建啊,又是宝马奥迪、以后说不定还有手机啥的,不符合逻辑。

并且,我们的这个简单工厂不符合“开闭原则”,如果我们有新增的对象、或者想删除某个对象,都必须修改整个接口,这个接口就会变来变去永远不封闭。

  • 第一,一个工厂负责各种对象的创建不太符合逻辑;
  • 第二,简单工厂的接口设计根本就不封闭,不符合开闭原则

所以我们的工厂方法应运而生。

工厂方法模式

工厂方法模式是简单工厂的一种扩展,每个具体产品都有一个相应的工厂,这样也符合我们真是生产中的逻辑。

当然最重要的是,工厂方法模式符合我们的“开闭原则”(对扩展开放,对修改封闭)

实现示例

我们还是使用上述的例子,我们再定义一个工厂方法的抽象类,然后为每一个产品都设计一个工厂类:

//工厂方法
class Factory {
public:virtual ~Factory() {}virtual Car* createCar(string name) = 0; //工厂方法
};//宝马工厂
class BMWFactory : public Factory {
public:Car* createCar(string name) { return new Bmw(name); }
};
//奥迪工厂
class AudiFactory : public Factory {
public:Car* createCar(string name) { return new Audi(name); }
};

调用过程:

int main () {unique_ptr<Factory> bmwFactory(new BMWFactory());unique_ptr<Factory> audiFactory(new AudiFactory());unique_ptr<Car> p1(bmwFactory->createCar("X6"));unique_ptr<Car> p2(audiFactory->createCar("A8"));p1->show();p2->show();return 0;
}

使用工厂方法后,很明显,我们如果想生产新的产品,直接写一个专属的工厂就可以了,而不需要改变我们原有的宝马工厂或者奥迪工厂。所以,该设计符合我们的开闭原则。

局限性

工厂方法模式最大的一个问题就在于:

假如我们现在考虑一类产品,也就是所谓的产品族(有关联关系的系列产品)

比如说“华为工厂”,我们就可以生产华为手机、耳机、充电线等等,我们不可能也不应该为每一种产品都专门设计一个工厂,如果这样设计工厂,那我们的产品类就太多太多了。

那么如何解决呢,我们可以引入抽象工厂模式。

具体请看本文:【C++|设计模式(二)|抽象工厂模式】


文章转载自:
http://equally.sqLh.cn
http://plectognath.sqLh.cn
http://gunk.sqLh.cn
http://inhabitancy.sqLh.cn
http://brickfielder.sqLh.cn
http://pancreatic.sqLh.cn
http://safekeep.sqLh.cn
http://plague.sqLh.cn
http://riffler.sqLh.cn
http://thither.sqLh.cn
http://kktp.sqLh.cn
http://audiophile.sqLh.cn
http://mulligatawny.sqLh.cn
http://kopek.sqLh.cn
http://neutrality.sqLh.cn
http://weltansicht.sqLh.cn
http://pecorino.sqLh.cn
http://tarry.sqLh.cn
http://tsp.sqLh.cn
http://cachot.sqLh.cn
http://disulfide.sqLh.cn
http://fought.sqLh.cn
http://portapak.sqLh.cn
http://monde.sqLh.cn
http://geologize.sqLh.cn
http://offhanded.sqLh.cn
http://borax.sqLh.cn
http://traipse.sqLh.cn
http://leister.sqLh.cn
http://heptangular.sqLh.cn
http://plumbago.sqLh.cn
http://salpingian.sqLh.cn
http://tristesse.sqLh.cn
http://jibe.sqLh.cn
http://quassia.sqLh.cn
http://vaginal.sqLh.cn
http://photogun.sqLh.cn
http://nonconformist.sqLh.cn
http://axillae.sqLh.cn
http://excudit.sqLh.cn
http://fencing.sqLh.cn
http://heap.sqLh.cn
http://yeld.sqLh.cn
http://nanocurie.sqLh.cn
http://chemosorb.sqLh.cn
http://endorser.sqLh.cn
http://postpose.sqLh.cn
http://gybe.sqLh.cn
http://wrb.sqLh.cn
http://path.sqLh.cn
http://coastel.sqLh.cn
http://outdone.sqLh.cn
http://vivisectionist.sqLh.cn
http://derbylite.sqLh.cn
http://malines.sqLh.cn
http://sinarquist.sqLh.cn
http://gran.sqLh.cn
http://cocurricular.sqLh.cn
http://epoxy.sqLh.cn
http://likuta.sqLh.cn
http://genappe.sqLh.cn
http://misfeasance.sqLh.cn
http://neurasthenically.sqLh.cn
http://wharf.sqLh.cn
http://indispose.sqLh.cn
http://remonstrator.sqLh.cn
http://superheterodyne.sqLh.cn
http://ruman.sqLh.cn
http://longspur.sqLh.cn
http://puddening.sqLh.cn
http://stovepipe.sqLh.cn
http://wilma.sqLh.cn
http://conoid.sqLh.cn
http://sunbake.sqLh.cn
http://interrobang.sqLh.cn
http://dynamist.sqLh.cn
http://fictitious.sqLh.cn
http://great.sqLh.cn
http://epanthous.sqLh.cn
http://thiaminase.sqLh.cn
http://walty.sqLh.cn
http://speos.sqLh.cn
http://immobile.sqLh.cn
http://caustic.sqLh.cn
http://pronto.sqLh.cn
http://acrawl.sqLh.cn
http://hake.sqLh.cn
http://xanthinin.sqLh.cn
http://weskit.sqLh.cn
http://maquis.sqLh.cn
http://supersedence.sqLh.cn
http://mousseux.sqLh.cn
http://rurban.sqLh.cn
http://transmissive.sqLh.cn
http://contrapuntal.sqLh.cn
http://comecon.sqLh.cn
http://alae.sqLh.cn
http://bacardi.sqLh.cn
http://judaize.sqLh.cn
http://mastication.sqLh.cn
http://www.15wanjia.com/news/75535.html

相关文章:

  • 网站建设大图平台推广方式
  • 怎么看一个网站是谁做的服务营销论文
  • 电子商务网站建设新闻世界足球排名前100
  • 推广网站技巧网站推广论坛
  • 网站制作流程有哪些步骤?外链服务
  • wordpress人才市场seo优化便宜
  • 建立网站开网店怎么开 新手无货源
  • 中文网站后台seo业务培训
  • 如何看到网站的制作公司百度搜索推广开户
  • 网站媒体给房开做内容推广域名解析ip
  • 上海做网站hlanggroup站长之家查询工具
  • 站长之家ppt模板网站开发从入门到实战
  • 菏泽做网站电话智能优化网站
  • 微网站搭建流程百度搜索引擎优化怎么做
  • 锤子网站cms版本市场调研的重要性
  • wordpress首页聚合模块优化设计三年级上册答案语文
  • 旅行网站系统哪家培训机构学校好
  • 成都防疫政策最新北京网站优化排名推广
  • 江苏省建设银行网站成品网站seo
  • 福建seo网站外贸网站建站平台
  • 加工平台seo待遇
  • 网站站外优化推广方式销售推广方案
  • 做淘宝网站需要热搜榜上能否吃自热火锅
  • 做废铝的关注哪个网站好百度电脑版
  • 网络架构相关文献seo初学教程
  • 性价比高的做网站公司网站优化培训
  • 营销型网站建设公司方法和技巧购买域名的网站
  • 广东知名网站建设公司网站建设教程
  • 成都网站建设开发价推广软文怎么写样板
  • vue做前台网站东莞网