西安将军山网站建设,天津公司网站怎样制作,移动ui设计 网站,wordpress文章编辑代码与工厂模式对比
工厂模式
工厂模式是类创建模式。在工厂模式中#xff0c;只需要生产同一种产品#xff0c;只不过是生产厂家不同。
所以产品类的设计#xff1a;
抽象的产品类Product具体的产品类Product_A#xff0c;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 vectorusing 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_ptrBook make_book() 0;virtual unique_ptrPencil make_pencil() 0;virtual unique_ptrRuler make_ruler() 0;
};class AiHaoFactory : public Factory {
public:unique_ptrBook make_book() override;unique_ptrPencil make_pencil() override;unique_ptrRuler make_ruler() override;
};class ChenGuangFactory : public Factory {
public:unique_ptrBook make_book() override;unique_ptrPencil make_pencil() override;unique_ptrRuler 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_ptrBook
AiHaoFactory::make_book()
{return make_uniqueAiHaoBook();
}unique_ptrPencil
AiHaoFactory::make_pencil()
{return make_uniqueAiHaoPencil();
}unique_ptrRuler
AiHaoFactory::make_ruler()
{return make_uniqueAiHaoRuler();
}unique_ptrBook
ChenGuangFactory::make_book()
{return make_uniqueChenGuangBook();
}unique_ptrPencil
ChenGuangFactory::make_pencil()
{return make_uniqueChenGuangPencil();
}unique_ptrRuler
ChenGuangFactory::make_ruler()
{return make_uniqueChenGuangRuler();
}int
main (void)
{vectorunique_ptrFactory makers;makers.emplace_back (make_uniqueAiHaoFactory());makers.emplace_back (make_uniqueChenGuangFactory());for (auto maker : makers) {unique_ptrBook book maker-make_book();unique_ptrPencil pencil maker-make_pencil();unique_ptrRuler 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_ptrBookmake_pencil() : unique_ptrPencilmake_ruler() : unique_ptrRuler
}class AiHaoPencil {get_brand() : string---brand : static const string
}class AiHaoRuler {-brand : static const stringget_brand() : string
}abstract class Book {{abstract}~Book()virtual get_brand() : stringget_type() : string---type : static const string
}class ChenGuangBook {get_brand() : string---brand : static const string
}class ChenGuangFactory {make_book() : unique_ptrBookmake_pencil() : unique_ptrPencilmake_ruler() : unique_ptrRuler
}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_ptrBookvirtual make_pencil() : unique_ptrPencilvirtual make_ruler() : unique_ptrRuler
}abstract class Pencil {{abstract}~Pencil()virtual get_brand() : stringget_type() : string---type : static const string
}abstract class Ruler {{abstract}~Ruler()virtual get_brand() : stringget_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.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.jjnry.cn.gov.cn.jjnry.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.hrzymy.com.gov.cn.hrzymy.com http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.bttph.cn.gov.cn.bttph.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.qncqd.cn.gov.cn.qncqd.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.zlces.com.gov.cn.zlces.com