装饰模式:

<?php
abstract class Tile {
abstract function getWealthFactor();
}
class Plains extends Tile {
private $wealthfactor = ;
function getWealthFactor() {
return $this->wealthfactor;
}
}
abstract class TileDecorator extends Tile { // 装饰
protected $tile;
function __construct( Tile $tile ) {
$this->tile = $tile;
}
}
class DiamondDecorator extends TileDecorator { // 钻石装饰
function getWealthFactor() {
return $this->tile->getWealthFactor()+;
}
}
class PollutionDecorator extends TileDecorator { // 污染装饰
function getWealthFactor() {
return $this->tile->getWealthFactor()-;
}
}
$tile = new Plains();
print $tile->getWealthFactor(); //
$tile = new DiamondDecorator( new Plains());
print $tile->getWealthFactor(); //
$tile = new PollutionDecorator( new DiamondDecorator( new Plains()));
print $tile->getWealthFactor(); //
?>

组合模式:

<?php
abstract class Unit {
abstract function bombardStrength();
}
class Archer extends Unit {
function bombardStrength() {
return ;
}
}
class LaserCannonUnit extends Unit {
function bombardStrength() {
return ;
}
}
class Army {
private $units = array();
private $armies= array();
function addUnit( Unit $unit ) {
array_push( $this->units, $unit );
}
function addArmy( Army $army ) {
array_push( $this->armies, $army );
}
function bombardStrength() {
$ret = ;
foreach( $this->units as $unit ) {
$ret += $unit->bombardStrength();
}
foreach( $this->armies as $army ) {
$ret += $army->bombardStrength();
}
return $ret;
}
}
$unit1 = new Archer();
$unit2 = new LaserCannonUnit();
$army = new Army();
$army->addUnit( $unit1 );
$army->addUnit( $unit2 );
print $army->bombardStrength();
print "\n";
$army2 = clone $army; // 克隆军队
$army->addArmy( $army2 );
print $army->bombardStrength();
print "\n";
?>

工厂模式

 <?php
/**
* 操作类
* 因为包含有抽象方法,所以类必须声明为抽象类
*/
abstract class Operation{
//抽象方法不能包含函数体
abstract public function getValue($num1,$num2);//强烈要求子类必须实现该功能函数
}
/**
* 加法类
*/
class OperationAdd extends Operation {
public function getValue($num1,$num2){
return $num1+$num2;
}
}
/**
* 减法类
*/
class OperationSub extends Operation {
public function getValue($num1,$num2){
return $num1-$num2;
}
}
/**
* 乘法类
*/
class OperationMul extends Operation {
public function getValue($num1,$num2){
return $num1*$num2;
}
}
/**
* 除法类
*/
class OperationDiv extends Operation {
public function getValue($num1,$num2){
try {
if ($num2==){
throw new Exception("除数不能为0");
}else {
return $num1/$num2;
}
}catch (Exception $e){
echo "错误信息:".$e->getMessage();
}
} /**
* 工厂类,主要用来创建对象
* 功能:根据输入的运算符号,工厂就能实例化出合适的对象
*
*/
class Factory{
public static function createObj($operate){
switch ($operate){
case '+':
return new OperationAdd();
break;
case '-':
return new OperationSub();
break;
case '*':
return new OperationSub();
break;
case '/':
return new OperationDiv();
break;
}
}
}
$test=Factory::createObj('/');
$result=$test->getValue(,);
echo $result;
?>

单例模式

<?php  

/**
* Singleton of Database
*/
class Database
{
// We need a static private variable to store a Database instance.
privatestatic $instance; // Mark as private to prevent it from being instanced.
private function__construct()
{
// Do nothing.
} private function__clone()
{
// Do nothing.
} public static function getInstance()
{
if (!(self::$instanceinstanceofself)) {
self::$instance = newself();
} returnself::$instance;
}
} $a =Database::getInstance();
$b =Database::getInstance(); // true
var_dump($a === $b);

策略模式

<?php
interface FlyBehavior{
public function fly();
} class FlyWithWings implements FlyBehavior{
public function fly(){
echo "Fly With Wings \n";
}
} class FlyWithNo implements FlyBehavior{
public function fly(){
echo "Fly With No Wings \n";
}
}
class Duck{
private $_flyBehavior;
public function performFly(){
$this->_flyBehavior->fly();
} public function setFlyBehavior(FlyBehavior $behavior){
$this->_flyBehavior = $behavior;
}
} class RubberDuck extends Duck{
}
// Test Case
$duck = new RubberDuck(); /* 想让鸭子用翅膀飞行 */
$duck->setFlyBehavior(new FlyWithWings());
$duck->performFly(); /* 想让鸭子不用翅膀飞行 */
$duck->setFlyBehavior(new FlyWithNo());
$duck->performFly(); ?>

适配器模式

class User {      

