架构师训练营 -- 第三周作业
发布于: 2020 年 06 月 24 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
语言: PHP
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
目录结构
抽象组件类 Component.php
<?php/** * Created by phpStorm * User: Kevin Xiang * Date: 2020/6/24 * Time: 1:32 PM */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"); }}
容器类 Container.php
<?php/** * Created by phpStorm * User: Kevin Xiang * Date: 2020/6/24 * Time: 3:15 PM */abstract class Container extends Component { protected $lists = []; public function __construct($type, $name) { parent::__construct($type, $name); } public function add(Component $component) :void { $this->lists[] = $component; } public function display() { parent::display(); foreach ($this->lists as $name => $field) { $field->display(); } }}
其余组件类
<?phpclass 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); }}
结构输出 index.php
<?php/** * Created by phpStorm * User: Kevin Xiang * Date: 2020/6/24 * Time: 9:16 AM */// 自动加载类文件spl_autoload_register(function ($class_name = "Label") { $class_name = str_replace('\\','/',$class_name); require_once __DIR__."/composite_pattern/".$class_name . ".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();
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 52
版权声明: 本文为 InfoQ 作者【_MISSYOURLOVE】的原创文章。
原文链接:【http://xie.infoq.cn/article/efd7620975e78c8e4cb401a47】。未经作者许可,禁止转载。
这个人很懒,还没有介绍过自己~ 2019.04.28 加入
这个懒人,还没有添加过简介~
评论