PHP设计模式之简单投诉页面实例
本文实例介绍了PHP简单投诉页面的实现代码,分享给大家供大家参考,具体内容如下
php代码:
<?php
/*
*设计模式练习
*1.数据库连接类(单例模式)
*2.调用接口实现留言本功能(工厂模式)
*3.实现分级举报处理功能(责任链模式)
*4.发送不同组合的举报信息(桥接模式)
*5.发送不同格式的举报信息(适配器模式)
*6.在投诉内容后自动追加时间(装饰器模式)
*7.根据会员登录信息变换显示风格(观察者模式)
*8.根据发帖长度加经验值(策略模式)
*/
interfaceDB{
functionconn();
}
/**
*单例模式
*/
classMysqlSingleimplementsDB{
protectedstatic$_instance=NULL;
publicstaticfunctiongetInstance(){
if(!self::$_instanceinstanceofself){
self::$_instance=newself;
}
returnself::$_instance;
}
finalprotectedfunction__construct(){
echo'Mysql单例创建成功<br>';
}
finalprotectedfunction__clone(){
returnfalse;
}
publicfunctionconn(){
echo'Mysql连接成功<br>';
}
}
/**
*工厂模式
*/
interfaceFactory{
functioncreateDB();
}
classMysqlFactoryimplementsFactory{
publicfunctioncreateDB(){
echo'Mysql工厂创建成功<br>';
returnMysqlSingle::getInstance();
}
}
/**
*根据用户名显示不同风格
*观察者模式
*/
classObserverimplementsSplSubject{
protected$_observers=NULL;
public$_style=NULL;
publicfunction__construct($style){
$this->_style=$style;
$this->_observers=newSplObjectStorage();
}
publicfunctionshow(){
$this->notify();
}
publicfunctionattach(SplObserver$observer){
$this->_observers->attach($observer);
}
publicfunctiondetach(SplObserver$observer){
$this->_observers->detach($observer);
}
publicfunctionnotify(){
$this->_observers->rewind();
while($this->_observers->valid()){
$observer=$this->_observers->current();
$observer->update($this);
$this->_observers->next();
}
}
}
classStyleAimplementsSplObserver{
publicfunctionupdate(SplSubject$subject){
echo$subject->_style.'模块A<br>';
}
}
classStyleBimplementsSplObserver{
publicfunctionupdate(SplSubject$subject){
echo$subject->_style.'模块B<br>';
}
}
/**
*根据不同方式进行投诉
*桥接模式
*/
classBridge{
protected$_obj=NULL;
publicfunction__construct($obj){
$this->_obj=$obj;
}
publicfunctionmsg($type){
}
publicfunctionshow(){
$this->msg();
$this->_obj->msg();
}
}
classBridgeEmailextendsBridge{
publicfunctionmsg(){
echo'Email>>';
}
}
classBridgeSmsextendsBridge{
publicfunctionmsg(){
echo'Sms>>';
}
}
classNormal{
publicfunctionmsg(){
echo'Normal<br>';
}
}
classDanger{
publicfunctionmsg(){
echo'Danger<br>';
}
}
/**
*适配器模式
*/
classSerialize{
public$content=NULL;
publicfunction__construct($content){
$this->content=serialize($content);
}
publicfunctionshow(){
return'序列化格式:<br>'.$this->content;
}
}
classJsonAdapterextendsSerialize{
publicfunction__construct($content){
parent::__construct($content);
$tmp=unserialize($this->content);
$this->content=json_encode($tmp,TRUE);
}
publicfunctionshow(){
return'Json格式:<br>'.$this->content;
}
}
/**
*在投诉内容后自动追加
*装饰器模式
*/
classBase{
protected$_content=NULL;
publicfunction__construct($content){
$this->_content=$content;
}
publicfunctiongetContent(){
return$this->_content;
}
}
classDecorator{
private$_base=NULL;
publicfunction__construct(Base$base){
$this->_base=$base;
}
publicfunctionshow(){
return$this->_base->getContent().'>>系统时间:'.date('Y-m-dH:i:s',time());
}
}
/**
*分级举报处理功能
*责任链模式
*/
classlevel1{
protected$_level=1;
protected$_top='Level2';
publicfunctiondeal($level){
if($level<=$this->_level){
echo'处理级别:1<br>';
return;
}
$top=new$this->_top;
$top->deal($level);
}
}
classlevel2{
protected$_level=2;
protected$_top='Level3';
publicfunctiondeal($level){
if($level<=$this->_level){
echo'处理级别:2<br>';
return;
}
$top=new$this->_top;
$top->deal($level);
}
}
classlevel3{
protected$_level=3;
protected$_top='Level2';
publicfunctiondeal($level){
echo'处理级别:3<br>';
return;
}
}
if(!empty($_POST)){
echo'<h1>PHP设计模式</h1>';
//连接数据库——工厂+单例模式
$mysqlFactory=newMysqlFactory();
$single=$mysqlFactory->createDB();
$single->conn();
echo'<br>';
//观察者模式
$username=$_POST['username'];
$ob=newObserver($username);
$a=newStyleA();
$ob->attach($a);
$b=newStyleB();
$ob->attach($b);
$ob->show();
echo'<br>';
$ob->detach($b);
$ob->show();
echo'<br>';
//桥接模式
$typeM=$_POST['typeM'];
$typeN='Bridge'.$_POST['typeN'];
$obj=new$typeN(new$typeM);
$obj->show();
echo'<br>';
//适配器模式
$post=$_POST;
$obj=newSerialize($post);
echo$obj->show();
echo'<br>';
$json=newJsonAdapter($post);
echo$json->show();
echo'<br>';
echo'<br>';
//装饰器模式
$content=$_POST['content'];
$decorator=newDecorator(newBase($content));
echo$decorator->show();
echo'<br>';
//责任链模式
echo'<br>';
$level=$_POST['level'];
$deal=newLevel1();
$deal->deal(intval($level));
return;
}
require("0.html");
html代码:
<!DOCTYPEhtml>
<!--
Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.
Tochangethistemplatefile,chooseTools|Templates
andopenthetemplateintheeditor.
-->
<html>
<head>
<title>PHP设计模式</title>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<style>
div{border:solidgray1px;margin-top:10px;height:100px;width:200px;}
</style>
</head>
<body>
<formaction="0.php"method="post">
<h1>用户名</h1>
<selectid="username"name="username">
<optionvalue="Tom">Tom</option>
<optionvalue="Lily">Lily</option>
</select>
<h1>投诉方式</h1>
<selectid="type"name="typeM">
<optionvalue="Normal">Normal</option>
<optionvalue="Danger">Danger</option>
</select>
<selectid="type"name="typeN">
<optionvalue="Email">Email</option>
<optionvalue="Sms">Sms</option>
</select>
<h1>处理级别</h1>
<selectid="level"name="level">
<optionvalue="1">1</option>
<optionvalue="2">2</option>
<optionvalue="3">3</option>
</select>
<h1>投诉内容</h1>
<textareaid="content"name="content"rows="3"></textarea>
<buttontype="submit">提交</button>
</form>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助。