    private $name;      

    function __construct($name) {      

        $this->name = $name;      

    }      

    public function getName() {      

        return $this->name;      

    }      

}
//新代码,开放平台标准接口 interface UserInterface { function getUserName(); } class UserInfo implements UserInterface { protected $user; function __construct($user) { $this->user = $user; } public function getUserName() { return $this->user->getName(); } }
$olduser = new User('张三'); echo $olduser->getName()."n"; $newuser = new UserInfo($olduser); echo $newuser->getUserName()."n";

PHP常用设计模式汇总的更多相关文章

  1. Java常用英语汇总(面试必备)

    Java常用英语汇总(面试必备) abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存 ...

  2. 20145222《信息安全系统设计基础》Linux常用命令汇总

    学习Linux时常用命令汇总 通过Ctrl+f键可在该网页搜索到你想要的命令. Linux中命令格式为:command [options] [arguments] //中括号代表是可选的,即有些命令不 ...

  3. Oozie命令行常用命令汇总[转]

    Oozie命令行常用命令汇总 有时候脚本跑多了就不愿意在OozieWeb端去看脚本的运行情况了.还好Oozie提供了很多命令行命令.能通过命令行直接检索自己想看到的脚本信息.在这里简单进行一下总结.一 ...

  4. Android常用设计模式(二)

    Android常用设计模式之观察者模式 观察者设计模式在Android应用中会经常用到,模式原理类似于这样的场景: 用户订报纸,然后在报社登记,报社来统计用户(添加用户),用户也可以取消订阅,报社删除 ...

  5. 代码重构 & 常用设计模式

    代码重构 重构目的 相同的代码最好只出现一次 主次方法 主方法 只包含实现完整逻辑的子方法 思维清楚,便于阅读 次方法 实现具体逻辑功能 测试通过后,后续几乎不用维护 重构的步骤 1  新建一个方法 ...

  6. IOS开发常用设计模式

    IOS开发常用设计模式 说起设计模式,感觉自己把握不了笔头,所以单拿出iOS开发中的几种常用设计模式谈一下. 单例模式(Singleton) 概念:整个应用或系统只能有该类的一个实例 在iOS开发我们 ...

  7. php常用函数汇总

    php常用函数汇总   字符串截取:           1.substr('要截取的字符串','从第几个字符开始','到第几个字符结束');             * 截取英文或者数字       ...

  8. JavaScript之Array常用函数汇总

    [20141121]JavaScript之Array常用功能汇总 *:first-child { margin-top: 0 !important; } body>*:last-child { ...

  9. vim常用命令汇总

    vim常用命令汇总: http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html 定位 本行第一个字符 ctrl+$ ...

随机推荐

  1. Redis Cluster 伪集群的搭建

    简介 为何要搭建Redis集群?Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系统的Hbase或者是MogoDB ...

  2. 获取百度搜索结果的真实url以及摘要和时间

    利用requests库和bs4实现,demo如下: #coding:utf- import requests from bs4 import BeautifulSoup import bs4 impo ...

  3. JavaScript的作用域与闭包

    JavaScript的作用域以函数为界,不同的函数拥有相对独立的作用域.函数内部可以声明和访问全局变量,也可以声明局部变量(使用var关键字,函数的参数也是局部变量),但函数外部无法访问内部的局部变量 ...

  4. vue安装vuex框架

    1.安装vuex npm install vuex --save-dev 2.创建storesrc下创建stores文件夹,创建noteStore.js import Vue from 'vue'; ...

  5. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤

    For many programs, developing a mathematical model of running timereduces to the following steps:■De ...

  6. 算法Sedgewick第四版-第1章基础-024-M/M/1 queue

    /****************************************************************************** * Compilation: javac ...

  7. p2657 windy数

    传送门 分析 首先这是一个询问一段区间内的个数的问题,所以我们可以用差分的思想用sum(R)-sum(L-1).然后我们考虑如何求出sum(n),我们用dp[i][j][k][t]表示考虑到第i位,最 ...

  8. Luogu 4777 【模板】扩展中国剩余定理(EXCRT)

    复习模板. 两两合并同余方程 $x\equiv C_{1} \ (Mod\ P_{1})$ $x\equiv C_{2} \ (Mod\ P_{2})$ 把它写成不定方程的形式: $x = C_{1} ...

  9. 关于IO流---笔记1

    今日内容介绍1.File2.递归=================================================================================1 I ...

  10. 关于 windows mobile 进程操作的方法

    #region Process class /// <summary> /// Summary description for Process. /// </summary> ...