架构训练营第三章课后作业单例模式 组合模式

发布于: 2020 年 06 月 23 日

1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。

GUI 实现有3个重点在GUI树形结构组织、刷新绘制机制、事件响应机制

#include "stdafx.h"

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include <string>

#include <vector>

#define Uint16 unsigned int

class sonoWidget

{

public:

sonoWidget(const std::string & type,const std::string & caption)

{

m_Caption = caption;

m_Type = type;

}

virtual bool addWidget()

{

return true;

}

virtual void draw()

{

return;

}

virtual sonoWidget::~ sonoWidget()

{

}

protected:

std::string m_Caption;//组件标题

std::string m_Type;//组件类型

};

//叶子

class sonoLeafWidget : public sonoWidget

{

public:

sonoLeafWidget(const std::string & type,const std::string & caption):sonoWidget(type,caption)

{

}

virtual ~ sonoLeafWidget()

{

}

virtual void draw()

{

printf("print %s (%s)\n",mType.cstr(),mCaption.cstr());

}

virtual bool addWidget(sonoWidget * win)

{

return true;

}

};

//容器

class sonoContainer : public sonoWidget

{

public:

sonoContainer(const std::string & type,const std::string & caption):sonoWidget(type,caption)

{

}

virtual ~ sonoContainer()

{

for (Uint16 i = 0; i < m_Children.size(); i++)

{

delete m_Children[i];

}

m_Children.clear();

}

virtual void draw()

{

printf("print %s (%s)\n",mType.cstr(),mCaption.cstr());

for (Uint16 i = 0; i < m_Children.size(); i++)

{

m_Children[i]->draw();

}

}

virtual bool addWidget(sonoWidget * win)

{

if (!win || win == this)

return false;

std::vector<sonoWidget *>::iterator p = std::find(mChildren.begin(), mChildren.end(), win);

if (p == m_Children.end())

{

mChildren.pushback(win);

return true;

}

return false;

}

private:

std::vector<sonoWidget *> m_Children;

};

int tmain(int argc, TCHAR* argv[])

{

sonoContainer *winMainForm = new sonoContainer("WinForm","WINDOW窗口");

//login 界面

sonoLeafWidget * picLogopic = new sonoLeafWidget("Picture","LOGO图片");

sonoLeafWidget * btnLogin = new sonoLeafWidget("Button","登录");

sonoLeafWidget * btnRegister = new sonoLeafWidget("Button","注册");

//login 用户信息输入界面

sonoContainer *frameFrame = new sonoContainer("Frame","FRAME1");

winMainForm->addWidget(picLogopic);

winMainForm->addWidget(btnLogin);

winMainForm->addWidget(btnRegister);

winMainForm->addWidget(frameFrame);

sonoLeafWidget * lblUserName = new sonoLeafWidget("Label","用户名");

sonoLeafWidget * txtboxUserName = new sonoLeafWidget("TextBox","文本框");

sonoLeafWidget * lblPassWord = new sonoLeafWidget("Label","密码");

sonoLeafWidget * txtBoxPassWord = new sonoLeafWidget("PassWordBox","密码框");

sonoLeafWidget * chkBoxRememberUser = new sonoLeafWidget("CheckBox","复选框");

sonoLeafWidget * txtboxRememberUser = new sonoLeafWidget("TextBox","记忆用户");

sonoLeafWidget * llblForgetPassWord = new sonoLeafWidget("LinkLabel","忘记密码");

frameFrame->addWidget(lblUserName);

frameFrame->addWidget(txtboxUserName);

frameFrame->addWidget(lblPassWord);

frameFrame->addWidget(txtBoxPassWord);

frameFrame->addWidget(chkBoxRememberUser);

frameFrame->addWidget(txtboxRememberUser);

frameFrame->addWidget(llblForgetPassWord);

//绘制输出

winMainForm->draw();

delete winMainForm;

winMainForm=NULL;

printf("end\n");

return 0;

}

用户头像

周冬辉

关注

还未添加个人签名 2020.04.14 加入

还未添加个人简介

评论

发布
暂无评论
架构训练营第三章课后作业单例模式 组合模式