《郭新乐软件设计与体系结构-实验422679.pdf》由会员分享,可在线阅读,更多相关《郭新乐软件设计与体系结构-实验422679.pdf(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 大连海事大学 软件设计与体系结构 课程实验报告(2015-2016 学年第一学期)实验四 设计模式应用 班 级:计算机科学与技术 2 班 学 号:*名:*指导教师:*成 绩:2016 年 01 月 02 日 目录 1 实验目的.2 2 实验内容.3 3 实验要求.4 4 实验步骤.4 5 总结与体会.21 5.1 实验中出现的问题及其解决方案.21 5.2 总结.21 5.3 体会.21 1 实验目的 1.1 利用 Observer、单件等设计模式开发实现一个小的系统。1.2 通过常用的设计模式的实例实验,熟悉设计模式特点及应用条件,掌握基于设计模式的软件系统的设计与开发应用。2 实验内容
2、2.1 用 C+语言实现系统。2.2 利用设计模式设计并实现一个简单的图形编辑系统。程序截图 3 实验要求(1)深入复习教材中介绍的各种设计模式。(2)实验步骤和实验总结中要详细描述实验过程中出现的问题、原因和解决方法。4 实验步骤 4.1 采用单件设计模式设计抽象工厂类,画出类结构图,写出类结构图对应的代码原型,并写出相应的主函数中的实现代码。4.1.1.类结构图 4.1.2.代码原型 namespace ges class AbstractFactory public:AbstractFactory()if(_count=1)if(_pInstance)delete _pInstance;
3、-_count;protected:AbstractFactory()public:virtual Line*CreateProductLine(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id)return nullptr;virtual Symbol*CreateProductSymbol(const int&posX,const int&posY,const int&id)return nullptr;virtual Text*CreateProductText(const int&p
4、osX,const int&posY,const int&id)return nullptr;virtual Rectangle*CreateProductRectangle(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id)return nullptr;virtual Picture*CreateProductPicture(const int&id)return new Picture(id);public:static AbstractFactory*CreateInstance()i
5、f(nullptr=_pInstance)_pInstance=new AbstractFactory();+_count;return _pInstance;protected:static AbstractFactory*_pInstance;static int _count;class ConcreteFactory1:public AbstractFactory public:ConcreteFactory1()if(_count=1)if(_pInstance)delete _pInstance;-_count;private:ConcreteFactory1()public:vi
6、rtual Line*CreateProductLine(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id)override return new Line1(posX0,posY0,posX1,posY1,id);virtual Symbol*CreateProductSymbol(const int&posX,const int&posY,const int&id)override return new Symbol1(posX,posY,id);virtual Text*CreateP
7、roductText(const int&posX,const int&posY,const int&id)override return new Text1(TEXT(双击输入标注内容),posX,posY,id);virtual Rectangle*CreateProductRectangle(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id)override return new Rectangle1(posX0,posY0,posX1,posY1,id);/Hidden interf
8、aces virtual Picture*CreateProductPicture(const int&id)override return nullptr;public:static ConcreteFactory1*CreateInstance()if(nullptr=_pInstance)_pInstance=new ConcreteFactory1();+_count;return _pInstance;protected:static ConcreteFactory1*_pInstance;static int _count;class ConcreteFactory2:public
9、 AbstractFactory public:ConcreteFactory2()if(_count=1)if(_pInstance)delete _pInstance;-_count;private:ConcreteFactory2()public:virtual Line*CreateProductLine(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id)override return new Line2(TEXT(200),posX0,posY0,posX1,posY1,id);v
10、irtual Symbol*CreateProductSymbol(const int&posX,const int&posY,const int&id)override return new Symbol2(TEXT(静 61-127C),posX,posY,id);virtual Text*CreateProductText(const int&posX,const int&posY,const int&id)override return new Text2(TEXT(双击输入标注内容),posX,posY,id);virtual Rectangle*CreateProductRecta
11、ngle(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id)override return new Rectangle2(posX0,posY0,posX1,posY1,id);/Hidden interfaces virtual Picture*CreateProductPicture(const int&id)override return nullptr;public:static ConcreteFactory2*CreateInstance()if(nullptr=_pInstan
12、ce)_pInstance=new ConcreteFactory2();+_count;return _pInstance;protected:static ConcreteFactory2*_pInstance;static int _count;/namespace ges 4.1.3.主函数中的实现代码 _pFactory=AbstractFactory:CreateInstance();_pFactory=ConcreteFactory2:CreateInstance();_pFactory=ConcreteFactory1:CreateInstance();4.2 采用 Compo
13、site 设计模式设计直线、符号、字符串、方框及复合图形,画出类结构图,写出类结构图对应的代码原型,并写出相应的主函数中的实现代码。4.2.1 类结构图 4.2.2.代码原型 namespace ges class Client;/forward declaring class Client /*/class Graphic class Graphic abstract:public IDed public:Graphic(const int&id):IDed(id)virtual Graphic();public:virtual void Draw(Client*pView,const HD
14、C&hdc)const=0;virtual void Update(Client*pView,const HDC&hdc)const=0;virtual void Add(Graphic*pGraphicObj)=0;virtual void Remove(const int&id)=0;virtual Graphic*GetChild(const int&id)=0;virtual void OnMove(const short&deltaX,const short&deltaY)=0;public:virtual bool IsHit(const int&x,const int&y)con
15、st=0;virtual int QueryInterface()const=0;virtual bool Save(HANDLE hFile)const=0;virtual bool Load(HANDLE hFile)=0;/*/class Picture class Picture:public Graphic public:Picture(const int&id):Graphic(id),_real(false)Picture()if(_real)for(auto&pObj:_pGraphics)delete pObj;/Implementations public:virtual
16、void Draw(Client*pView,const HDC&hdc)const override virtual void Update(Client*pView,const HDC&hdc)const override for(auto&pObj:_pGraphics)pObj-Update(pView,hdc);virtual void Add(Graphic*pGraphicObj)override for(auto&pObj:_pGraphics)if(pObj-IDEqual(pGraphicObj-GetID()return;/Error:Conflict with exis
17、ting graphic _pGraphics.push_back(pGraphicObj);virtual void Remove(const int&id)override std:vector:const_iterator citer;for(citer=_pGraphics.begin();citer!=_pGraphics.end();+citer)if(*citer)-IDEqual(id)delete(*citer);_pGraphics.erase(citer);return;virtual Graphic*GetChild(const int&id)override for(
18、auto&pChild:_pGraphics)if(pChild-IDEqual(id)return pChild;return nullptr;virtual void OnMove(const short&deltaX,const short&deltaY)override for(auto&pObj:_pGraphics)pObj-OnMove(deltaX,deltaY);virtual bool Save(HANDLE hFile)const override DWORD bw=0;int gt=GT_PICTURE;int group=GES_FILE_GROUP_TOUCH;/W
19、rite GT into the file.if(FALSE=WriteFile(hFile,>,sizeof(int),&bw,nullptr)return false;for(auto&pObj:_pGraphics)/Write Group-Begin flag into the file.if(FALSE=WriteFile(hFile,&group,sizeof(int),&bw,nullptr)return false;if(false=pObj-Save(hFile)CloseHandle(hFile);return false;return true;virtual boo
20、l Load(HANDLE hFile)override;virtual int QueryInterface()const override return GT_PICTURE;/This is a picture class.public:virtual bool IsHit(const int&x,const int&y)const override for(auto&pObj:_pGraphics)if(pObj-IsHit(x,y)return true;return false;virtual void GetGraphicsPtrs(std:vector&pGraphics)co
21、nst pGraphics=_pGraphics;public:inline void SetReal(const bool&real)_real=real;private:std:vector _pGraphics;bool _real;namespace ges class Client;/forward declaring class Client class Symbol abstract:public Graphic public:Symbol(const int&id):Graphic(id)public:virtual void SetText(TCHAR*pText)=0;pu
22、blic:/Hid the unused interfaces virtual void Add(Graphic*pGraphicObj)override;virtual void Remove(const int&id)override;virtual Graphic*GetChild(const int&id)override return nullptr;public:virtual int QueryInterface()const override return GT_SYMBOL;/This is a symbol class.;class Line abstract:public
23、 Graphic public:Line(const int&id):Graphic(id)public:virtual void SetStartPos(const int&posX,const int&posY)=0;virtual void SetEndPos(const int&posX,const int&posY)=0;virtual void SetText(TCHAR*pText)=0;public:virtual bool IsPossibleToViewThis()const=0;public:/Hid the unused interfaces virtual void
24、Add(Graphic*pGraphicObj)override;virtual void Remove(const int&id)override;virtual Graphic*GetChild(const int&id)override return nullptr;public:virtual int QueryInterface()const override return GT_LINE;/This is a line class.;class Text abstract:public Graphic public:Text(const int&id):Graphic(id)pub
25、lic:virtual void SetFontSize(const int&fontSize)=0;virtual void SetText(TCHAR*pText)=0;public:/Hid the unused interfaces virtual void Add(Graphic*pGraphicObj)override;virtual void Remove(const int&id)override;virtual Graphic*GetChild(const int&id)override return nullptr;public:virtual int QueryInter
26、face()const override return GT_TEXT;/This is a text class.;class Rectangle abstract:public Graphic public:Rectangle(const int&id):Graphic(id)public:virtual void SetStartPos(const int&posX,const int&posY)=0;virtual void SetEndPos(const int&posX,const int&posY)=0;virtual bool IsPossibleToViewThis()con
27、st=0;public:/Hid the unused interfaces virtual void Add(Graphic*pGraphicObj)override;virtual void Remove(const int&id)override;virtual Graphic*GetChild(const int&id)override return nullptr;public:virtual int QueryInterface()const override return GT_RECTANGLE;/This is a rectangle class.;class Line1:p
28、ublic Line public:Line1(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id);Line1();public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMove(const short&deltaX,const
29、 short&deltaY)override;virtual bool IsHit(const int&x,const int&y)const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;public:virtual void SetStartPos(const int&posX,const int&posY)override;virtual void SetEndPos(const int&posX,const int&posY)override;
30、virtual void SetText(TCHAR*pText)override virtual bool IsPossibleToViewThis()const override;virtual int QueryInterface()const override return(GT_LINE|GT_LINE1);/This is a line1 class.private:int _posX0;int _posY0;int _posX1;int _posY1;class Line2:public Line public:Line2(TCHAR*pText,const int&posX0,
31、const int&posY0,const int&posX1,const int&posY1,const int&id);Line2();public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMove(const short&deltaX,const short&deltaY)override;virtual bool IsHi
32、t(const int&x,const int&y)const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;public:virtual void SetStartPos(const int&posX,const int&posY)override;virtual void SetEndPos(const int&posX,const int&posY)override;virtual void SetText(TCHAR*pText)overrid
33、e;virtual bool IsPossibleToViewThis()const override;virtual int QueryInterface()const override return(GT_LINE|GT_LINE2);/This is a line2 class.private:int _posX0;int _posY0;int _posX1;int _posY1;public:TCHAR*_pText;class Text1:public Text public:Text1(TCHAR*pText,const int&posX,const int&posY,const
34、int&id);Text1();public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMove(const short&deltaX,const short&deltaY)override;virtual bool IsHit(const int&x,const int&y)const override;virtual bool
35、Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;virtual int QueryInterface()const override return(GT_TEXT|GT_TEXT1);/This is a text1 class.public:virtual void SetFontSize(const int&fontSize)override;virtual void SetText(TCHAR*pText)override;private:int _posX;int _posY;int _f
36、ont_size;TCHAR*_pText;HFONT _hFont;class Text2:public Text public:Text2(TCHAR*pText,const int&posX,const int&posY,const int&id);Text2();public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMov
37、e(const short&deltaX,const short&deltaY)override;virtual bool IsHit(const int&x,const int&y)const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;virtual int QueryInterface()const override return(GT_TEXT|GT_TEXT2);/This is a text2 class.public:virtual v
38、oid SetFontSize(const int&fontSize)override;virtual void SetText(TCHAR*pText)override;private:int _posX;int _posY;int _font_size;TCHAR*_pText;HFONT _hFont;class Rectangle1:public Rectangle public:Rectangle1(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id);Rectangle1();pu
39、blic:virtual void SetStartPos(const int&posX,const int&posY)override;virtual void SetEndPos(const int&posX,const int&posY)override;public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMove(con
40、st short&deltaX,const short&deltaY)override;virtual bool IsHit(const int&x,const int&y)const override;virtual bool IsPossibleToViewThis()const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;virtual int QueryInterface()const override return(GT_RECTANGLE
41、|GT_RECTANGLE1);/This is a rectangle1 class.private:int _posX0;int _posY0;int _posX1;int _posY1;class Rectangle2:public Rectangle public:Rectangle2(const int&posX0,const int&posY0,const int&posX1,const int&posY1,const int&id);Rectangle2();public:virtual void SetStartPos(const int&posX,const int&posY
42、)override;virtual void SetEndPos(const int&posX,const int&posY)override;public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMove(const short&deltaX,const short&deltaY)override;virtual bool Is
43、Hit(const int&x,const int&y)const override;virtual bool IsPossibleToViewThis()const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;virtual int QueryInterface()const override return(GT_RECTANGLE|GT_RECTANGLE2);/This is a rectangle2 class.private:int _po
44、sX0;int _posY0;int _posX1;int _posY1;class Symbol1:public Symbol public:Symbol1(const int&posX,const int&posY,const int&id);Symbol1();public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pView,const HDC&hdc)const override;virtual void OnMove(
45、const short&deltaX,const short&deltaY)override;virtual bool IsHit(const int&x,const int&y)const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;virtual int QueryInterface()const override return(GT_SYMBOL|GT_SYMBOL1);/This is a symbol1 class.public:virtu
46、al void SetText(TCHAR*pText)override private:int _posX;int _posY;float _size;class Symbol2:public Symbol public:Symbol2(TCHAR*pText,const int&posX,const int&posY,const int&id);Symbol2();public:/Implementations:virtual void Draw(Client*pView,const HDC&hdc)const override;virtual void Update(Client*pVi
47、ew,const HDC&hdc)const override;virtual void OnMove(const short&deltaX,const short&deltaY)override;virtual bool IsHit(const int&x,const int&y)const override;virtual bool Save(HANDLE hFile)const override;virtual bool Load(HANDLE hFile)override;virtual int QueryInterface()const override return(GT_SYMB
48、OL|GT_SYMBOL2);/This is a symbol2 class.public:virtual void SetText(TCHAR*pText)override;private:int _posX;int _posY;float _size;TCHAR*_pText;/namespace ges 4.2.3.主函数中的实现代码 void ges:Client:Composite()std:vector pGraphics;HDC hdc;RECT rect;if(_tempGraphicObj2)(Picture*)_tempGraphicObj2)-GetGraphicsPt
49、rs(pGraphics);for(auto&pObj:pGraphics)Leave(pObj-GetID();(Picture*)_tempGraphicObj2)-SetReal(true);/Make it real Picture.Attach(_tempGraphicObj2);_tempGraphicObj2=nullptr;_optState=None;hdc=GetDC(_hWnd);/TODO:Painting client window GetClientRect(_hWnd,&rect);:Rectangle(hdc,rect.left,rect.top,rect.ri
50、ght,rect.bottom);Notify(hdc);/Release DC ReleaseDC(_hWnd,hdc);/Event:LButtonDown void ges:Client:OnLButtonDown(const WPARAM&wParam,const short&x,const short&y)int vX,vY;Graphic*pGraphic=nullptr;vX=(int)(float)(x-_clientRect.left)/_viewport.tS-_viewport.tX);vY=(int)(float)(y-_clientRect.top)/_viewpor