写点什么

架构师训练营第三周作业

用户头像
fenix
关注
发布于: 2020 年 06 月 24 日

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

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



这是代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Component {
public:
Component(string type, string name) : _type(type), _name{name} {}
virtual void draw(ostream &os) {
os << "print " << _type << "(" << _name << ")" << endl;
}
private:
string _type;
string _name;
};
class Button : public Component {
public:
Button(string name) : Component("Button", name) {}
};
class Picture : public Component {
public:
Picture(string name) : Component("Picture", name) {}
};
class Label : public Component {
public:
Label(string name) : Component("Label", name) {}
};
class TextBox : public Component {
public:
TextBox(string name) : Component("TextBox", name) {}
};
class PasswordBox : public Component {
public:
PasswordBox(string name) : Component("PasswordBox", name) {}
};
class CheckBox : public Component {
public:
CheckBox(string name) : Component("CheckBox", name) {}
};
class LinkLable : public Component {
public:
LinkLable(string name) : Component("LinkLable", name) {}
};
class Composite : public Component {
public:
Composite(string type, string name) : _list{}, Component(type, name) {}
virtual void draw(ostream &os) override {
Component::draw(os);
for (auto p : _list) {
p->draw(os);
}
}
void addComponent(Component *component) {
_list.push_back(component);
}
~Composite() {
for (auto p : _list) {
delete p;
}
}
private:
vector<Component*> _list;
};
class WinForm : public Composite {
public:
WinForm(string name) : Composite("WinForm", name) {}
};
class Frame : public Composite {
public:
Frame(string name) : Composite("Frame", name) {}
};
int main() {
Composite* p_win = new WinForm("WINDOW窗口");
Composite* p_frame = new Frame("FRAME1");
p_win->addComponent(new Picture("LOGO图片"));
p_win->addComponent(new Button("登录"));
p_win->addComponent(new Button("注册"));
p_win->addComponent(p_frame);
p_frame->addComponent(new Label("用户名"));
p_frame->addComponent(new TextBox("文本框"));
p_frame->addComponent(new Label("密码"));
p_frame->addComponent(new PasswordBox("密码框"));
p_frame->addComponent(new CheckBox("复选框"));
p_frame->addComponent(new TextBox("记住用户名"));
p_frame->addComponent(new LinkLable("忘记密码"));
p_win->draw(cout);
delete p_win;
return 0;
}



这是输出结果



用户头像

fenix

关注

还未添加个人签名 2018.03.02 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第三周作业