软件设计原则 - 第三周作业
发布于: 2020 年 06 月 24 日
作业一:手写单列模式
作业二:用组合模式编程输出
<?phpinterface IComponent { public function draw();}abstract class Container implements IComponent { protected $components = []; public function addComponent(IComponent $component) { $this->components[] = $component; } public function draw() { $this->parentDraw(); /*** @var IComponent $component */ foreach ($this->components as $component) { $component->draw(); } } protected abstract function parentDraw();}class Picture implements IComponent{ public function draw(){ echo "Picture".PHP_EOL;}}class WinForm extends Container{ protected function parentDraw(){ echo "WinForm".PHP_EOL;}}class Frame extends Container{ protected function parentDraw(){ echo "Frame".PHP_EOL;}}class Lable implements IComponent{ private $value; /** * Lable constructor. * @param $value */ public function __construct($value) { $this->value = $value; } public function draw(){ echo "Lable:".$this->value.PHP_EOL;}}class TextBox implements IComponent{ public function draw(){ echo "TextBox".PHP_EOL;}}class PasswordBox implements IComponent{ public function draw(){ echo "PasswordBox".PHP_EOL;}}class CheckBox implements IComponent{ public function draw(){ echo "CheckBox".PHP_EOL; }}class LinkLable implements IComponent{ private $value; /** * Lable constructor. * @param $value */ public function __construct($value) { $this->value = $value; } public function draw(){ echo "LinkLable:".$this->value.PHP_EOL; }}class Buttion implements IComponent { private $value; /** * Lable constructor. * @param $value */ public function __construct($value) { $this->value = $value; } public function draw(){ echo "buttion:".$this->value.PHP_EOL;}}$winform = new WinForm();$winform->addComponent(new Picture());$frame = new Frame();$frame->addComponent(new Lable('用户名'));$frame->addComponent(new TextBox());$frame->addComponent(new Lable('密码'));$frame->addComponent(new PasswordBox());$frame->addComponent(new CheckBox());$frame->addComponent(new LinkLable("忘记密码"));$winform->addComponent($frame);$winform->addComponent(new Buttion('登录'));$winform->addComponent(new Buttion('注册'));$winform->draw();
输出结果为:
WinForm
Picture
Frame
Lable:用户名
TextBox
Lable:密码
PasswordBox
CheckBox
LinkLable:忘记密码
buttion:登录
buttion:注册
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 35
孙志平
关注
还未添加个人签名 2018.05.08 加入
还未添加个人简介
评论