作业

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

1.单例实现



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



1.show.php

<?php
include 'WinFrom.php';
$win = new WinForm('WinForm', 'WINDOW窗口');
$win->add(new Picture('Picture', 'LOGO图片'));
$win->add(new Button('Button', '登录'));
$win->add(new Button('Button', '注册'));
$frame = new Frame('Frame', 'FRAME1');
$frame->add(new Label('Label', '用户名'));
$frame->add(new TextBox('TextBox', '文本框'));
$frame->add(new Label('Label', '密码'));
$frame->add(new PasswordBox('Password', '密码框'));
$frame->add(new CheckBox('CheckBox', '复选框'));
$frame->add(new TextBox('TextBox', '记住用户名'));
$frame->add(new LinkLabel('LinkLabel', '忘记密码'));
$win->add($frame);
$win->display();



2.WinForm.php

<?php
include 'Container.php';
class WinForm extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class Picture extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class Button extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class Frame extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class Label extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class TextBox extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class PasswordBox extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class CheckBox extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}
class LinkLabel extends Container
{
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
}



3.Component.php

<?php
abstract class Component
{
protected $type;
protected $name;
function __construct($type, $name)
{
$this->type = $type;
$this->name = $name;
}
abstract public function add(Component $component);
public function display()
{
printf("print $this->type($this->name)\n");
}
}



4.Container.php

<?php
include 'Component.php';
abstract class Container extends Component
{
protected $lists = [];
public function __construct($type, $name)
{
parent::__construct($type, $name);
}
public function add(Component $component)
{
$this->lists[] = $component;
}
public function display()
{
parent::display();
foreach ($this->lists as $name => $field) {
$field->display();
}
}
}



用户头像

chenzt

关注

还未添加个人签名 2018.05.15 加入

还未添加个人简介

评论

发布
暂无评论
作业