写点什么

架构师训练 - 第三周作业

用户头像
X﹏X
关注
发布于: 2020 年 06 月 23 日

作业一

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






作业二

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



输出结果

代码

main.cpp

#include "box.hpp"
#include "button.hpp"
#include "label.hpp"
#include "picture.hpp"
#include "frame.hpp"
#include "winform.hpp"
int main(int argc, char**argv) {
delete (new WinForm("WINDOW窗口"))
->add(new Picture("LOGO图片"))
->add(new Button("登录"))
->add(new Button("注册"))
->add((new Frame("框架"))
->add(new Label("用户名"))
->add(new TextBox("文本框"))
->add(new Label("密码"))
->add(new PasswordBox("密码框"))
->add(new CheckBox("复选框"))
->add(new TextBox("记住用户名"))
->add(new LinkLabel("忘记密码")))
->print(1);
return 0;
}

base.hpp

#ifndef __BASE_HPP_
#define __BASE_HPP_
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Base {
public :
Base(std::string name) : name(name) {}
virtual ~Base() {}
virtual Base* print(int depth) = 0;
virtual Base* add(Base* base) = 0;
protected:
std::string name;
};
class Leaf : public Base {
public:
Leaf(std::string name) : Base(std::move(name)) {}
virtual ~Leaf() {}
Base* print(int depth) {
cout << std::string(depth, '-') << name.c_str() << endl;
return this;
}
private:
Base * add(Base* base) final {
return nullptr;
}
};
class Node : public Base {
public :
Node(std::string name) : Base(name) {}
virtual ~Node() {
for (auto it = list.begin(); it != list.end(); ++it) {
delete (*it);
}
}
Base* print(int depth) {
cout << std::string(depth, '-') << name.c_str() << endl;
for (auto it = list.begin(); it != list.end(); ++it) {
(*it)->print(depth + 1);
}
return this;
}
Base* add(Base* base) {
list.push_back(base);
return this;
}
private:
std::vector<Base*> list;
};
#endif

box.hpp

#ifndef __BOX_HPP_
#define __BOX_HPP_
#include "base.hpp"
class Box : public Leaf {
public:
Box(std::string name) : Leaf(std::move(std::string("Box::") + name)) {}
virtual ~Box() {}
};
class PasswordBox : public Box {
public:
PasswordBox(std::string name) : Box(std::move(std::string("PasswordBox::") + name)) {}
virtual ~PasswordBox() {}
};
class CheckBox : public Box {
public:
CheckBox(std::string name) : Box(std::move(std::string("CheckBox::") + name)) {}
virtual ~CheckBox() {}
};
class TextBox : public Box {
public:
TextBox(std::string name) : Box(std::move(std::string("TextBox::") + name)) {}
virtual ~TextBox() {}
};
#endif

button.hpp

#ifndef __BUTTON_HPP_
#define __BUTTON_HPP_
#include "base.hpp"
class Button : public Leaf {
public:
Button(std::string name) : Leaf(std::move(std::string("Button::") + name)) {}
virtual ~Button() {}
};
#endif

frame.hpp

// frame.hpp
#ifndef __FRAME_HPP_
#define __FRAME_HPP_
#include "base.hpp"
class Frame : public Node {
public:
Frame(std::string name) : Node(std::move(std::string("Frame::") + name)) {}
virtual ~Frame() {}
};
#endif // !__FRAME_HPP_

label.hpp

#ifndef __LABEL_HPP_
#define __LABEL_HPP_
#include "base.hpp"
class Label : public Leaf {
public:
Label(std::string name) : Leaf(std::move(std::string("Label::") + name)) {}
virtual ~Label() {}
};
class LinkLabel : public Label {
public:
LinkLabel(std::string name) : Label(std::move(std::string("LinkLabel::") + name)) {}
virtual ~LinkLabel() {}
};
#endif // !__LABEL_HPP_

picture.hpp

#ifndef __PICTURE_HPP_
#define __PICTURE_HPP_
#include "base.hpp"
class Picture : public Leaf {
public:
Picture(std::string name) : Leaf(std::move(std::string("Picture::") + name)) {}
virtual ~Picture() {}
};
#endif // !__PICTURE_HPP_

winform.hpp

#ifndef __WINFORM_HPP_
#define __WINFORM_HPP_
#include "base.hpp"
class WinForm : public Node {
public:
WinForm(std::string name) : Node(std::move(std::string("WinForm::") + name)) {}
virtual ~WinForm() {}
};
#endif // !__WINFORM_HPP_



发布于: 2020 年 06 月 23 日阅读数: 53
用户头像

X﹏X

关注

还未添加个人签名 2018.04.25 加入

还未添加个人简介

评论

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