# 第一题 手写单例模式代码
# 第二题 组合模式的代码练习
## 题目
## 设计
## 代码实现
//----cat Button.h-------
/**
* Project Untitled
*/
#ifndef _BUTTON_H
#define _BUTTON_H
#include "WindowComponent.h"
class Button:public WindowComponent {
public:
Button(string str):WindowComponent(str){}
void display();
};
#endif //_BUTTON_H
//----cat Button.cpp-------
/**
* Project Untitled
*/
#include "Button.h"
/**
* Button implementation
*/
/**
* @return void
*/
void Button::display() {
cout<<"print Button("<<content<<")"<<endl;
return;
}
//----cat CheckBox.h-------
/**
* Project Untitled
*/
#ifndef _CHECKBOX_H
#define _CHECKBOX_H
#include "WindowComponent.h"
class CheckBox:public WindowComponent {
public:
CheckBox(string str):WindowComponent(str){}
void display();
};
#endif //_CHECKBOX_H
//----cat CheckBox.cpp-------
/**
* Project Untitled
*/
#include "CheckBox.h"
/**
* CheckBox implementation
*/
/**
* @return void
*/
void CheckBox::display() {
cout<<"print CheckBox("<<content<<")"<<endl;
return;
}
//----cat ComponentList.h-------
/**
* Project Untitled
*/
#ifndef _COMPONENTLIST_H
#define _COMPONENTLIST_H
#include "WindowComponent.h"
#include <list>
#include <memory>
using std::list;
using std::unique_ptr;
class ComponentList:public WindowComponent {
public:
ComponentList(string str):WindowComponent(str){}
virtual ~ComponentList(){
}
virtual void display() = 0;
/**
* @param com
*/
void AddComponent(unique_ptr<WindowComponent>&& com);
protected:
list < unique_ptr<WindowComponent> > vec;
};
#endif //_COMPONENTLIST_H
//----cat ComponentList.cpp-------
/**
* Project Untitled
*/
#include "ComponentList.h"
/**
* ComponentList implementation
*/
/**
* @return void
*/
void ComponentList::display() {
for(const auto& i:vec)
i->display();
return;
}
/**
* @param com
*/
void ComponentList::AddComponent(unique_ptr<WindowComponent>&& com) {
vec.push_back(std::move(com));
}
//----cat Frame.h-------
/**
* Project Untitled
*/
#ifndef _FRAME_H
#define _FRAME_H
#include "ComponentList.h"
class Frame:public ComponentList {
public:
Frame(string str):ComponentList(str){}
void display();
};
#endif //_FRAME_H
//----cat Frame.cpp-------
/**
* Project Untitled
*/
#include "Frame.h"
/**
* Frame implementation
*/
/**
* @return void
*/
void Frame::display() {
cout<<"print Frame("<<content<<")"<<endl;
ComponentList::display();
return;
}
//----cat LinkLable.h-------
/**
* Project Untitled
*/
#ifndef _LINKLABLE_H
#define _LINKLABLE_H
#include "WindowComponent.h"
class LinkLable:public WindowComponent {
public:
LinkLable(string str):WindowComponent(str){}
void display();
};
#endif //_LINKLABLE_H
//----cat LinkLable.cpp-------
/**
* Project Untitled
*/
#include "LinkLable.h"
/**
* LinkLable implementation
*/
/**
* @return void
*/
void LinkLable::display() {
cout<<"print LinkLable("<<content<<")"<<endl;
return;
}
//----cat PasswordBox.h-------
/**
* Project Untitled
*/
#ifndef _PASSWORDBOX_H
#define _PASSWORDBOX_H
#include "WindowComponent.h"
class PasswordBox:public WindowComponent {
public:
PasswordBox(string str):WindowComponent(str){}
void display();
};
#endif //_PASSWORDBOX_H
//----cat PasswordBox.cpp-------
/**
* Project Untitled
*/
#include "PasswordBox.h"
/**
* PasswordBox implementation
*/
/**
* @return void
*/
void PasswordBox::display() {
cout<<"print PasswordBox("<<content<<")"<<endl;
return;
}
//----cat Picture.h-------
/**
* Project Untitled
*/
#ifndef _PICTURE_H
#define _PICTURE_H
#include "WindowComponent.h"
class Picture:public WindowComponent {
public:
Picture(string str):WindowComponent(str){}
void display();
};
#endif //_PICTURE_H
//----cat Picture.cpp-------
/**
* Project Untitled
*/
#include "Picture.h"
/**
* Picture implementation
*/
/**
* @return void
*/
void Picture::display() {
cout<<"print Picture("<<content<<")"<<endl;
return;
}
//----cat SimpleLabel.h-------
/**
* Project Untitled
*/
#ifndef _SIMPLELABEL_H
#define _SIMPLELABEL_H
#include "WindowComponent.h"
class SimpleLabel:public WindowComponent {
public:
SimpleLabel(string str):WindowComponent(str){}
void display();
};
#endif //_SIMPLELABEL_H
//----cat SimpleLabel.cpp-------
/**
* Project Untitled
*/
#include "SimpleLabel.h"
/**
* SimpleLabel implementation
*/
/**
* @return void
*/
void SimpleLabel::display() {
cout<<"print Label("<<content<<")"<<endl;
return;
}
//----cat TextBox.h-------
/**
* Project Untitled
*/
#ifndef _TEXTBOX_H
#define _TEXTBOX_H
#include "WindowComponent.h"
class TextBox:public WindowComponent {
public:
TextBox(string str):WindowComponent(str){}
void display();
};
#endif //_TEXTBOX_H
//----cat TextBox.cpp-------
/**
* Project Untitled
*/
#include "TextBox.h"
/**
* TextBox implementation
*/
/**
* @return void
*/
void TextBox::display()
{
cout<<"print TextBox("<<content<<")"<<endl;
return;
}
//----cat WindowComponent.h-------
/**
* Project Untitled
*/
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
#ifndef _WINDOWCOMPONENT_H
#define _WINDOWCOMPONENT_H
class WindowComponent {
public:
WindowComponent(string str):content(str) {}
virtual ~WindowComponent(){}
virtual void display() = 0;
protected:
string content;
};
#endif //_WINDOWCOMPONENT_H
//----cat WindowComponent.cpp-------
/**
* Project Untitled
*/
#include "WindowComponent.h"
/**
* WindowComponent implementation
*/
/**
* @return void
*/
void WindowComponent::display() {
return;
}
//----cat WinForm.h-------
/**
* Project Untitled
*/
#ifndef _WINFORM_H
#define _WINFORM_H
#include "ComponentList.h"
class WinForm:public ComponentList {
public:
WinForm(string str):ComponentList(str) {}
void display();
};
#endif //_WINFORM_H
//----cat WinForm.cpp-------
/**
* Project Untitled
*/
#include "WinForm.h"
/**
* WinForm implementation
*/
/**
* @return void
*/
void WinForm::display() {
cout<<"print WinForm("<<content<<")"<<endl;
ComponentList::display();
return;
}
//----cat main.cpp--------
#include <iostream>
#include <memory>
#include "Button.h"
#include "CheckBox.h"
#include "ComponentList.h"
#include "Frame.h"
#include "LinkLable.h"
#include "PasswordBox.h"
#include "Picture.h"
#include "SimpleLabel.h"
#include "TextBox.h"
#include "WindowComponent.h"
#include "WinForm.h"
#include<utility>
template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&&... params)
{
return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}
int main()
{
unique_ptr<ComponentList> root = make_unique<WinForm>("WINDOWS窗口");
root->AddComponent(make_unique<Picture>("LOGO图片"));
root->AddComponent(make_unique<Button>("登录"));
root->AddComponent(make_unique<Button>("注册"));
unique_ptr<ComponentList> container = make_unique<Frame>("FRAME1");
container->AddComponent(make_unique<SimpleLabel>("用户名"));
container->AddComponent(make_unique<TextBox>("文本框"));
container->AddComponent(make_unique<SimpleLabel>("密码"));
container->AddComponent(make_unique<PasswordBox>("密码框"));
container->AddComponent(make_unique<CheckBox>("复选框"));
container->AddComponent(make_unique<TextBox>("记住用户名"));
container->AddComponent(make_unique<LinkLable>("忘记密码"));
root->AddComponent(std::move(container));
root->display();
return 0;
}