<?php
interface shape
{
public function draw();
}
class square implements shape
{
function draw()
{
echo "square::draw()";
}
}
class circle implements shape
{
function draw()
{
echo "circle::draw()";
}
}
interface color
{
public function fill();
}
class red implements color
{
function fill()
{
echo "red::fill()";
}
}
class blue implements color
{
function fill()
{
echo "blue::fill()";
}
}
abstract class abstractFactory
{
abstract function getColor($color);
abstract function getShape($shape);
}
class shapeFactory extends abstractFactory
{
public function getColor($color)
{ }
public function getShape($shape)
{
if ($shape == 'circle') {
return new circle();
} else if ($shape == 'square') {
return new square();
}
}
}
class colorFactory extends abstractFactory
{
public function getColor($color)
{
if ($color == 'red') {
return new red();
} else if ($color == 'blue') {
return new blue();
}
}
public function getShape($shape)
{ }
}
class factoryProducer
{
public static function getFactory($choice)
{
if ($choice == 'color') {
return new colorFactory;
} else if ($choice == 'shape') {
return new shapeFactory;
}
}
}
$shapeFac = factoryProducer::getFactory('shape');
$shape = $shapeFac->getShape('circle');
$shape->draw();
$colorFac = factoryProducer::getFactory('color');
$color = $colorFac->getColor('red');
$color->fill();
<?php
interface packing
{
public function pack();
}
class bottle implements packing
{
public function pack()
{
echo "-bottle-";
}
}
class wrapper implements packing
{
public function pack()
{
echo "-wrapper-";
}
}
interface item
{
public function name();
public function packing();
public function price();
}
abstract class Burger implements item
{
public function packing()
{
return new wrapper();
}
}
abstract class drink implements item
{
public function packing()
{
return new bottle();
}
}
class vegBurger extends Burger
{
public function name()
{
echo "vegBurger";
}
public function price()
{
echo 0.2;
}
}
class chickenBurger extends Burger
{
public function name()
{
echo "checkenBurger";
}
public function price()
{
echo 0.1;
}
}
class Pepsi extends drink
{
public function price()
{
echo 0.3;
}
public function name()
{
echo "Pepsi";
}
}
class Coke extends drink
{
public function price()
{
echo 0.4;
}
public function name()
{
echo "Coke";
}
}
class meal
{
private $list;
public function addItem(item $item)
{
$this->list[] = $item;
}
public function getcost()
{
$cost = 0;
foreach ($this->list as $item) {
$cost += $item->price();
}
return $cost;
}
}
class Builder
{
public function prepareVegBurger()
{
$meal = new meal();
$meal->addItem(new vegBurger);
$meal->addItem(new Pepsi);
return $meal;
}
}
(new Builder())->prepareVegBurger()->getcost();
<?php
interface shape
{
public function draw();
}
class rectangle implements shape
{
public function draw()
{
echo "shape:rectangle\n";
}
}
class circle implements shape
{
public function draw()
{
echo "shape:circle\n";
}
}
abstract class shapeDecorate implements shape
{
protected $decorateShape;
public function __construct(shape $decorateShape)
{
$this->decorateShape = $decorateShape;
}
public function draw()
{
$this->decorateShape->draw();
}
}
class redShapeDecorator extends shapeDecorate
{
public function __construct(shape $shape)
{
parent::__construct($shape);
}
public function draw()
{
$this->decorateShape->draw();
$this->setRedBorder($this->decorateShape);
}
public function setRedBorder(shape $decorateShape)
{
echo "Border Color: Red\n";
}
}
$redRectangle = new redShapeDecorator(new rectangle);
$redRectangle->draw();
<?php
interface shape
{
public function draw();
}
class Rectangle implements shape
{
public function draw()
{
echo "Rectangle::draw()";
}
}
class Circle implements shape
{
public function draw()
{
echo "Circle::draw()";
}
}
class shapeFactory
{
public function getShape($shape)
{
if ($shape == 'circle') {
return new Circle();
} else if ($shape == 'rectangle') {
return new Rectangle();
} else {
echo 'error';
return false;
}
}
}
$factory = new shapeFactory;
$shap = $factory->getShape('circle');
$shap->draw();
<?php
abstract class subject
{
private $observers = array();
public function addObserver(observer $observer)
{
$this->observers[] = $observer;
echo "添加观察者成功\n";
}
public function delObserver(observer $observer)
{
$key = array_search($observer, $this->observers);
if ($observer === $this->observers[$key]) {
unset($this->observers[$key]);
echo "删除成功";
} else {
echo "不存在";
}
}
public function notifyObservers()
{
foreach ($this->observers as $observer) {
$observer->update();
}
}
}
class server extends subject
{
public function publish()
{
echo "发现更新包";
$this->notifyObservers();
}
}
interface observer
{
public function update();
}
class wechat implements observer
{
public function update()
{
echo "微信更新";
}
}
class web implements observer
{
public function update()
{
echo "web更新";
}
}
class app implements observer
{
public function update()
{
echo "app更新";
}
}
$server = new server;
$server->addObserver(new web);
$server->addObserver(new app);
$server->addObserver(new wechat);
$server->publish();
<?php
abstract class prototype
{
abstract function __clone();
}
class map extends prototype
{
public $width;
public $height;
public $sea;
public function setAttribute(array $attributes)
{
foreach ($attributes as $key => $val) {
$this->$key = $val;
}
}
public function __clone()
{ }
}
class sea
{
public $color = 'red';
public function setcolor($color)
{
$this->color = $color;
}
}
$map_prototype = new map();
$attributes = array('width' => 40, 'height' => 60, 'sea' => (new sea()));
$map_prototype->setAttribute($attributes);
$new_map = clone $map_prototype;
var_dump($new_map);
var_dump($map_prototype);
$map_prototype->sea->setcolor('blue');
var_dump($map_prototype);
var_dump($new_map);
class Map1 extends Prototype
{
public $clone_id = 0;
public $width;
public $height;
public $sea;
public function setAttribute(array $attributes)
{
foreach ($attributes as $key => $val) {
$this->$key = $val;
}
}
public function __clone()
{
$this->sea = clone $this->sea;
}
}
<?php
interface strategy
{
public function doOperation($num1, $num2);
}
class add implements strategy
{
public function doOperation($num1, $num2)
{
return $num1 + $num2;
}
}
class multiply implements strategy
{
public function doOperation($num1, $num2)
{
return $num1 * $num2;
}
}
class substract implements strategy
{
public function doOperation($num1, $num2)
{
return $num1 - $num2;
}
}
class context
{
private $strategy;
public function __construct(strategy $strategy)
{
$this->strategy = $strategy;
}
public function executeStrategy($num1, $num2)
{
return $this->strategy->doOperation($num1, $num2);
}
}
$context = new context(new add());
echo $context->executeStrategy(20, 3);
<?php
$a = array("Dog", "Cat", "Horse", 'AAA', 'BBB', 'CCC', 'DDD', 'EEE');
print_r(
array_reduce($a, function ($v1, $v2) {
var_dump($v1);
return $v1 . "-" . $v2;
}, 'initValue')
);
interface Middleware
{
public static function handle(Closure $next);
}
class VerfiyCsrfToken implements Middleware
{
public static function handle(Closure $next)
{
echo "Token";
$next();
}
}
class VerfiyAuth implements Middleware
{
public static function handle(Closure $next)
{
echo "Auth";
$next();
}
}
class SetCookie implements Middleware
{
public static function handle(Closure $next)
{
$next();
echo "Cookie";
}
}
function call_middware()
{
SetCookie::handle(function () {
VerfiyAuth::handle(function () {
VerfiyCsrfToken::handle(function () {
echo "执行";
});
});
});
}
call_middware();
$pop_arr = [
'VerfiyCsrfToken',
'VerfiyAuth',
'SetCookie'
];
$handle = function () {
echo '当前要执行的程序!';
};
$callback = array_reduce($pop_arr, function ($stack, $pipe) {
return function () use ($stack, $pipe) {
return $pipe::handle($stack);
};
}, $handle);
call_user_func($callback);