架构师训练营 - 第三章 - 作业
发布于: 2020 年 06 月 24 日
好吧,我承认,我是个 php 程序员(*/ω\*)
组合模式作业代码
<?php
interface Elements
{
public function print();
}
class Window implements Elements
{
private $elementsList = [];
private $windowName;
public function __construct($windowName)
{
$this->windowName = $windowName;
}
public function addElements(Elements $element)
{
$this->elementsList[] = $element;
}
public function print()
{
echo 'window name:'. $this->windowName. "\n";
foreach($this->elementsList as $oneElement) {
$oneElement->print();
}
}
}
class Picture implements Elements
{
private $pictureName;
public function __construct(string $pictureName)
{
$this->pictureName = $pictureName;
}
public function print()
{
echo 'picture: '.$this->pictureName. "\n";
}
}
class Button implements Elements
{
private $buttonName;
public function __construct(string $buttonName)
{
$this->buttonName = $buttonName;
}
public function print()
{
echo 'button name:'. $this->buttonName. "\n";
}
}
class Label implements Elements
{
private $labelName;
public function __construct(string $labelName)
{
$this->labelName = $labelName;
}
public function print()
{
echo 'label name:'. $this->labelName. "\n";
}
}
class Frame implements Elements
{
private $elementsList = [];
private $frameName;
public function __construct($frameName)
{
$this->frameName = $frameName;
}
public function addElements(Elements $element)
{
$this->elementsList[] = $element;
}
public function print()
{
echo 'frame name:'. $this->frameName. "\n";
foreach($this->elementsList as $oneElement) {
$oneElement->print();
}
}
}
class TextBox implements Elements
{
private $textboxName;
public function __construct(string $textboxName)
{
$this->textboxName = $textboxName;
}
public function print()
{
echo 'textbox name:'. $this->textboxName. "\n";
}
}
class PasswordBox implements Elements
{
private $passwordBox;
public function __construct(string $passwordBox)
{
$this->passwordBox = $passwordBox;
}
public function print()
{
echo 'passwordbox name:'. $this->passwordBox. "\n";
}
}
class CheckBox implements Elements
{
private $checkBox;
public function __construct(string $checkBox)
{
$this->checkBox = $checkBox;
}
public function print()
{
echo 'checkbox name:'. $this->checkBox. "\n";
}
}
class LinkLable implements Elements
{
private $linklableName;
public function __construct(string $linklableName)
{
$this->linklableName = $linklableName;
}
public function print()
{
echo 'linklableName name:'. $this->linklableName. "\n";
}
}
//组装
$window = new Window("WINDOW 窗口");
$logoPicture = new Picture("LOGO图片");
$window->addElements($logoPicture);
$frame = new Frame("FRAME1");
$userNameLabel = new Label("用户名");
$userNameTextbox = new TextBox("用户名文本框");
$passordLabel = new Label("密码");
$passwordPasswordBox = new PasswordBox("密码输入框");
$rememberPassword = new CheckBox("记住密码复选框");
$rememberLabel = new Label("记住密码");
$forgetPasswordLink = new LinkLable("忘记密码");
$frame->addElements($userNameLabel);
$frame->addElements($userNameTextbox);
$frame->addElements($passordLabel);
$frame->addElements($passwordPasswordBox);
$frame->addElements($rememberPassword);
$frame->addElements($rememberLabel);
$frame->addElements($forgetPasswordLink);
$window->addElements($frame);
$loginButton = new Button("登录");
$registerButton = new Button("注册");
$window->addElements($loginButton);
$window->addElements($registerButton);
$window->print();
复制代码
代码对应的 uml 类图:
组合模式的理解:
组合模式可以封装一个对象内部的复杂逻辑,将对象的复杂性限制在一个单一的类中,不让客户类感知到。
这样做,增强了代码的内聚性,可以避免客户端类因为和复杂的被调用类内部对象交互而自己变的复杂。
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 50
而立
关注
还未添加个人签名 2018.01.25 加入
还未添加个人简介
评论