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

浙江建设干部学校网站电脑版百度入口

浙江建设干部学校网站,电脑版百度入口,强的网站建设,品牌网站建设报价与工厂模式对比 工厂模式 工厂模式是类创建模式。在工厂模式中,只需要生产同一种产品,只不过是生产厂家不同。 所以产品类的设计: 抽象的产品类Product具体的产品类Product_A,Product_B, Product_C, Product_D…… 工厂的设计…

与工厂模式对比

工厂模式

工厂模式是类创建模式。在工厂模式中,只需要生产同一种产品,只不过是生产厂家不同。

所以产品类的设计:

  • 抽象的产品类Product
  • 具体的产品类Product_AProduct_B, Product_C, Product_D……

工厂的设计:

  • 抽象的工厂Factory
  • 与产品对应的Factory_A, Factory_B, Factory_C, Factory_D……

特点是每增加一个厂家,就会成对地增加类。
不考虑增加产品种类,否则就会升级为抽象工厂模式。

抽象工厂模式

抽象工厂模式是对象创建模式。抽象工厂模式中,每个厂家都生产多种产品

反映到我们的工厂类,就是需要提供更多的产品制造接口。

优点

每增加一个厂家,就要有一个具体的工厂类产生、多个具体的产品类产生。
只需让具体的工厂继承Factory抽象的工厂、具体的产品继承抽象的产品、具体的工厂负责制造具体的产品。对其他类没有影响。

缺点/限制

当增加一种新的产品时(不推荐),每个已有的工厂都需要增加新的方法,来制造对应的产品,违背了开闭原则。

产品已有的种类是固定的,而品阶/等级/厂商可以变。

有时这也算是个优点,因为将每个工厂的产品视为一套产品,这很符合一些应用场合。
例如对不同的操作系统提供一套UI组件,对不同操作系统使用不同的具体工厂来产生一套组件。

又比如在数据相关类的设计中,有不同的数据库,不同的数据库会给出不同的连接类、语句类。数据库就像具体的工厂,连接类、语句类则是产品。不同的数据库自成一套。

类图

下面的例子模拟不同的文具工厂生产各种文具,
假设厂家有:爱好,晨光……
假设文具有:书、铅笔、尺子……

在这里插入图片描述

代码

#include <iostream>
#include <memory>
#include <vector>using namespace std;class Book;
class AiHaoBook;
class ChenGuangBook;class Pencil;
class AiHaoPencil;
class ChenGuangPencil;class Ruler;
class AiHaoRuler;
class ChenGuangRuler;class Factory;
class ChenGuangFactory;
class AiHaoFactory;class Factory {
public:virtual ~Factory() = default;virtual unique_ptr<Book> make_book() = 0;virtual unique_ptr<Pencil> make_pencil() = 0;virtual unique_ptr<Ruler> make_ruler() = 0;
};class AiHaoFactory : public Factory {
public:unique_ptr<Book> make_book() override;unique_ptr<Pencil> make_pencil() override;unique_ptr<Ruler> make_ruler() override;
};class ChenGuangFactory : public Factory {
public:unique_ptr<Book> make_book() override;unique_ptr<Pencil> make_pencil() override;unique_ptr<Ruler> make_ruler() override;
};class Book {
public:virtual ~Book() = default;virtual string get_brand() const = 0;string get_type() const;
private:static const string type;
};class Pencil {
public:virtual ~Pencil() = default;virtual string get_brand() const = 0;string get_type() const;
private:static const string type;
};class Ruler {
public:virtual ~Ruler() = default;virtual string get_brand() const = 0;string get_type() const;
private:static const string type;
};const string Book::type ("Book");
const string Pencil::type ("Pencil");
const string Ruler::type ("Ruler");string
Book::get_type() const
{return Book::type;
}string
Pencil::get_type() const
{return Pencil::type;
}string
Ruler::get_type() const
{return Ruler::type;
}class AiHaoBook: public Book {
public:string get_brand() const override;
private:static const string brand;
};class AiHaoPencil : public Pencil {
public:string get_brand() const override;
private:static const string brand;
};class AiHaoRuler : public Ruler {
public:string get_brand() const override;
private:static const string brand;
};class ChenGuangBook: public Book {
public:string get_brand() const override;
private:static const string brand;
};class ChenGuangPencil : public Pencil {
public:string get_brand() const override;
private:static const string brand;
};class ChenGuangRuler : public Ruler {
public:string get_brand() const override;
private:static const string brand;
};const string AiHaoBook::brand ("AiHao");
const string AiHaoPencil::brand ("AiHao");
const string AiHaoRuler::brand ("AiHao");const string ChenGuangBook::brand ("ChenGuang");
const string ChenGuangPencil::brand ("ChenGuang");
const string ChenGuangRuler::brand ("ChenGuang");
string
AiHaoBook::get_brand() const
{return AiHaoBook::brand;
}string
AiHaoPencil::get_brand() const
{return AiHaoPencil::brand;
}string
AiHaoRuler::get_brand() const
{return AiHaoRuler::brand;
}string
ChenGuangBook::get_brand() const
{return ChenGuangBook::brand;
}string
ChenGuangPencil::get_brand() const
{return ChenGuangPencil::brand;
}string
ChenGuangRuler::get_brand() const
{return ChenGuangRuler::brand;
}unique_ptr<Book>
AiHaoFactory::make_book()
{return make_unique<AiHaoBook>();
}unique_ptr<Pencil>
AiHaoFactory::make_pencil()
{return make_unique<AiHaoPencil>();
}unique_ptr<Ruler>
AiHaoFactory::make_ruler()
{return make_unique<AiHaoRuler>();
}unique_ptr<Book>
ChenGuangFactory::make_book()
{return make_unique<ChenGuangBook>();
}unique_ptr<Pencil>
ChenGuangFactory::make_pencil()
{return make_unique<ChenGuangPencil>();
}unique_ptr<Ruler>
ChenGuangFactory::make_ruler()
{return make_unique<ChenGuangRuler>();
}int
main (void)
{vector<unique_ptr<Factory>> makers;makers.emplace_back (make_unique<AiHaoFactory>());makers.emplace_back (make_unique<ChenGuangFactory>());for (auto &maker : makers) {unique_ptr<Book> book = maker->make_book();unique_ptr<Pencil> pencil = maker->make_pencil();unique_ptr<Ruler> ruler = maker->make_ruler();cout << book->get_brand() << " " << book->get_type() << endl;cout << pencil->get_brand() << " " << pencil->get_type() << endl;cout << ruler->get_brand() << " " << ruler->get_type() << endl;}
}

