第三周作业

发布于: 2020 年 06 月 24 日

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

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

//组件抽象类
abstract class ComponentBase
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
//添加组件
abstract function add(ComponentBase $c);
//显示组件
abstract function show();
}
class WinForm extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
//防止添加重名组件
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
//显示子组件
foreach($this->item as $value){
$value->show();
}
}
}
class Picture extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}
class Button extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}
class Frame extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
foreach($this->item as $value){
$value->show();
}
}
}
class Lable extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}
class TextBox extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}
class PasswordBox extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}
class CheckBox extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}
class LinkLable extends ComponentBase
{
protected $item = [];
public function add(ComponentBase $c)
{
$nodeName = $c->getName();
if (!isset($this->item[$nodeName])) {
$this->item[$nodeName] = $c;
} else {
throw new Exception("该页面组件已存在,组件名称:" . $nodeName);
}
}
public function show()
{
echo $this->name;
echo "\n";
}
}

//初始化窗口
$form = new WinForm('WINODW窗口');
//初始化FRAME
$frame = new Frame('FRAME1');
$form->add(new Picture('LOGO图片'));
$form->add(new Button('登录'));
$form->add(new Button('注册'));
$frame->add(new Lable('用户名'));
$frame->add(new TextBox('文本框'));
$frame->add(new Lable('密码'));
$frame->add(new PasswordBox('密码框'));
$frame->add(new CheckBox('复选框'));
$frame->add(new TextBox('记住用户名'));
$frame->add(new LinkLable('忘记密码'));
$form->add($frame);
$form->show();

执行结果:

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

关注

还未添加个人签名 2018.05.10 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业