设计模式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;
}
#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;
}
相关文章推荐
- 最大堆(最小堆)C++实现源码
- LRU(近期最少使用算法)C++实现源码
- B树的原理以及C++实现(附源码和文档)
- 较高人工智能的人机博弈程序实现(多个算法结合)含C++源码
- C++ 实现读取URL中的源码
- 红黑树的C++完整实现源码
- [C++]Vector源码实现
- stand C++ stack 的实现源码
- 在Android源码中查找Java代码中native函数对应的C++实现
- 固定尺寸内存块的缓冲队列类及C++实现源码
- 实现CEGUI中文汉字输入法光标跟随(C/C++源码)
- 基于Windows Socket的安全通信(C++实现,附源码)
- C++实现自己的插件框架 基于QtCreator源码裁剪
- 设计模式Mediator的C++实现源码
- 设计模式adapter的C++实现源码
- SVM与C++源码实现
- 关于C++类库KYLib: 用C语言实现平衡二叉树(AVL tree)的源码
- [图形学] B样条曲线 - 原理和C++实现的演示程序(附源码)
- QQ2008和2012截屏の实现(C++源码)
- 线性最小二乘拟合算法实现-附C++源码