系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、单例模式
1.饿汉式
class SingleTon{
public:
static SingleTon* getInstance(){
return instance;
}
private:
Singleton(){}
static Singleton* instance;
};
Singleton* Singleton::instance = new Singleton;
2.懒汉式
class Singleton{
public:
static Singleton* getInstance(){
if(NULL == instance){
instance = new Singleton;
}
return instance;
}
private:
Singleton(){}
static Singleton* instance;
};
Singleton* Singleton::instance = NULL;
二、工厂模式
1.简单工厂
class fruit{
public:
virtual void name() = 0;
};
class apple:public fruit{
public:
virtual void name(){
cout << "apple" << endl;
}
};
class pear:public fruit{
public:
virtual void name(){
cout << "pear" << endl;
}
};
class Factory{
public:
fruit* createFruit(string name){
if(name == "apple")
return new apple;
else if(name == "pear")
return new pear;
}
};
int main()
{
Factory* fac = new Factory;
fruit* apple = fac->createFruit("apple");
apple->name();
return 0;
}
2.工厂模式
class fruit{
public:
virtual void name() = 0;
};
class apple :public fruit{
public:
virtual void name(){
cout << "apple" << endl;
}
};
class pear :public fruit{
public:
virtual void name(){
cout << "pear" << endl;
}
};
class Factory{
public:
virtual fruit* createFruit() = 0;
};
class appleFactory :public Factory{
public:
fruit* createFruit(){
return new apple;
}
};
class pearFactory :public Factory{
public:
fruit* createFruit(){
return new pear;
}
};
int main()
{
Factory* fac = new appleFactory;
fruit* apple = fac->createFruit();
apple->name();
return 0;
}
3.抽象工厂模式
//例子:电脑配置CPU、显卡、内存,配置一台intel的CPU、内存,kinton的显卡
class CPU{
public:
virtual void calc() = 0;
};
class CARD{
public:
virtual void display() = 0;
};
class MEMORY{
public:
virtual void storage() = 0;
};
class Factory{
public:
virtual CPU* createCpu() = 0;
virtual CARD* createCard() = 0;
virtual MEMORY* createMemory() = 0;
};
class computer{
public:
computer(CPU* cpu, CARD* card, MEMORY* mem){
this->cpu = cpu;
this->card = card;
this->mem = mem;
}
void work(){
cpu->calc();
card->display();
mem->storage();
}
private:
CPU* cpu;
CARD* card;
MEMORY* mem;
};
//------------------
//intel的配置
class IntelCPU :public CPU{
public:
void calc(){ cout << "intelCPU 开始计算.." << endl; }
};
class IntelCARD :public CARD{
public:
void display(){ cout << "intelCARD 开始显示.." << endl; }
};
class IntelMEMORY :public MEMORY{
public:
void storage(){ cout << "intelMEMORY 开始存储.." << endl; }
};
class IntelFactory :public Factory{
public:
virtual CPU* createCpu(){ return new IntelCPU; }
virtual CARD* createCard(){ return new IntelCARD; }
virtual MEMORY* createMemory(){ return new IntelMEMORY; }
};
//Kinton的配置
class KintonCPU :public CPU{
public:
void calc(){ cout << "KintonCPU 开始计算.." << endl; }
};
class KintonCARD :public CARD{
public:
void display(){ cout << "KintonCARD 开始显示.." << endl; }
};
class KintonMEMORY :public MEMORY{
public:
void storage(){ cout << "KintonMEMORY 开始存储.." << endl; }
};
class KintonFactory :public Factory{
public:
virtual CPU* createCpu(){ return new KintonCPU; }
virtual CARD* createCard(){ return new KintonCARD; }
virtual MEMORY* createMemory(){ return new KintonMEMORY; }
};
int main()
{
Factory* intelFac = new IntelFactory;
CPU* intelCpu = intelFac->createCpu();
MEMORY* intelMem = intelFac->createMemory();
Factory* kintonFac = new KintonFactory;
CARD* kintonCard = kintonFac->createCard();
computer* com = new computer(intelCpu, kintonCard, intelMem);
com->work();
return 0;
}
三、代理模式
class item{
public:
item(string name, bool real){
this->name = name;
this->real = real;
}
string getName(){ return name; }
bool isReal(){ return real; }
private:
string name;
bool real;
};
class Shopping{
public:
virtual void buy(item* it) = 0;
};
class usaShopping :public Shopping{
public:
void buy(item* it){
cout << "在美国买了" << it->getName() << endl;
}
};
class chinaShopping :public Shopping{
public:
void buy(item* it){
cout << "在中国买了" << it->getName() << endl;
}
};
class proxyShopping :public Shopping{
public:
proxyShopping(Shopping* s) :shop(s){}
void buy(item* it){
if (it->isReal()){
cout << "是正牌货,可买" << endl;
shop->buy(it);
cout << "成功买回" << endl;
}
else{
cout << "假货,不买了" << endl;
}
}
private:
Shopping* shop;
};
int main()
{
item* it = new item("nike", true);
Shopping* usaShop = new usaShopping;
Shopping* proxyShop = new proxyShopping(usaShop);
proxyShop->buy(it);
return 0;
}
总结
下次一定