目录
一、简单工厂模式
二、工厂模式
三、抽象工厂模式
四、单例模式
一、简单工厂模式
类别 | 描述 |
---|
适用场景 |
- 工厂类负责创建的对象比较少,由于创建的对象较少,不会造成工厂方法中的业务逻辑太过复杂
- 客户端只知道传入工厂类的参数,对于如何创建对象并不关心
|
优点 |
|
缺点 |
- 工厂类职责过重
- 增加系统中类的个数,复杂度增加
- 违反“开闭原则”,添加新产品需要修改工厂逻辑
|
#include <bits/stdc++.h>
using namespace std;
//抽象水果,此处做了核心的工作
class AbstractFruit {
public:
virtual void ShowName() = 0;
};
//苹果
class Apple :public AbstractFruit {
public:
virtual void ShowName() {
cout << "我是苹果!" << endl;
}
};
//香蕉
class Banana :public AbstractFruit {
public:
virtual void ShowName() {
cout << "我是香蕉!" << endl;
}
};
//鸭梨
class Pear :public AbstractFruit {
public:
virtual void ShowName() {
cout << "我是鸭梨!" << endl;
}
};
//水果工厂
class FruitFactor {
public:
static AbstractFruit * CreateFruit(string flag) {
if (flag == "apple") {
return new Apple;
}
else if (flag == "banana") {
return new Banana;
}
else if (flag == "pear") {
return new Pear;
}
else {
return NULL;
}
}
};
int main(){
FruitFactor* factory = new FruitFactor;
AbstractFruit* fruit = factory->CreateFruit("apple");
fruit->ShowName();
delete fruit;
fruit = factory->CreateFruit("banana");
fruit->ShowName();
delete fruit;
fruit = factory->CreateFruit("pear");
fruit->ShowName();
delete fruit;
fruit = NULL;
delete factory;
factory = NULL;
return 0;
}
二、工厂模式
类别 | 描述 |
适用场景 |
|
优点 |
- 针对工厂做了进一步抽象,从而让具体的工厂可以在适用的时候再实例化
- 每个工厂的职责更加单一
|
缺点 |
|
#include <bits/stdc++.h>
using namespace std;
//抽象水果
class AbstractFruit {
public:
virtual void ShowName() = 0;
};
//苹果
class Apple :public AbstractFruit {
public:
virtual void ShowName() {
cout << "我是苹果!" << endl;
}
};
//香蕉
class Banana :public AbstractFruit {
public:
virtual void ShowName() {
cout << "我是香蕉!" << endl;
}
};
//鸭梨
class Pear :public AbstractFruit {
public:
virtual void ShowName() {
cout << "我是鸭梨!" << endl;
}
};
//抽象工厂,进一步抽象了一层,从而可以让工厂提供统一的接口,而不是具体的实现
class AbstractFruitFactory {
public:
virtual AbstractFruit* CreateFruit() = 0;
};
//苹果工厂
class AppleFactory :public AbstractFruitFactory {
public:
virtual AbstractFruit* CreateFruit() {
return new Apple;
}
};
//香蕉工厂
class BananaFactory :public AbstractFruitFactory {
public:
virtual AbstractFruit* CreateFruit() {
return new Banana;
}
};
//梨工厂
class PearFactory :public AbstractFruitFactory {
public:
virtual AbstractFruit* CreateFruit() {
return new Pear;
}
};
int main() {
AbstractFruitFactory* factory = NULL;
AbstractFruit* fruit = NULL;
//创建一个苹果工厂
factory = new AppleFactory;
fruit = factory->CreateFruit();
fruit->ShowName();
delete fruit;
delete factory;
//创建一个香蕉工厂
factory = new BananaFactory;
fruit = factory->CreateFruit();
fruit->ShowName();
delete fruit;
delete factory;
//创建一个鸭梨工厂
factory = new PearFactory;
fruit = factory->CreateFruit();
fruit->ShowName();
delete fruit;
delete factory;
fruit = NULL;
factory = NULL;
return 0;
}
三、抽象工厂模式
类别 | 描述 |
适用场景 |
- 系统中有多于一个的产品族 (美 日 中,核心还是工厂抽象类一定要稳定)
- 产品等级结构稳定 (比如增加桃子时,则抽象工厂得修改)
|
优点 |
- 产品族中的多个对象被设计成一起工作时,它能够保证客户端始终只使用同一个产品族中的对象
|
缺点 |
- 增加新的产品等级结构麻烦,需要对原有系统进行较大的修改
|
#include <bits/stdc++.h>
using namespace std;
//抽象苹果
class AbstractApple {
public:
virtual void ShowName() = 0;
};
//中国苹果
class ChinaApple :public AbstractApple {
public:
virtual void ShowName() {
cout << "中国苹果!" << endl;
}
};
//美国苹果
class USAApple :public AbstractApple {
public:
virtual void ShowName() {
cout << "美国苹果!" << endl;
}
};
//日本苹果
class JapanApple :public AbstractApple {
public:
virtual void ShowName() {
cout << "日本苹果!" << endl;
}
};
//抽象香蕉
class AbstractBanana {
public:
virtual void ShowName() = 0;
};
//中国香蕉
class ChinaBanana :public AbstractBanana {
public:
virtual void ShowName() {
cout << "中国香蕉!" << endl;
}
};
//美国香蕉
class USABanan :public AbstractBanana {
public:
virtual void ShowName() {
cout << "美国香蕉!" << endl;
}
};
//日本香蕉
class JapanBanan :public AbstractBanana {
public:
virtual void ShowName() {
cout << "日本香蕉!" << endl;
}
};
//抽象鸭梨
class AbstractPear {
public:
virtual void ShowName() = 0;
};
//中国鸭梨
class ChinaPear:public AbstractPear{
public:
virtual void ShowName() {
cout << "中国鸭梨!" << endl;
}
};
//美国鸭梨
class USAPear :public AbstractPear {
public:
virtual void ShowName() {
cout << "美国鸭梨!" << endl;
}
};
//日本鸭梨
class JapanPear :public AbstractPear {
public:
virtual void ShowName() {
cout << "日本鸭梨!" << endl;
}
};
//抽象工厂 针对产品族
class AbstractFactory {
public:
virtual AbstractApple* CreateApple() = 0;
virtual AbstractBanana* CreateBanana() = 0;
virtual AbstractPear* CreatePear() = 0;
};
//中国工厂
class ChinaFactory :public AbstractFactory {
public:
virtual AbstractApple* CreateApple() {
return new ChinaApple;
}
virtual AbstractBanana* CreateBanana(){
return new ChinaBanana;
}
virtual AbstractPear* CreatePear() {
return new ChinaPear;
}
};
//美国工厂
class USAFactory :public AbstractFactory {
public:
virtual AbstractApple* CreateApple() {
return new USAApple;
}
virtual AbstractBanana* CreateBanana() {
return new USABanan;
}
virtual AbstractPear* CreatePear() {
return new USAPear;
}
};
//日本工厂
class JapanFactory :public AbstractFactory {
public:
virtual AbstractApple* CreateApple() {
return new JapanApple;
}
virtual AbstractBanana* CreateBanana() {
return new JapanBanan;
}
virtual AbstractPear* CreatePear() {
return new JapanPear;
}
};
int main() {
AbstractFactory* factory = NULL;
AbstractApple* apple = NULL;
AbstractBanana* banana = NULL;
AbstractPear* pear = NULL;
//中国工厂
factory = new ChinaFactory;
apple = factory->CreateApple();
banana = factory->CreateBanana();
pear = factory->CreatePear();
apple->ShowName();
banana->ShowName();
pear->ShowName();
return 0;
}
四、单例模式
只允许有一个类对象存在的一种模式
类别 | 描述 |
适用场景 |
- 系统只需要一个实例对象
- 客户调用类的单个实例只允许使用一个公共访问点
|
优点 |
- 单例模式提供了对唯一实例的受控访问
- 节约系统资源。由于在系统内存中只存在一个对象。
|
缺点 |
|
- 1.懒汉式(需要的时候才会创建)
- 2.饿汉式(直接静态创建,程序起来前就创建好了) -- 解决多线程问题
#include <iostream>
using namespace std;
class A {
private:
A() {
a = new A;
}
public:
static A* getInstance() {
return a;
}
private:
static A* a;
};
A* A::a = NULL;
//实现单例步骤
//1.构造函数私有化
//2.增加静态私有的当前类的指针变量
//3.提供静态对外接口,可以让用户获得单例对象
//单例 分为懒汉式 饿汉式
//1.懒汉式(需要的时候才会创建)
class Singleton_lazy {
private:
Singleton_lazy() { cout << "我是懒汉构造!" << endl; }
public:
static Singleton_lazy* getInstance() {
if (pSingleton == NULL) { /*此处判断有可能被强占执行*/
pSingleton = new Singleton_lazy;
}
return pSingleton;
}
private:
static Singleton_lazy* pSingleton;
};
//类外初始化
Singleton_lazy* Singleton_lazy::pSingleton = NULL;
//2.饿汉式
class Singleton_hungry {
private:
Singleton_hungry() { cout << "我是饿汉构造!" << endl; }
public:
static Singleton_hungry* getInstance() {
return pSingleton;
}
private:
static Singleton_hungry* pSingleton;
};
//类外初始化,此时已经创建对象完了
Singleton_hungry* Singleton_hungry::pSingleton = new Singleton_hungry;
int main() {
Singleton_lazy* p1 = Singleton_lazy::getInstance();
Singleton_lazy* p2 = Singleton_lazy::getInstance();
if (p1 == p2) {
cout << "两个指针指向同一块内存空间,是单例!" << endl;
}
else {
cout << "不是单例模式!" << endl;
}
Singleton_hungry* p3 = Singleton_hungry::getInstance();
Singleton_hungry* p4 = Singleton_hungry::getInstance();
if (p3 == p4) {
cout << "两个指针指向同一块内存空间,是单例!" << endl;
}
else {
cout << "不是单例模式!" << endl;
}
cout << "main函数开始执行!" << endl;
return 0;
}