热搜词
发表于 2022-4-24 16:22:44 | 显示全部楼层 |阅读模式
本文实例介绍了PHP简单投诉页面的实现代码,分享给大家供大家参考,具体内容如下

php代码:

';

}

final protected function __clone() {undefined

return false;

}

public function conn() {undefined

echo 'Mysql连接成功

';

}

}

/**

* 工厂模式

*/

interface Factory {undefined

function createDB();

}

class MysqlFactory implements Factory {undefined

public function createDB() {undefined

echo 'Mysql工厂创建成功

';

return MysqlSingle::getInstance();

}

}

/**

* 根据用户名显示不同风格

* 观察者模式

*/

class Observer implements SplSubject {undefined

protected $_observers = NULL;

public $_style = NULL;

public function __construct($style) {undefined

$this->_style = $style;

$this->_observers = new SplObjectStorage();

}

public function show() {undefined

$this->notify();

}

public function attach(SplObserver $observer) {undefined

$this->_observers->attach($observer);

}

public function detach(SplObserver $observer) {undefined

$this->_observers->detach($observer);

}

public function notify() {undefined

$this->_observers->rewind();

while ($this->_observers->valid()) {undefined

$observer = $this->_observers->current();

$observer->update($this);

$this->_observers->next();

}

}

}

class StyleA implements SplObserver {undefined

public function update(SplSubject $subject) {undefined

echo $subject->_style . ' 模块A

';

}

}

class StyleB implements SplObserver {undefined

public function update(SplSubject $subject) {undefined

echo $subject->_style . ' 模块B

';

}

}

/**

* 根据不同方式进行投诉

* 桥接模式

*/

class Bridge {undefined

protected $_obj = NULL;

public function __construct($obj) {undefined

$this->_obj = $obj;

}

public function msg($type) {undefined

}

public function show() {undefined

$this->msg();

$this->_obj->msg();

}

}

class BridgeEmail extends Bridge {undefined

public function msg() {undefined

echo 'Email>>';

}

}

class BridgeSms extends Bridge {undefined

public function msg() {undefined

echo 'Sms>>';

}

}

class Normal {undefined

public function msg() {undefined

echo 'Normal

';

}

}

class Danger {undefined

public function msg() {undefined

echo 'Danger

';

}

}

/**

* 适配器模式

*/

class Serialize {undefined

public $content = NULL;

public function __construct($content) {undefined

$this->content = serialize($content);

}

public function show() {undefined

return '序列化格式:

' . $this->content;

}

}

class JsonAdapter extends Serialize {undefined

public function __construct($content) {undefined

parent::__construct($content);

$tmp = unserialize($this->content);

$this->content = json_encode($tmp, TRUE);

}

public function show() {undefined

return 'Json格式:

' . $this->content;

}

}

/**

* 在投诉内容后自动追加

* 装饰器模式

*/

class Base {undefined

protected $_content = NULL;

public function __construct($content) {undefined

$this->_content = $content;

}

public function getContent() {undefined

return $this->_content;

}

}

class Decorator {undefined

private $_base = NULL;

public function __construct(Base $base) {undefined

$this->_base = $base;

}

public function show() {undefined

return $this->_base->getContent() . '>>系统时间:' . date('Y-m-d H:i:s', time());

}

}

/**

* 分级举报处理功能

* 责任链模式

*/

class level1 {undefined

protected $_level = 1;

protected $_top = 'Level2';

public function deal($level) {undefined

if ($level <= $this->_level) {undefined

echo '处理级别:1

';

return;

}

$top = new $this->_top;

$top->deal($level);

}

}

class level2 {undefined

protected $_level = 2;

protected $_top = 'Level3';

public function deal($level) {undefined

if ($level <= $this->_level) {undefined

echo '处理级别:2

';

return;

}

$top = new $this->_top;

$top->deal($level);

}

}

class level3 {undefined

protected $_level = 3;

protected $_top = 'Level2';

public function deal($level) {undefined

echo '处理级别:3

';

return;

}

}

if (!empty($_POST)) {undefined

echo '

PHP设计模式';

//连接数据库——工厂+单例模式

$mysqlFactory = new MysqlFactory();

$single = $mysqlFactory->createDB();

$single->conn();

echo '

';

//观察者模式

$username = $_POST['username'];

$ob = new Observer($username);

$a = new StyleA();

$ob->attach($a);

$b = new StyleB();

$ob->attach($b);

$ob->show();

echo '

';

$ob->detach($b);

$ob->show();

echo '

';

//桥接模式

$typeM = $_POST['typeM'];

$typeN = 'Bridge' . $_POST['typeN'];

$obj = new $typeN(new $typeM);

$obj->show();

echo '

';

//适配器模式

$post = $_POST;

$obj = new Serialize($post);

echo $obj->show();

echo '

';

$json = new JsonAdapter($post);

echo $json->show();

echo '

';

echo '

';

//装饰器模式

$content = $_POST['content'];

$decorator = new Decorator(new Base($content));

echo $decorator->show();

echo '

';

//责任链模式

echo '

';

$level = $_POST['level'];

$deal = new Level1();

$deal->deal(intval($level));

return;

}

require("0.html");

html代码:

PHP设计模式用户名

Tom

Lily

投诉方式

Normal

Danger

Email

Sms

处理级别

1

2

3

投诉内容

提交

来源:https://blog.csdn.net/weixin_31063289/article/details/115622465

全部评论0
回复
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|管理员之家 ( 苏ICP备2023053177号-2 )

GMT+8, 2024-11-24 17:35 , Processed in 0.150603 second(s), 22 queries .

Powered by Discuz! X3.5

Cpoyright © 2001-2024 Discuz! Team