php面试之设计模式

//观察者模式
//适用场景:订阅者通知
//自己对观察者模式对理解:
需求:有事情变化你要通知我
实现:
    1、要通知对人必须在我这里注册,否则我不知道要通知谁。
    2、想要获取通知的人必须遵守我的规则,实现我指定的通知事件,不然我没办法统一通知,(比如高考查分数,只支持电话查询,你非要微信查我肯定通知不到你)
    3、事件发生的时候我会逐一通知你们。
ob_servers[] = $ob_server;
    }

    //通知
    public function notify()
    {
        if(!empty($this->ob_servers))
        {
            foreach($this->ob_servers as $ob_server)
            {
                $ob_server->update();
            }
        }
    }
}

//定义观察者接口
interface obServer{
    public function update($event_info = null);
}

class obServer1 implements obServer{
    public function update($event_info = null){
        echo "观察者1 收到执行通知\n";
    }
}

class obServer2 implements obServer{
    public function update($event_info = null){
        echo "观察者2 收到执行通知\n";
    }
}


class event extends genEvent{

    //事件触发
    public function trigger()
    {
        $this->notify();
    }
}

//实现
$event = new event();
$event->addObs(new obServer1());
$event->addObs(new obServer2());
$event->trigger();

?>
//简单工厂模式
//适用场景:创建对象比较少,业务逻辑不太复杂
createBike("bike");
$bike->product("hello,bike");

$car = $factory->createBike("car");
$car->product("hello,car");

?>

适配模式

adaptee = $adaptee;
    }

  
    //委派调用Adaptee的sampleMethod1方法  
    public function doMouthOpen()  
    {  
        $this->adaptee->openMouth();  
    }  
  
    public function doMouthClose()  
    {  
        $this->adaptee->closeMouth();  
    }  

}

//组成绿色遥控
class greenAdapter implements greenTarget
{
    private $adapter;
    //初始化对象
    public function __contruct(Toy $adapter)
    {
        $this->adapter = $adapter;
    }

    public function operateMouth($type = 0)
    {
        $type ? $this->adapter->openMouth() : $this->adapter->closeMouth();
    }
}

//测试
class testDriver
{
    public function run()
    {
        //实例化玩具狗
        $dog = new dog();
        $adapter_dog = new redAdapter($dog);
        $adapter_dog->doMouthOpen();
        $adapter_dog->doMouthClose();
    }   
}

$test =  new testDriver();
$test->run();
?>
DB类适配模式
conn = mysql_connect($host,$user,$passwd);
        mysql_select_db($dbname);
        return $this->conn;
    }

    public function query($sql)
    {
        $res = mysql_query($sql,$this->conn);
        return $res;
    }

    public function close()
    {
        mysql_close($this->conn);
    }
}

//pdo
class newPDO implements DBInterface{
    protected $conn;
    public function connect($host,$user,$passwd,$dbname)
    {
        $this->conn = new Pdo("mysql:host=$host;dbname=$dbname",$user,$passwd);
    }

    public function query($sql)
    {
        return $this->conn->query($sql);
    }

    public function close()
    {
        unset($this->conn);
    }
}


//测试
// $db = new MySQL();
// $rs = $db->connect('127.0.0.1','mysql','nopass','activity');
// $total = $db->query("select count(*) from activity_cron");


$db = new newPDO();
$rs = $db->connect('127.0.0.1','mysql','nopass.2','activity');
$total = $db->query("select count(*) from activity_cron");

?>
//策略模式
setStrategy = $userStrategy;
    }

    public function index(){
        $this->setStrategy->showAd();
        $this->setStrategy->showCa();
    }
}

$a = new show();
$sex = 'f';
if($sex == 'f')
{
    $sf = new femaleStrategy();
    $a->setStrategy($sf);
    $a->index();
}
else
{
    $sf = new maleStrategy();
    $a->setStrategy($sf);
    $a->index();
}
?>
//状态模式,积分等级判断
score > 0 && $a->score < 1000)
        {
            echo "{$a->score}点积分,黄金会员";
        }
        else
        {
            //超出了等级
            $a->setState(new platinum());
            $a->grade();
        }
    }
}

class platinum implements istate{
    public function grade(State $a)
    {
        if($a->score >= 1000 && $a->score < 2000)
        {
            echo "{$a->score}点积分,白金会员";
        }
        else
        {
            //超出了等级
            $a->setState(new diamond());
            $a->grade();
        }
    }
}

class diamond implements istate{
    public function grade(State $a)
    {
        if($a->score >= 2000 )
        {
            echo "{$a->score}点积分,钻石会员";
        }
    }
}

class state
{
    private $first;
    public function __construct()
    {
        $this->first = new golden();
    }

    public function setState(istate $obj)
    {
        $this->first = $obj;
    }

    public function grade()
    {
        $this->first->grade($this);
    }
}

$b = new state();
$b->score = 1000;
$b->grade();

?>
//装饰模式
trackList = [];
    }

    public function addTrack($track)
    {
        $this->trackList = $track;
    }

    public function getTrack()
    {
        $output = "";
        foreach($this->trackList as $key=>$val)
        {
            $output .= ($key+1) . ":{$val}";
        }

        return $output;
    }
}


//变化后的需求,需要把所有的输出采用大写
class cdToUpper {
    private $_cd;

    public function __construct(CD $CD)
    {
        $this->_cd = $CD;
    }

    public function makeCaps()
    {
        foreach($this->_cd->trackList as $key=>$val)
        {
            $this->_cd->trackList[$key] = strtoupper($val);
        }
    }
}

//实例化
$my_cd = new CD();
$trackList = [
    'what is your name',
    'hanmeimei',
    'lilei',
];

//初始化
$my_cd->addTrack($trackList);
$cd_to_upper = new cdToUpper($my_cd);
$cd_to_upper->makeCaps();
$list = $my_cd->getTrack();
echo '
';
print_r($list);exit;
?>

 

你可能感兴趣的:(存库)