目录
类别 | 描述 |
---|---|
适用场景 |
|
优点 |
|
缺点 |
|
- #include
- 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
- 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
- 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;
- }
只允许有一个类对象存在的一种模式
类别 | 描述 |
适用场景 |
|
优点 |
|
缺点 |
|
- #include
- 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;
- }