郭新乐软件设计与体系结构实验4知识讲解.docx
- 文档编号:4171284
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:37
- 大小:131.37KB
郭新乐软件设计与体系结构实验4知识讲解.docx
《郭新乐软件设计与体系结构实验4知识讲解.docx》由会员分享,可在线阅读,更多相关《郭新乐软件设计与体系结构实验4知识讲解.docx(37页珍藏版)》请在冰豆网上搜索。
郭新乐软件设计与体系结构实验4知识讲解
郭新乐软件设计与体系结构-实验4
大连海事大学
软件设计与体系结构
课程实验报告
(2015-2016学年第一学期)
实验四设计模式应用
班级:
计算机科学与技术2班
学号:
2220132451
姓名:
程子璇
指导教师:
谢兄
成绩:
2016年01月02日
1实验目的
1.1利用Observer、单件等设计模式开发实现一个小的系统。
1.2通过常用的设计模式的实例实验,熟悉设计模式特点及应用条件,掌握基于设计模式的软件系统的设计与开发应用。
2实验内容
2.1用C++语言实现系统。
2.2利用设计模式设计并实现一个简单的图形编辑系统。
程序截图
3实验要求
(1)深入复习教材中介绍的各种设计模式。
(2)实验步骤和实验总结中要详细描述实验过程中出现的问题、原因和解决方法。
4实验步骤
4.1采用单件设计模式设计抽象工厂类,画出类结构图,写出类结构图对应的代码原型,并写出相应的主函数中的实现代码。
4.1.1.类结构图
4.1.2.代码原型
namespaceges{
classAbstractFactory{
public:
~AbstractFactory(){
if(_count==1){
if(_pInstance){
delete_pInstance;
}
}
--_count;
}
protected:
AbstractFactory(){}
public:
virtualLine*CreateProductLine(constint&posX0,constint&posY0,
constint&posX1,constint&posY1,constint&id)
{returnnullptr;}
virtualSymbol*CreateProductSymbol(constint&posX,constint&posY,constint&id)
{returnnullptr;}
virtualText*CreateProductText(constint&posX,constint&posY,constint&id)
{returnnullptr;}
virtualRectangle*CreateProductRectangle(constint&posX0,constint&posY0,constint&posX1,constint&posY1,constint&id)
{returnnullptr;}
virtualPicture*CreateProductPicture(constint&id){
returnnewPicture(id);
};
public:
staticAbstractFactory*CreateInstance(){
if(nullptr==_pInstance){
_pInstance=newAbstractFactory();
}
++_count;
return_pInstance;
}
protected:
staticAbstractFactory*_pInstance;
staticint_count;
};
classConcreteFactory1:
publicAbstractFactory{
public:
~ConcreteFactory1(){
if(_count==1){
if(_pInstance){
delete_pInstance;
}
}
--_count;
}
private:
ConcreteFactory1()
{}
public:
virtualLine*CreateProductLine(constint&posX0,constint&posY0,
constint&posX1,constint&posY1,constint&id)override{
returnnewLine1(posX0,posY0,posX1,posY1,id);
}
virtualSymbol*CreateProductSymbol(constint&posX,constint&posY,constint&id)override{
returnnewSymbol1(posX,posY,id);
}
virtualText*CreateProductText(constint&posX,constint&posY,constint&id)override{
returnnewText1(TEXT("双击输入标注内容"),posX,posY,id);
}
virtualRectangle*CreateProductRectangle(constint&posX0,constint&posY0,constint&posX1,constint&posY1,constint&id)override{
returnnewRectangle1(posX0,posY0,posX1,posY1,id);
}
//Hiddeninterfaces
virtualPicture*CreateProductPicture(constint&id)override{returnnullptr;}
public:
staticConcreteFactory1*CreateInstance(){
if(nullptr==_pInstance){
_pInstance=newConcreteFactory1();
}
++_count;
return_pInstance;
}
protected:
staticConcreteFactory1*_pInstance;
staticint_count;
};
classConcreteFactory2:
publicAbstractFactory{
public:
~ConcreteFactory2(){
if(_count==1){
if(_pInstance){
delete_pInstance;
}
}
--_count;
}
private:
ConcreteFactory2(){}
public:
virtualLine*CreateProductLine(constint&posX0,constint&posY0,
constint&posX1,constint&posY1,constint&id)override{
returnnewLine2(TEXT("200"),posX0,posY0,posX1,posY1,id);
}
virtualSymbol*CreateProductSymbol(constint&posX,constint&posY,constint&id)override{
returnnewSymbol2(TEXT("静61-127C"),posX,posY,id);
}
virtualText*CreateProductText(constint&posX,constint&posY,constint&id)override{
returnnewText2(TEXT("双击输入标注内容"),posX,posY,id);
}
virtualRectangle*CreateProductRectangle(constint&posX0,constint&posY0,constint&posX1,constint&posY1,constint&id)override{
returnnewRectangle2(posX0,posY0,posX1,posY1,id);;
}
//Hiddeninterfaces
virtualPicture*CreateProductPicture(constint&id)override{returnnullptr;}
public:
staticConcreteFactory2*CreateInstance(){
if(nullptr==_pInstance){
_pInstance=newConcreteFactory2();
}
++_count;
return_pInstance;
}
protected:
staticConcreteFactory2*_pInstance;
staticint_count;
};
}//namespaceges
4.1.3.主函数中的实现代码
_pFactory=AbstractFactory:
:
CreateInstance();
……
_pFactory=ConcreteFactory2:
:
CreateInstance();
……
_pFactory=ConcreteFactory1:
:
CreateInstance();
……
4.2采用Composite设计模式设计直线、符号、字符串、方框及复合图形,画出类结构图,写出类结构图对应的代码原型,并写出相应的主函数中的实现代码。
4.2.1类结构图
4.2.2.代码原型
namespaceges{
classClient;//forwarddeclaringclassClient
//**************************************************************
//[class]Graphic
classGraphicabstract:
publicIDed{
public:
Graphic(constint&id):
IDed(id){}
virtual~Graphic(){};
public:
virtualvoidDraw(Client*pView,constHDC&hdc)const=0;
virtualvoidUpdate(Client*pView,constHDC&hdc)const=0;
virtualvoidAdd(Graphic*pGraphicObj)=0;
virtualvoidRemove(constint&id)=0;
virtualGraphic*GetChild(constint&id)=0;
virtualvoidOnMove(constshort&deltaX,constshort&deltaY)=0;
public:
virtualboolIsHit(constint&x,constint&y)const=0;
virtualintQueryInterface()const=0;
virtualboolSave(HANDLEhFile)const=0;
virtualboolLoad(HANDLEhFile)=0;
};
//************************************************************
//[class]Picture
classPicture:
publicGraphic{
public:
Picture(constint&id):
Graphic(id),_real(false){}
~Picture(){
if(_real){
for(auto&pObj:
_pGraphics){
deletepObj;
}
}
}
//Implementations
public:
virtualvoidDraw(Client*pView,constHDC&hdc)constoverride{}
virtualvoidUpdate(Client*pView,constHDC&hdc)constoverride{
for(auto&pObj:
_pGraphics){
pObj->Update(pView,hdc);
}
}
virtualvoidAdd(Graphic*pGraphicObj)override{
for(auto&pObj:
_pGraphics){
if(pObj->IDEqual(pGraphicObj->GetID()))
return;//Error:
Conflictwithexistinggraphic
}
_pGraphics.push_back(pGraphicObj);
}
virtualvoidRemove(constint&id)override{
std:
:
vector : Graphic*>: : const_iteratorciter; for(citer=_pGraphics.begin(); citer! =_pGraphics.end(); ++citer){ if((*citer)->IDEqual(id)){ delete(*citer); _pGraphics.erase(citer); return; } } } virtualGraphic*GetChild(constint&id)override{ for(auto&pChild: _pGraphics){ if(pChild->IDEqual(id)) returnpChild; } returnnullptr; } virtualvoidOnMove(constshort&deltaX,constshort&deltaY)override{ for(auto&pObj: _pGraphics){ pObj->OnMove(deltaX,deltaY); } } virtualboolSave(HANDLEhFile)constoverride{ DWORDbw=0; intgt=GT_PICTURE; intgroup=GES_FILE_GROUP_TOUCH; //WriteGTintothefile. if(FALSE==WriteFile(hFile,>,sizeof(int),&bw,nullptr)) returnfalse; for(auto&pObj: _pGraphics){ //WriteGroup-Beginflagintothefile. if(FALSE==WriteFile(hFile,&group,sizeof(int),&bw,nullptr)) returnfalse; if(false==pObj->Save(hFile)){ CloseHandle(hFile); returnfalse; } } returntrue; } virtualboolLoad(HANDLEhFile)override; virtualintQueryInterface()constoverride{ returnGT_PICTURE;//Thisisapictureclass. } public: virtualboolIsHit(constint&x,constint&y)constoverride{ for(auto&pObj: _pGraphics){ if(pObj->IsHit(x,y)) returntrue; } returnfalse; } virtualvoidGetGraphicsPtrs(std: : vector pGraphics=_pGraphics; } public: inlinevoidSetReal(constbool&real){_real=real;} private: std: : vector bool_real; }; namespaceges{ classClient;//forwarddeclaringclassClient classSymbolabstract: publicGraphic{ public: Symbol(constint&id): Graphic(id){} public: virtualvoidSetText(TCHAR*pText)=0; public: //Hidtheunusedinterfaces virtualvoidAdd(Graphic*pGraphicObj)override{}; virtualvoidRemove(constint&id)override{}; virtualGraphic*GetChild(constint&id)override {returnnullptr;}; public: virtualintQueryInterface()constoverride{ returnGT_SYMBOL;//Thisisasymbolclass. } }; classLineabstract: publicGraphic{ public: Line(constint&id): Graphic(id){} public: virtualvoidSetStartPos(constint&posX,constint&posY)=0; virtualvoidSetEndPos(constint&posX,constint&posY)=0; virtualvoidSetText(TCHAR*pText)=0; public: virtualboolIsPossibleToViewThis()const=0; public: //Hidtheunusedinterfaces virtualvoidAdd(Graphic*pGraphicObj)override{}; virtualvoidRemove(constint&id)override{}; virtualGraphic*GetChild(constint&id)override {returnnullptr;}; public: virtualintQueryInterface()constoverride{ returnGT_LINE;//Thisisalineclass. } }; classTextabstract: publicGraphic{ public: Text(constint&id): Graphic(id){} public: virtualvoidSetFontSize(constint&fontSize)=0; virtualvoidSetText(TCHAR*pText)=0; public: //Hidtheunusedinterfaces virtualvoidAdd(Graphic*pGraphicObj)override{}; virtualvoidRemove(constint&id)override{}; virtualGraphic*GetChild(constint&id)override {returnnullptr;}; public: virtualintQueryInterface()constoverride{ returnGT_TEXT;//Thisisatextclass. } }; classRectangleabstract: publicGraphic{ public: Rectangle(constint&id): Graphic(id){} public: virtualvoidSetStartPos(constint&posX,constint&posY)=0; virtualvoidSetEndPos(constint&posX,constint&posY)=0; virtualboolIsPossibleToViewThis()const=0; public: //Hidtheunusedinterfaces&pGraphics)const{
_pGraphics;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 新乐 软件设计 体系结构 实验 知识 讲解
![提示](https://static.bdocx.com/images/bang_tan.gif)