架构师 0 期第三周作业(命题作业)

用户头像
何伟敏
关注
发布于: 2020 年 06 月 24 日
  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

实现语言:C++

单例模式实现要点:

(1)构造函数声明私有

(2)需要有一个静态的获取函数

(3)new的时候需要加锁,避免多线程出错





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





代码:

实现语言:C++

//控件的抽象类
class Renderable {
public:
Renderable(std::string name) :__name(name) {}
virtual ~Renderable() {}
virtual void Render() = 0;
protected:
std::string __name;
};
//画布的基类,同时也是控件类的子类
class Canvas : public Renderable {
public:
Canvas(std::string name):Renderable(name) {}
void AddController(Renderable* renderer) { __controllerList.push_back(renderer); }
void Render() {
std::list<Renderable*>::iterator itr = __controllerList.begin();
for (; itr != __controllerList.end(); itr++) {
(*itr)->Render();
}
}
private:
std::list<Renderable*> __controllerList;
};
class WinForm : public Canvas {
public:
WinForm(std::string name):Canvas(name) { }
virtual ~WinForm() {}
virtual void Render() {
std::cout << "prinf WinForm( "<<__name<<")"<<std::endl;
Canvas::Render();
}
};
class Picture :public Renderable {
public:
Picture(std::string name) :Renderable(name) {}
virtual ~Picture() {}
virtual void Render() {
std::cout << "prinf Picture( " << __name << ")" << std::endl;
}
};
class Button :public Renderable {
public:
Button(std::string name) :Renderable(name) {}
virtual ~Button() {}
virtual void Render() {
std::cout << "prinf Button( " << __name << ")" << std::endl;
}
};
class Label :public Renderable {
public:
Label(std::string name) :Renderable(name) {}
virtual ~Label() {}
virtual void Render() {
std::cout << "prinf Label( " << __name << ")" << std::endl;
}
};
class TextBox :public Renderable {
public:
TextBox(std::string name) :Renderable(name) {}
virtual ~TextBox() {}
virtual void Render() {
std::cout << "prinf TextBox( " << __name << ")" << std::endl;
}
};
class PasswordBox :public Renderable {
public:
PasswordBox(std::string name) :Renderable(name) {}
virtual ~PasswordBox() {}
virtual void Render() {
std::cout << "prinf PasswordBox( " << __name << ")" << std::endl;
}
};
class CheckBox :public Renderable {
public:
CheckBox(std::string name) :Renderable(name) {}
virtual ~CheckBox() {}
virtual void Render() {
std::cout << "prinf CheckBox( " << __name << ")" << std::endl;
}
};
class Linkable :public Renderable {
public:
Linkable(std::string name) :Renderable(name) {}
virtual ~Linkable() {}
virtual void Render() {
std::cout << "prinf Linkable( " << __name << ")" << std::endl;
}
};
class Frame :public Canvas {
public:
Frame(std::string name) :Canvas(name) { }
virtual ~Frame() {}
virtual void Render() {
std::cout << "prinf WinForm( " << __name << ")" << std::endl;
Canvas::Render();
}
};
int main()
{
WinForm myWin("WINDOW窗口");
Picture logoPic("LOGO图片");
Button loginBtn("登录");
Button registerBtn("注册");
Frame frame1("Frame1");
Label usernameLb("用户名");
TextBox usernameTb("文本框");
Label passwordLb("密码");
PasswordBox passwordTb("密码框");
CheckBox multiSelect("复选框");
TextBox rememberTb("记住用户名");
Linkable forget("忘记密码");
frame1.AddController(&usernameLb);
frame1.AddController(&usernameTb);
frame1.AddController(&passwordLb);
frame1.AddController(&passwordTb);
frame1.AddController(&multiSelect);
frame1.AddController(&rememberTb);
frame1.AddController(&forget);
myWin.AddController(&logoPic);
myWin.AddController(&loginBtn);
myWin.AddController(&registerBtn);
myWin.AddController(&frame1);
myWin.Render();
}

运行结果:



用户头像

何伟敏

关注

还未添加个人签名 2018.03.11 加入

还未添加个人简介

评论

发布
暂无评论
架构师 0 期第三周作业(命题作业)