-
單例模式
全局定義生成一個(gè)對(duì)象蜒滩,聲明周期跟整個(gè)程序一樣
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton* getInstance()
{
return instance;
}
private:
//不讓類的外部再創(chuàng)建實(shí)例
Singleton() {
}
static Singleton* instance;//指向本類唯一實(shí)例的指針混卵。
};
/*
餓漢式 - 在編譯期間就已經(jīng)確定這個(gè)唯一的實(shí)例了母廷。
*/
Singleton* Singleton::instance = new Singleton; //類的內(nèi)部
class Singleton2
{
public:
static Singleton2* getInstance()
{
//加鎖
if (instance == NULL) {
instance = new Singleton2;
}
//關(guān)鎖
return instance;
}
private:
Singleton2() {
}
static Singleton2 * instance;
};
//懶漢式的初始化方式
Singleton2 *Singleton2::instance = NULL;
int main(void)
{
Singleton * s1 = Singleton::getInstance();
Singleton *s2 = Singleton::getInstance();
if (s1 == s2) {
cout << "s1 == s2" << endl;
}
else {
cout << "s1 != s2 " << endl;
}
Singleton2 *s3 = Singleton2::getInstance();
Singleton2 *s4 = Singleton2::getInstance();
if (s3 == s4) {
cout << "s3 == s4" << endl;
}
else {
cout << "s3 != s4" << endl;
}
return 0;
}
-
工廠方法模式
(指定的工廠生成指定的類)缺點(diǎn)就是需要生成多個(gè)工廠
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//抽象的水果類
class Fruit
{
public:
virtual void getName() = 0;
};
class Apple :public Fruit {
public:
virtual void getName() {
cout << "我是蘋果" << endl;
}
};
class Banana :public Fruit {
public:
virtual void getName() {
cout << "我是香蕉 " << endl;
}
};
//添加一個(gè)梨產(chǎn)品
class Pear : public Fruit
{
public:
virtual void getName() {
cout << "我是鴨梨 " << endl;
}
};
//抽象的工廠類
class AbstractFactory
{
public:
virtual Fruit * createFruit() = 0;//抽象的水果生產(chǎn)器
};
//蘋果的工廠
class AppleFactory :public AbstractFactory
{
public:
virtual Fruit * createFruit() {
return new Apple;
}
};
//香蕉工廠
class BananaFactory : public AbstractFactory {
public:
virtual Fruit *createFruit() {
return new Banana;
}
};
//添加梨的工廠
class PearFactory :public AbstractFactory
{
public:
virtual Fruit *createFruit() {
//....
return new Pear;
}
};
int main(void)
{
//1 給我來(lái)一個(gè)香蕉的工廠
AbstractFactory * bananaFactory = new BananaFactory;
//2 給我來(lái)一個(gè)水果
Fruit * banana = bananaFactory->createFruit(); //只能夠生成香蕉 //多態(tài)
//Fruit *banana = new Banana;
banana->getName(); //多態(tài)
delete bananaFactory;
delete banana;
AbstractFactory *appleFactory = new AppleFactory;
Fruit *apple = appleFactory->createFruit();
apple->getName();
AbstractFactory *pearFactory = new PearFactory;
Fruit *pear = pearFactory->createFruit();
//Fruit *pear = new Pear;
pear->getName(); //多態(tài)
return 0;
}
-
抽象工廠模式(將產(chǎn)品歸屬一類治泥,一個(gè)工廠生成一類產(chǎn)品藐窄,無(wú)需多個(gè)產(chǎn)品多個(gè)工廠)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
// 0--------- 抽象層 ------
class CPU
{
public:
virtual void caculate() = 0;
};
class Card
{
public:
virtual void display() = 0;
};
class Memory
{
public:
virtual void storage() = 0;
};
class AbstractFactory
{
public:
virtual CPU* createCPU() = 0;
virtual Card* createCard() = 0;
virtual Memory * createMem() = 0;
};
//架構(gòu)類
class Computer
{
public:
Computer(CPU *cpu, Card *card, Memory *mem)
{
this->cpu = cpu;
this->card = card;
this->mem = mem;
}
void work() {
this->cpu->caculate();
this->card->display();
this->mem->storage();
}
private:
CPU *cpu;
Card*card;
Memory *mem;
};
//抽象層結(jié)束------
// ----- 實(shí)現(xiàn)層-----
//intel廠商
class IntelCPU :public CPU
{
public:
virtual void caculate() {
cout << "Intel CPU 開(kāi)始計(jì)算了..." << endl;
}
};
class IntelCard :public Card
{
public:
virtual void display() {
cout << "Intel card 開(kāi)始顯示 了..." << endl;
}
};
class IntelMem :public Memory
{
public:
virtual void storage() {
cout << "Intel mem 開(kāi)始存儲(chǔ)了..." << endl;
}
};
class IntelFactory :public AbstractFactory
{
public:
virtual CPU* createCPU() {
return new IntelCPU;
}
virtual Card* createCard() {
return new IntelCard;
}
virtual Memory * createMem() {
return new IntelMem;
}
};
// NVIDIA的廠商
class NvidiaCPU :public CPU
{
public:
virtual void caculate() {
cout << "Nvidia CPU 開(kāi)始計(jì)算了..." << endl;
}
};
class NvidiaCard :public Card
{
public:
virtual void display() {
cout << "Nvidia card 開(kāi)始顯示 了..." << endl;
}
};
class NvidiaMem :public Memory
{
public:
virtual void storage() {
cout << "Nvidia mem 開(kāi)始存儲(chǔ)了..." << endl;
}
};
class NvidiaFacory :public AbstractFactory
{
public:
virtual CPU* createCPU() {
return new NvidiaCPU;
}
virtual Card* createCard() {
return new NvidiaCard;
}
virtual Memory * createMem() {
return new NvidiaMem;
}
};
// Kinston 廠商
class KinstonCPU :public CPU
{
public:
virtual void caculate() {
cout << "Kinston CPU 開(kāi)始計(jì)算了..." << endl;
}
};
class KinstonCard :public Card
{
public:
virtual void display() {
cout << "Kinston card 開(kāi)始顯示 了..." << endl;
}
};
class KinstonMem :public Memory
{
public:
virtual void storage() {
cout << "Kinston mem 開(kāi)始存儲(chǔ)了..." << endl;
}
};
class KinstonFacory :public AbstractFactory
{
public:
virtual CPU* createCPU() {
return new KinstonCPU;
}
virtual Card* createCard() {
return new KinstonCard;
}
virtual Memory * createMem() {
return new KinstonMem;
}
};
int main(void)
{
//1 組裝一個(gè)intel系列的電腦 對(duì)intel公司的所有產(chǎn)品,執(zhí)行一個(gè)單元測(cè)試楞慈。
//創(chuàng)建一個(gè)intel的工廠
AbstractFactory * intelFactory = new IntelFactory;
CPU *intelCPU = intelFactory->createCPU();
Card *intelCard = intelFactory->createCard();
Memory *intelMem = intelFactory->createMem();
Computer *com1 = new Computer(intelCPU, intelCard, intelMem);
com1->work();
cout << " ----- " << endl;
//2 組裝一個(gè) intel的cpu kinston card NVIDIA的顯卡的電腦
AbstractFactory *kinstonFactory = new KinstonFacory;
Memory *kingstonMem = kinstonFactory->createMem();
AbstractFactory *nvidiaFactory = new NvidiaFacory;
Card *nvidiaCard = nvidiaFactory->createCard();
Computer *com2 = new Computer(intelCPU, nvidiaCard, kingstonMem);
com2->work();
return 0;
}
-
簡(jiǎn)單工廠模式(一個(gè)工廠可以生成多個(gè)產(chǎn)品幔烛,每次新增產(chǎn)品都需要去修改工廠類,不符合軟件設(shè)計(jì)規(guī)范)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Fruit
{
public:
Fruit(string kind)
{
this->kind = kind;
if (kind == "apple") {
//代表蘋果
//蘋果的初始化方式
}
else if (kind == "banana") {
//代表香蕉
//香蕉的初始化方式
}
}
void getName() {
if (this->kind == "apple") {
cout << "我是蘋果" << endl;
}
else if (this->kind == "banana"){
cout << "我是香蕉" << endl;
}
}
private:
string kind;//代表水果的種類
};
int main(void)
{
//創(chuàng)建一個(gè)蘋果
Fruit *apple = new Fruit("apple");
apple->getName();
delete apple;
//main函數(shù)跟Fruit類的構(gòu)造函數(shù)耦合度高囊蓝, 隨著水果種類的增加 構(gòu)造函數(shù)越來(lái)越復(fù)雜
return 0;
}
-
建造者模式
1.0概念
(將一個(gè)復(fù)雜的對(duì)象的構(gòu)造和表示分離使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示饿悬。創(chuàng)建者模式隱藏了復(fù)雜對(duì)象的創(chuàng)建過(guò)程,它把復(fù)雜對(duì)象的創(chuàng)建過(guò)程加以抽象聚霜,通過(guò)子類繼承或者重載的方式狡恬,動(dòng)態(tài)的創(chuàng)建具有復(fù)合屬性的對(duì)象)
1.1作用:
指揮者(Director)直接和客戶(Client)進(jìn)行需求溝通;
溝通后指揮者將客戶創(chuàng)建產(chǎn)品的需求劃分為各個(gè)部件的建造請(qǐng)求(Builder)蝎宇;
將各個(gè)部件的建造請(qǐng)求委派到具體的建造者(ConcreteBuilder)弟劲;
各個(gè)具體建造者負(fù)責(zé)進(jìn)行產(chǎn)品部件的構(gòu)建;
最終構(gòu)建成具體產(chǎn)品(Product姥芥。
1.2主要作用:
- 在用戶不知道對(duì)象的建造過(guò)程和細(xì)節(jié)的情況下就可以直接創(chuàng)建復(fù)雜的對(duì)象兔乞。
- 用戶只需要給出指定復(fù)雜對(duì)象的類型和內(nèi)容;
- 建造者模式負(fù)責(zé)按順序創(chuàng)建復(fù)雜對(duì)象(把內(nèi)部的建造過(guò)程和細(xì)節(jié)隱藏起來(lái))
1.3解決問(wèn)題
方便用戶創(chuàng)建復(fù)雜的對(duì)象(不需要知道實(shí)現(xiàn)過(guò)程)
代碼復(fù)用性 & 封裝性(將對(duì)象構(gòu)建過(guò)程和細(xì)節(jié)進(jìn)行封裝 & 復(fù)用)
UML圖
示例代碼:
產(chǎn)品類
public class Bike {
private:
IFrame frame;
ISeat seat;
ITire tire;
public :
IFrame getFrame() {
return frame;
}
void setFrame(IFrame frame) {
this.frame = frame;
}
ISeat getSeat() {
return seat;
}
void setSeat(ISeat seat) {
this.seat = seat;
}
ITire getTire() {
return tire;
}
void setTire(ITire tire) {
this.tire = tire;
}
}
抽象Builder
public class Builder {
virtual void buildFrame();
virtual void buildSeat();
virtual void buildTire();
virtual Bike createBike();
}
具體Builder
// 具體 builder 類
public class MobikeBuilder : public Builder{
private Bike mBike = new Bike();
public:
void buildFrame() {
mBike.setFrame(new AlloyFrame());
}
void buildSeat() {
mBike.setSeat(new DermisSeat());
}
void buildTire() {
mBike.setTire(new SolidTire());
}
Bike createBike() {
return mBike;
}
}
指揮者
public class Director {
private:
Builder mBuilder;
public:
Director(Builder builder) {
mBuilder = builder;
}
Bike construct() {
mBuilder.buildFrame();
mBuilder.buildSeat();
mBuilder.buildTire();
return mBuilder.createBike();
}
}
使用者
private void showBike(Builder builder) {
Director director = new Director(builder);
Bike bike = director.construct();
bike.getFrame().frame();
bike.getSeat().seat();
bike.getTire().tire();
}
void mian(){
showBike(new MobikeBuilder());
}
常規(guī)用發(fā)凉唐,使用者跟指揮者描述庸追,指揮者調(diào)用具體的Builder,這樣比在開(kāi)發(fā)時(shí)候比較繁瑣,一般沒(méi)有指揮者熊榛,指揮者和Builder結(jié)合
項(xiàng)目中常用的方式
public class NewBuilder {
public:
virtual void buildFrame();
virtual void buildSeat();
virtual void buildTire();
virtual Bike createBike();
/**
* 把導(dǎo)演類中的construct()方法合并到抽象建造者類中
* @return 具體產(chǎn)品對(duì)象
*/
Bike construct() {
this.buildFrame();
this.buildSeat();
this.buildTire();
return this.createBike();
}
}
產(chǎn)品
// 省略 getter 和 setter 方法
public class Computer {
private :
String cpu;
String screen;
String memory;
String mainboard;
public :
Computer(String cpu, String screen, String memory, String mainboard) {
this.cpu = cpu;
this.screen = screen;
this.memory = memory;
this.mainboard = mainboard;
}
}
public class NewComputer {
private:
String cpu;
String screen;
String memory;
String mainboard;
public :
NewComputer() {
}
private:
NewComputer(Builder builder) {
cpu = builder.cpu;
screen = builder.screen;
memory = builder.memory;
mainboard = builder.mainboard;
}
public class Builder {
private:
String cpu;
String screen;
String memory;
String mainboard;
public:
Builder() {}
Builder cpu(String val) {
cpu = val;
return this;
}
Builder screen(String val) {
screen = val;
return this;
}
Builder memory(String val) {
memory = val;
return this;
}
Builder mainboard(String val) {
mainboard = val;
return this;
}
NewComputer build() {
return new NewComputer(this);}
}
}
使用
void main() {
// 非 Builder 模式
Computer computer = new Computer(“cpu”, “screen”, “memory”, “mainboard”);
// Builder 模式
NewComputer newComputer = new NewComputer.Builder()
.cpu(“cpu”)
.screen(“screen”)
.memory(“memory”)
.mainboard(“mainboard”)
.build();
}
上面的示例代碼只是傳入四個(gè)參數(shù)锚国,如果參數(shù)是十四個(gè)甚至更多,builder 模式的優(yōu)勢(shì)將會(huì)更加明顯玄坦,傳遞參數(shù)更加靈活血筑,代碼具有更高的可讀性.
優(yōu)點(diǎn)
使用建造者模式可以使客戶端不必知道產(chǎn)品內(nèi)部組成的細(xì)節(jié)绘沉。
具體的建造者類之間是相互獨(dú)立的,這有利于系統(tǒng)的擴(kuò)展豺总。
具體的建造者相互獨(dú)立车伞,因此可以對(duì)建造的過(guò)程逐步細(xì)化,而不會(huì)對(duì)其他模塊產(chǎn)生任何影響喻喳。
缺點(diǎn)
建造者模式所創(chuàng)建的產(chǎn)品一般具有較多的共同點(diǎn)另玖,其組成部分相似;如果產(chǎn)品之間的差異性很大表伦,則不適合使用建造者模式谦去,因此其使用范圍受到一定的限制。
如果產(chǎn)品的內(nèi)部變化復(fù)雜蹦哼,可能會(huì)導(dǎo)致需要定義很多具體建造者類來(lái)實(shí)現(xiàn)這種變化鳄哭,導(dǎo)致系統(tǒng)變得很龐大。
-
原型模式(用原型實(shí)例纲熏,創(chuàng)建對(duì)象的種類妆丘,并且通過(guò)拷貝這些原型創(chuàng)建新的對(duì)象)
UML 例圖
例子代碼
#include <iostream>
#include <string>
#include <list>
using namespace std;
//產(chǎn)品類
class Computer
{
public:
void SetBrandName(string brandName) //設(shè)置品牌
{
this->m_BrandName = brandName;
}
void SetCpu(string cpu) //設(shè)置CPU
{
this->m_Cpu = cpu;
}
void SetMemory(string memory) //設(shè)置內(nèi)存
{
this->m_Memory = memory;
}
void SetVideoCard(string videoCard) //設(shè)置顯卡
{
this->m_VideoCard=videoCard;
}
Computer Clone() //克隆函數(shù)
{
Computer ret;
ret.SetBrandName(this->m_BrandName);
ret.SetCpu(this->m_Cpu);
ret.SetMemory(this->m_Memory);
ret.SetVideoCard(this->m_VideoCard);
return ret;
}
void ShowParams() //顯示電腦相關(guān)信息
{
cout << "該款電腦相關(guān)參數(shù)如下:" << endl;
cout << "品牌:" << this->m_BrandName << endl;
cout << "CPU:" << this->m_Cpu << endl;
cout << "內(nèi)存:" << this->m_Memory << endl;
cout << "顯卡:" << this->m_VideoCard << endl;
}
private:
string m_BrandName; //品牌
string m_Cpu; //CPU
string m_Memory; //內(nèi)存
string m_VideoCard; //顯卡
};
int main()
{
//本例以電腦代工廠為例,分別生產(chǎn)同一配置局劲,不同品牌的電腦
//先生產(chǎn)華碩電腦
Computer asusComputer;
asusComputer.SetBrandName("華碩");
asusComputer.SetCpu("I7 8700");
asusComputer.SetMemory("16g");
asusComputer.SetVideoCard("gtx 1080 ti");
asusComputer.ShowParams();
cout << endl;
//再生產(chǎn)宏基電腦
Computer acerComputer = asusComputer.Clone();
acerComputer.SetBrandName("宏基");
acerComputer.ShowParams();
getchar();
return 0;
}
對(duì)于相同類型的產(chǎn)品可以直接繼承調(diào)用Clone,簡(jiǎn)約代碼