架构师训练营作业 (第三周)
发布于: 2020 年 06 月 24 日
作业一
请在草稿纸上手写一个单例模式的实现代码(拍照提交作业)
 
 单例模式-php
作业二
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3
 
 类图:
 
 abstract class FormComponent{    protected $name;
    public function __construct($name) {        $this->name = $name;    }
    public function printName() {        echo "Print ".get_class($this)."(".$this->name.")\n";    }}
class FormComposite extends FormComponent{    protected $elementAry = [];
    public function add(FormComponent $element) {       $this->elementAry[] = $element;    }
    public function getList() {       return $this->elementAry;    }}
class WinForm extends FormComposite{}
class Frame extends FormComposite{}
class Pictrue extends FormComponent{}
class Button extends FormComponent{}class Lable extends FormComponent{}
class TextBox extends FormComponent{}
class PasswordBox extends FormComponent{}
class CheckBox extends FormComponent{}
class LinkLable extends FormComponent{}
class Client{    private function getFormInfo() {        $winform = new WinForm("WINDOW窗口");
        $logo = new Pictrue("Logo图片");        $login = new Button("登录");        $register = new Button("注册");        $frame = new Frame("FRAME1");
        $username = new Lable("用户名");        $input = new TextBox("文本框");        $passwd = new Lable("密码");        $passinput = new PasswordBox("密码框");        $checkbox = new CheckBox("复选框");        $tips = new TextBox("记住用户名");        $linklable = new LinkLable("忘记密码");
        $frame->add($username);        $frame->add($input);        $frame->add($passwd);        $frame->add($passinput);        $frame->add($checkbox);        $frame->add($tips);        $frame->add($linklable);
        $winform->add($logo);        $winform->add($login);        $winform->add($register);        $winform->add($frame);
        return $winform;    }
     private function showTree( $root) {        $root->printName();
        $list = $root->getList();        foreach ($list as $item) {            if ($item instanceof FormComposite) {                $this->showTree($item);            }else {                $item->printName();            }        }    }
    public function run() {        $root = $this->getFormInfo();        $this->showTree($root);    }}
$cli = new Client();$cli->run();复制代码
 划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 57
版权声明: 本文为 InfoQ 作者【默默】的原创文章。
原文链接:【http://xie.infoq.cn/article/23936f95c7cd4fd77ddece740】。文章转载请联系作者。

默默
关注
还未添加个人签名 2018.09.17 加入
还未添加个人简介











 
    
评论