您的位置:首页 > 编程语言 > C语言/C++

设计模式prototype的C++实现源码

2010-05-21 17:06 399 查看
1、抽象类IGraphic定义

#ifndef IGRAPHIC_H
#define IGRAPHIC_H

class IGraphic
{
public:
virtual char* getName() = 0;
virtual IGraphic* clone() = 0;
};

#endif

2、具体实现类MyGraphic定义

#ifndef MYGRAPHIC_H
#define MYGRAPHIC_H

#include "IGraphic.h"

#define STR_MYGRAPHIC "szName: "
#define STR_CLONENUM " Num: "
#define STR_SZNAMEADDR " szNameAddr: "
#define STR_SZRESADDR " szResAddr: "

class MyGraphic : public IGraphic
{
public:
MyGraphic(MyGraphic& mp);
MyGraphic(char* sname);
~MyGraphic();
char* getName();
IGraphic* clone();

private:
char* szName;
int num;
char* szRes;
};

#endif

3、具体实现类MyGraphic实现

#include <stdio.h>
#include <string.h>
#include "MyGraphic.h"

MyGraphic::MyGraphic(MyGraphic& mp)
{
num = 0;
if(mp.szName != NULL)
{
szName = new char[strlen(mp.szName) + 1];
strcpy(szName,mp.szName);
}
else
{
szName = NULL;
}
if(mp.szRes != NULL)
{
szRes = new char[strlen(mp.szRes) + 1];
strcpy(szRes,mp.szRes);
}
else
{
szRes = NULL;
}
}

MyGraphic::MyGraphic(char* sname)
{
num = 0;
szRes = NULL;
if(sname != NULL)
{
szName = new char[strlen(sname) + 1];
strcpy(szName,sname);
}
else
{
szName = NULL;
}
}

MyGraphic::~MyGraphic()
{
num = 0;
if(szName != NULL)
{
delete[] szName;
szName = NULL;
}
if(szRes != NULL)
{
delete[] szRes;
szRes = NULL;
}
}

char* MyGraphic::getName()
{

if(szRes != NULL)
{
// printf("szRes:%s Address:%08x/n",szRes,szRes);
delete[] szRes;
szRes = NULL;
}
char* stmp = new char[3];
sprintf(stmp,"%02d",num);
int len = strlen(STR_MYGRAPHIC) + strlen(szName) + strlen(STR_MYGRAPHIC) + strlen(stmp);// + strlen(STR_SZNAMEADDR) + strlen(STR_SZRESADDR) + 10;
szRes = new char[len + 1];
strcpy(szRes, STR_MYGRAPHIC);
strcat(szRes, szName);
strcat(szRes, STR_CLONENUM);
strcat(szRes, stmp);
delete[] stmp;
// printf("szRes:%s Address:%08x/n",szRes,szRes);
return szRes;

}

IGraphic* MyGraphic::clone()
{
num++;
MyGraphic* ip = new MyGraphic(*this);

return ip;
}

4、具体实现类YourGraphic定义

#ifndef YOURGRAPHIC_H
#define YOURGRAPHIC_H
#include "IGraphic.h"

#define STR_MYGRAPHIC "szName: "
#define STR_CLONENUM " Num: "
#define STR_SZNAMEADDR " szNameAddr: "
#define STR_SZRESADDR " szResAddr: "

class YourGraphic : public IGraphic
{
public:
YourGraphic(YourGraphic& mp);
YourGraphic(char* sname);
~YourGraphic();
char* getName();
IGraphic* clone();

private:
char* szName;
int num;
char* szRes;
};

#endif

5、具体实现类YourGraphic实现

#include <stdio.h>
#include <string.h>
#include "YourGraphic.h"

YourGraphic::YourGraphic(YourGraphic& mp)
{
num = 0;
if(mp.szName != NULL)
{
szName = new char[strlen(mp.szName) + 1];
strcpy(szName,mp.szName);
}
else
{
szName = NULL;
}
if(mp.szRes != NULL)
{
szRes = new char[strlen(mp.szRes) + 1];
strcpy(szRes,mp.szRes);
}
else
{
szRes = NULL;
}
}

YourGraphic::YourGraphic(char* sname)
{
num = 0;
szRes = NULL;
if(sname != NULL)
{
szName = new char[strlen(sname) + 1];
strcpy(szName,sname);
}
else
{
szName = NULL;
}
}

YourGraphic::~YourGraphic()
{
num = 0;
if(szName != NULL)
{
delete[] szName;
szName = NULL;
}
if(szRes != NULL)
{
delete[] szRes;
szRes = NULL;
}
}

char* YourGraphic::getName()
{
if(szRes != NULL)
{
// printf("szRes:%s Address:%08x/n",szRes,szRes);
delete[] szRes;
szRes = NULL;
}
char* stmp = new char[3];
sprintf(stmp,"%02d",num);
int len = strlen(STR_MYGRAPHIC) + strlen(szName) + strlen(STR_MYGRAPHIC) + strlen(stmp);// + strlen(STR_SZNAMEADDR) + strlen(STR_SZRESADDR) + 10;
szRes = new char[len + 1];
strcpy(szRes, STR_MYGRAPHIC);
strcat(szRes, szName);
strcat(szRes, STR_CLONENUM);
strcat(szRes, stmp);
delete[] stmp;
// printf("szRes:%s Address:%08x/n",szRes,szRes);
return szRes;
}

IGraphic* YourGraphic::clone()
{
num++;
YourGraphic* ip = new YourGraphic(*this);

return ip;
}

6、客户使用类Client实现

#include <stdio.h>
#include <string.h>
#include "IGraphic.h"
#include "MyGraphic.h"
#include "YourGraphic.h"

int main()
{
IGraphic* mp = new MyGraphic("mmmm");
printf("mp == %08x ::: %s/n",&mp,mp->getName());
IGraphic* mp1 = mp->clone();
printf("mp1 == %x ::: %s/n",&mp1,mp1->getName());
printf("mp == %x ::: %s/n",&mp,mp->getName());
IGraphic* mp2 = mp1->clone();
printf("mp2 == %x ::: %s/n",&mp2,mp2->getName());
printf("mp1 == %x ::: %s/n",&mp1,mp1->getName());
printf("mp == %x ::: %s/n",&mp,mp->getName());

IGraphic* yp = new YourGraphic("yyyy");
printf("yp == %08x ::: %s/n",&yp,yp->getName());
IGraphic* yp1 = yp->clone();
printf("yp1 == %x ::: %s/n",&yp1,yp1->getName());
printf("yp == %x ::: %s/n",&yp,yp->getName());
IGraphic* yp2 = yp1->clone();
printf("yp2 == %x ::: %s/n",&yp2,yp2->getName());
printf("yp1 == %x ::: %s/n",&yp1,yp1->getName());
printf("yp == %x ::: %s/n",&yp,yp->getName());
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: