<?php
/**
* @author jinliang
* @date 2013-1-2
* @file b.class.php
*/
//单例设计
class singleton{
private static $s = array();
private function __construct(){
}
public static function getInstance($classname){
if(empty(self::$s[$classname])){
self::$s[$classname] = new $classname();
}
return self::$s[$classname];
}
public function printInfo(){
echo get_called_class();
}
}
//工厂方法模式
interface Sender{
public function send();
}
class MailSend implements Sender{
public function send(){
echo "send mail";
}
}
class SmsSend implements Sender{
public function send(){
echo "send sms";
}
}
class SendFactory {
//private static $send;
public static function produce($type){
if($type == 'mail') {
return new MailSend();
} else if($type == 'sms') {
return new SmsSend();
} else {
return '请输入正确的类型';
}
}
}
//代理模式
interface Person {
public function eat();
}
class Baby implements Person{
private $name;
public function __construct($name){
$this->name = $name;
}
public function eat(){
echo $this->name."吃钣";
}
}
class Mother implements Person{
private $baby;
public function __construct($baby){
$this->baby = $baby;
}
private function eatBefor(){
echo "吃之前";
}
private function eatAfter(){
echo "吃之后";
}
public function eat(){
$this->eatBefor();
$this->baby->eat();
$this->eatAfter();
}
}
//适配器模式
interface TypeA{
public function insert();
}
interface TypeB{
public function connect();
}
class TypeAimpl implements TypeA{
public function insert(){
echo "Start insert<br/>";
}
}
class TypeBimpl implements TypeB{
public function connect(){
echo "Start connect<br/>";
}
}
class createA{
private $typeA;
public function __construct($typeA){
$this->typeA = $typeA;
}
public function dotypeA(){
echo "other...things<br/>";
$this->typeA->insert();
}
}
class Adapter implements TypeA{
private $typeB;
public function __construct($typeB){
$this->typeB = $typeB;
}
public function insert(){
$this->typeB->connect();
}
}
$typeAimpl = new TypeAimpl();
$typeBimpl = new TypeBimpl();
$Adapter = new Adapter($typeBimpl);
$createA = new createA($typeAimpl);
$createB = new createA($Adapter);
$createA->dotypeA();
$createB->dotypeA();