谈C++设计模式

C++设计模式

这里我只谈单件和工厂。

A: 单件Singleton,它允许一个类有且只有一个实例的方法。也就是说类的实体只有1个。

//Example Singleton

#include <iostream>

using namespace std;

 

class Singleton {

                     static Singleton s;

                     int i;

                     Singleton(int x):i(x){};

                     Singleton& operator =(const Singleton& other);// Disallowed

                     Singleton(const Singleton& other);// Disallowed

public:

                     static Singleton& instance() {return s;}

                     int getValue() {return i;}

                     void setValue(int x) {i=x;}

};

 

Singleton Singleton::Singleton();

int main(){

                     Singleton& s=Singleton::instance();

                     ...

}

我们也可以定义一个static的指针,一个staticinstance函数,用来返回一个static的实体,实现方法是多样的,单目标只有1个就是保证类的实体只有1个。

//用于指针的方法实现Singleton

static Singleton * Instance()  

                {  

                       if( NULL == _instance)  

                              {  

                                   _instance   =   new   Singleton;//这里如何使用有参构造函数。  

                              }  

                              return   _instance;  

                }

 

B:工厂Factory

工厂用来创建对象,而不允许将创建对象的代码分布于整个系统。

我们先11个来看,然后再分析区别在哪里。

1:通用工厂:

//Factory

#include <iostream>

#include <stdexcept>

#include <cstddef>

#include <string>

using namespace std;

 

//Base

class Disk{

public:

       virtual void Play()=0;

       virtual ~Disk(){};

       class BadDiskPlay : public logic_error{

       public:

              BadDiskPlay(string type):logic_error("Cann't play type"+type){};

       };

       static Disk* factory(const string& type)

              throw(BadDiskPlay);

};

 

//DVD

class DVD : public Disk

{

       DVD(){}; //Private Constructor

       friend class Disk;

public:

       void Play(){cout << "DVD::Play" << endl;};

       ~DVD(){cout << "DVD::~DVD" << endl;};

};

 

// VCD

class VCD : public Disk

{

       VCD(){}; //Private Constructor

       friend class Disk;

public:

       void Play(){cout << "VCD::Play" << endl;};

       ~VCD(){cout << "VCD::~VCD" << endl;};

};

 

Disk* Disk::factory(const string& type)

{

       throw(Disk::BadDiskPlay){

              if ("DVD" == type) {

                     return new DVD;

              }

              else if ("VCD" == type) {

                     return new VCD;

              }

              throw BadDiskPlay(type);

       }

}

2:多态工厂

//Factory2

#include <iostream>

#include <stdexcept>

#include <cstddef>

#include <string>

using namespace std;

 

//Base

class Disk{

public:

       virtual void Play()=0;

       virtual ~Disk(){};

};

 

class DiskFactory{

       virtual Disk* create ()=0;

       static map<string,DiskFactory*> factories;

public:

       virtual ~DiskFactory(){}

       friend class DiskFacoryInitializer;

       class BadDiskCreation:public logic_error{

    public:

              BadDiskCreation(string type):logic_error("Cann't creat type"+type){}

       };

 

    static Disk* CreateDisk(const string& id) throw(BadDiskCreation){

           if(factories.find(id) != factories.end())

                  return factories.[id]->create();

           else

                  throw BadDiskCreation(id);

       }

};

 

//Define the static objects;

map<string, DiskFactory*> DiskFactory::factories;

 

//DVD

class DVD : public Disk

{

       DVD(){}; //Private Constructor

       friend class DiskFactoryInitializer;

       class Factory;

       friend class Factory;

       class Factory : public DiskFactory{

       public:

              Disk* creat(){return new DVD;}

              friend class DiskFactoryInitalizer;

       };

public:

       void Play(){cout << "DVD::Play" << endl;};

       ~DVD(){cout << "DVD::~DVD" << endl;};

};

 

// VCD

class VCD : public Disk

{

       VCD(){}; //Private Constructor

       friend class DiskFactoryInitializer;

       class Factory;

       friend class Factory;

       class Factory : public DiskFactory{

       public:

              Disk* creat(){return new DVD;}

              friend class DiskFactoryInitalizer;

       };

public:

       void Play(){cout << "VCD::Play" << endl;};

       ~VCD(){cout << "VCD::~VCD" << endl;};

};

 

// Singleton to initialize the DiskFactory

class DiskFactoryInitializer{

       static DiskFactoryInitializer si;

       DiskFactoryInitializer(){

              DiskFactory::factories["DVD"]=new DVD::Factory;

              DiskFactory::factories["VCD"]=new VCD::Factory;

       }

    ~DiskFactoryInitializer(){

              map<string, DiskFactory*>::iterator it=

                     DiskFactory::factories.begin();

              while (it != DiskFactory::factories.end()) {

                     delete it++->second;

              }

};

 

//Static member definition

       DiskFactoryInitializer DiskFactoryInitializer::si;

  

3.抽象工厂
//Factory 3 Abstract Factory
class Obstacle
{
public:
    virtual void action()=0;
};
 
class Player{
public:
       virtual void interactWith(Obstacle*)=0;
};
 
class Kitty: public Player{
       virtual void interactWith(Obstacle* ob){
              cout<<"Kitty has encountered a ";
              ob->action();
       }
};
 
class KungFuGuy: public Player{
       virtual void interactWith(Obstacle* ob){
              cout<<"KungFuGuy now battles against a";
              ob->action();
       }
};
 
class Puzzle: public Obstacle{
public:
       void action(){cout<<"Puzzle"<<endl;}
};
 
class NastyWeapon: public Obstacle{
public:
       void action(){cout<<"NastyWeapon"<<endl;}
};
 
//The abstract Factory
class GameElementFactory{
public:
       virtual Player* makePlayer()=0;
       virtual Obstacle* makeObstacle()=0;
};
 
//Concrete factories
class KittyAndPuzzles: public GameElementFactory{
public:
       virtual Player* makePlayer(){return new Kitty};
       virtual Obstacle* makeObstacle(){return new Puzzle};
};
 
class KillAndDismember: public GameElementFactory{
public:
       virtual Player* makePlayer(){return new KungFuGuy};
       virtual Obstacle* makeObstacle(){return new NastyWeapon};
};
 
class GameEnvironment{
       GameElementFactory* gef;
       Player* p;
       Obstacle* ob;
public:
       GameEnvironment(GameElementFactory* factory):gef(factory),
                                                       p(factory->makePlayer()),
                                                                                     ob(factory->makeObstacle())
       {
       }
       void Play(){p->interactWith(ob);}
       ~GameEnvironment(){
              delete p;
              delete ob;
              delete gef;
       }
};

-----------------------------------------还没完,等我写------------------------------------------

  • 1
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 4

打赏作者

KenYuan2016

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值