plantuml

@startuml/' Objects '/class AiHaoBook {+get_brand() : string---brand : static const string
}class AiHaoFactory {+make_book() : unique_ptr<Book>+make_pencil() : unique_ptr<Pencil>+make_ruler() : unique_ptr<Ruler>
}class AiHaoPencil {+get_brand() : string---brand : static const string
}class AiHaoRuler {-brand : static const string+get_brand() : string
}abstract class Book {+{abstract}~Book()+virtual get_brand() : string+get_type() : string---type : static const string
}class ChenGuangBook {+get_brand() : string---brand : static const string
}class ChenGuangFactory {+make_book() : unique_ptr<Book>+make_pencil() : unique_ptr<Pencil>+make_ruler() : unique_ptr<Ruler>
}class ChenGuangPencil {+get_brand() : string---brand : static const string
}class ChenGuangRuler {+get_brand() : string---brand : static const string
}abstract class Factory {+{abstract}~Factory()+virtual make_book() : unique_ptr<Book>+virtual make_pencil() : unique_ptr<Pencil>+virtual make_ruler() : unique_ptr<Ruler>
}abstract class Pencil {+{abstract}~Pencil()+virtual get_brand() : string+get_type() : string---type : static const string
}abstract class Ruler {+{abstract}~Ruler()+virtual get_brand() : string+get_type() : string---type : static const string
}/' Inheritance relationships '/Book <|----- AiHaoBookBook <|----- ChenGuangBookFactory <|-- AiHaoFactoryFactory <|-- ChenGuangFactoryPencil <|----- AiHaoPencilPencil <|----- ChenGuangPencilRuler <|----- AiHaoRulerRuler <|----- ChenGuangRuler.ChenGuangFactory .....> .ChenGuangBook.ChenGuangFactory .....> .ChenGuangPencil.ChenGuangFactory .....> .ChenGuangRuler.AiHaoFactory .....> .AiHaoBook
.AiHaoFactory .....> .AiHaoPencil
.AiHaoFactory .....> .AiHaoRuler/' Aggregation relationships '//' Nested objects '/@enduml
http://www.ds6.com.cn/news/84652.html

相关文章:

  • 自己做的网站怎么才能在百度上查找国外网站加速
  • 网站推广公司渠道哈尔滨seo关键词排名
  • 建设工程造价管理协会网站网站服务器多少钱一年
  • 如何建设微商网站长沙专业竞价优化首选
  • 百度搜索的优势网站优化平台
  • 大型商城网站建设方案北京网站优化排名
  • PHP网站开发有哪些框架制作网站用什么软件
  • 以下哪一项不属于seo对网站推广的作用百度搜索引擎算法
  • 网站系统开发流程网站网络推广推广
  • 酒店网站建设功能搜索大全引擎
  • 笔记网站开发代码win10优化大师有用吗
  • 深圳十大装修公司品牌排行榜seo怎样
  • 网站怎么做图片动态google搜索关键词热度
  • 网站目录结构图网站推广软件排名
  • wordpress 上传主机陕西seo顾问服务
  • vue php 哪个做网站 好手机百度快照
  • 境外网站icp备案惠州seo怎么做
  • 企业网站制作开发百度图片识别
  • 做的网站名网络营销推广外包服务
  • 一级消防工程师考试内容南京seo外包
  • 哪些网站做翻译可以赚钱semi final
  • 品牌手机网站建设长沙优化排名
  • wordpress怎样做单页网站在线html5制作网站
  • 如何用txt做网站时增加照片产品营销策略有哪些
  • wordpress 过滤seo营销
  • steam课程做网站2024年将爆发新瘟疫
  • 在菲律宾做网络网站犯法吗百度云网盘资源搜索引擎入口
  • 英文网站源码下载长沙关键词优化新行情报价
  • 网站建设友汇网页设计制作网站模板图片
  • 网站建设委托开发合同范本青岛seo百科