//在原对象中添加
function __clone(){
$this->对象 = clone $this->对象
}
abstract class Animal{
public static function create(){
//实例化调用类
return new static();
}
}
class Person extends Animal{...}
//返回Person实例化类
Person::create();
//Person委托Person_Writer类处理打印事务.
class Person {
private $writer;
...
function __call($method_name,$args){
if(methood_exists($this->wirter,$method_name)){
return $this->writer->method_name($this);
}
}
//高级__call写法,当委托方法参数不确定时使用.
function __call($method_name,$args){
//当然这里这样写法意义不大,但是call一般都是用call_user_func_array调用
$args = $this ;
if(methood_exists($this->wirter,$method_name)){
return call_user_func_array(
array($this->writer,$method_name),$args);
)
}
}
}
//Product
class Product {
public $name;
public $price;
}
//Product_Sale
class Product_Sale {
private $callbacks;
//记录回调函数
function register_callback ($callback) {
if(! is_callback($callback)){
thow new Exception('callback not callable');
}
$this->callbacks[] = $callback;
}
//执行回调函数
function sale ($product){
print "{$product->name} : 处理中 \n";
foreach($this->callbacks as $callback){
call_user_func($callback , $product);
}
}
}
//Produce_Totalizer
class Produce_Totalizer {
static function warn_amount ($amt) {
$count = 0;
return function ($produce) use ($amt , &count) {
$count += $produce->price;
print " count : {count}\n"
if($count>$amt){
print "超过指定金额{$amt}啦~";
}
};
}
}
//模拟场景
$product_sale = new Produce_Sale();
//指定报警金额为8块
$product_sale = register_callback(Produce_Totalizer::warn_amount(8));
//卖商品
$product_sale->sale(new Product("Durex",6));
$product_sale->sale(new Produce("Jissbon",5));
//输出结果
Durex : 处理中
count :6
Jissbon : 处理中
count: 11
超过指定金额8块啦~
abstract class Cost_Strategy {
protected $duration;
abstract function cost ();
abstract function charge_type();
public __construct($duration){
$this->duration = $duration;
}
}
class Timed_Const_Strategy extends Cost_Stratedy {
function cost () {
//上一次课给5块钱- -.
return $this->duration * 5;
}
function charge_type(){
return "多次课结算";
}
}
class Fixed_Const_Strategy extends Cost_Stratedy {
function cost (){
return 30 ;
}
function charge_type(){
return "一次性课结算";
}
}
abstract class Leason {
private $cost_strategy;
public __construct(Const_Strategy $cost_strategy){
$this->cost_strategy = $cost_strategy;
}
function __call($method_name,$args){
$args = $cost_strategy ;
if(methood_exists($this->cost_strategy,$method_name)){
return call_user_func_array(
array($this->writer,$method_name),$args);
)
}
}
}
//运用
$leasons[] = new Seminar(new Timed_Const_Strategy(4));
$leasons[] = new Lecture(new Fixed_Const_Strategy(null));
foreach ($leasons as $leason){
print "leason charge : {$leason->const()}";
print "charge_type : {$leason->charge_type()}"
}
leason charge 20. charge_type : 多次课结算;
leason charge 30. charge_type : 一次课结算;
class Preferences {
private static $instance;
private function __construct(){ ... }
public static function get_instance(){
if(empty(self::$instance)){
self::$instance = new Preferences();
}
return self::$instance;
}
...
}
//使用
$preferences = Preferences::get_instance();
abstract class Show_Evernote {
abstract function get_header_text();
abstract function get_context();
abstract function get_footer_text();
abstract function get_user();
abstract function get_comment();
}
class 显示新浪微博 extends Show_Evernote{
function get_header_text(){...};
function get_context(){new 新浪微博_内容;}
function get_footer_text(){...};
function get_user(){new 新浪微博_账号 ;}
function get_comment(){new 新浪微博_评论;}
}
//使用
印象笔记控件类->内容 = 显示新浪微博->get_context;
印象笔记控件类->账号 = 显示新浪微博->get_context;
...
class Show_Evernote{
private $内容;
private $账号;
private $评论;
function __construct(内容,账号,评论){
$this->内容 = 内容;
$this->账号 = 账号;
$this->评论 = 评论;
}
function get_内容(){
return clone $this->内容);
}
function get_账号(){
return clone $this->账号);
}
function get_评论(){
return clone $this->评论;
}
}
//使用
$factory = new Show_Evernote(
new 新浪微博内容(),
new 新浪微博账号(),
new 新浪微博评论()
);
印象笔记控件类->显示印象笔记 = $factory;
//创建一个妹子 $妹子 = new 人(); //添加洗脚男、捶背男 $妹子->add_man(new 洗脚男); $妹子->add_man(new 捶背男); //循环所有男的给予舒服的方法. $妹子->计算舒服程度();
abstract class 人{
...
abstract function get_well();
}
class 男 extends 人 {
//无论你是神马男,服务你,你就能获得10点舒服度.
private $well = 10;
function get_well(){
return $this->well();
}
}
abstract class 装饰男类型 extends 人 {
protected $人;
function __construct(人 $人){
$this->人 = $人;
}
}
class 捶背装饰 extends 类型男装饰{
function get_well(){
return $this->人->get_well()+30;
}
}
class 洗发装饰 extends 类型男装饰{
function get_well(){
return $this->人->get_well()+20;
}
}
class 洗褪装饰 extends 类型男装饰{
//老子不喜欢别人碰我的毛裤.
function get_well(){
return $this->人->get_well()-20;
}
}
//创建捶背,能给予的舒服指数 - -嘻嘻.
$人 = new 捶背装饰(new 男);
$人->get_well(); // 10+30 = 40
//来来来,全能选手,捶背、洗发、洗腿一起来
$人 = new 洗脚装饰(new 洗发装饰(new 捶背装饰(new 男()))); //10+30+20-20 = 40,注意顺序,由里到外执行.
abstract class 人 {
protectd $支付方式;
function set_支付方式(){...}
function 付款(金额){
return $支付方式->付款($金额);
}
}
abstract class 付款{
abstract function 付款($金额);
}
class 支付宝付款 extends 付款{
function 付款($金额){
return 外接支付宝付款流程($金额);
}
}
...
//使用
$男 =new 男();
///爽爽爽
...
//结账
$支付宝支付账单 = new 支付宝付款($金额);
$人 = new 男();
$人->set_支付方式(new 支付宝付款());
$人->付款();
interface 被观察者{
function attach(观察者);
function detatch(观察者);
function notify();
}
class Login implements 被观察者{
private $观察者;
function __construct(){
$this->观察者 = array();
}
function attach($观察者){
$this->观察者 = $观察者;
}
function detach($观察者){
//删除某个观察者的操作;
}
function notify(){
foreach ($this->观察者 as $单个观察者){
$单个观察者->update($this);
}
}
}
interface 观察者{
function update(被观察者);
}
abstract class Login_观察者 implements 观察者{
private $login;
function __construct (Login $login){
$this->login = $login;
$login->attach($this);
}
function update(观察者 $观察者){
if ($观察者 ===$this->login){
$this->do_update($观察者);
}
}
abstract function do_update(Login $login);
}
class 邮件观察者 extends 登陆观察者 {
function do_update(Login $login){
//判断条件 发送邮件
}
}
class 日志观察者 extends 登陆观察者 {
function do_update(Login $login){
//判断条件 记录到日志;
}
}
//使用
$login = new Login();
new 邮件观察者 ($login);
new 日志观察者 ($login);
abstract class 军队访问者{
abstract function 访问(单元);
function 访问军队($军队){
$this->访问($军队);
}
function 访问弓箭手($弓箭手){
$this->访问($弓箭手);
}
//这里重复定义了大量代码,其实可以用call来替代
function __call($method_name,$args){
if(strrpos($method_name, "访问")){
return call_user_func_array(
array($this,"访问"),$args
);
}
}
}
class 军队战斗力访问者 extends 军队访问者{
private $text="";
function 访问($单元){
$ret = "";
$pad = 4*$单元->getDpth(); //设置显示深一级前面多4个空格.
$ret .= sprintf( "%{$pad}s","");
$ret .= get_class($单元). ": ";
$ret .= "战斗力: " .$单元->bombardStrenth()."\n";
$this->text .=$ret;
}
function get_text(){
return $this->text;
}
}
abstract class 单元{
function 接受($军队访问者){
$method = "访问_".get_class($this);
$军队访问者->$method($this);
}
private $depth;
protected function set_depath($depth){
$this->depth=$depth;
}
function get_depth(){
return $this->depth;
}
...
}
abstract class 综合单元 extends 单元{
function 接受($军队访问者){
parent::接受($军队访问者)
foreach($this->单元集合 as $this_unit){
$this->unit->接受($军队访问者);
}
}
}
class 军队 extends 综合单元{
function bombardStrenth(){
$ret =0;
foreach($this-units() as $unit){
$ret += $unit->bombardStrenth();
}
return $ret
}
}
class 弓箭手 extends 单元{
function bombardStrenth(){
return 4;
}
}
$main_army = new Army(); $main_army->add_unit(new 步兵()); $main_army->add_unit(new 弓箭手()); $军队战斗力访问者_实例 =new 军队战斗力访问者(); $main_army->接受(均分战斗力访问者); print $军队战斗力访问者->get_text();
abstract class Command{
abstract function execute(Conmmand_Context $context);
}
class Login_Command extends Command{
function execute(CommandContext $context){
$managr =Register::getAccessManager();
$user = $context->get("username");
$pass = $context->get('pass');
$user_obj = $manager->login($user,$pass);
if(is_null($user_obj)){
$context->setError($manager->getError());
return false;
}
$context->addParam("user",$user_obj);
return true;
}
}
class Command_Facotry{
public function get_command($action){
$class = UCFirst(strtolower($action))."_Command";
$cmd = new $class();
return $cmd;
}
}
class Controller{
private $context;
function __construct(){
//Command_Context主要用来存储request和params
$this->context =new Command_Context();
}
function process(){
$cmd Command_Factory::get_commad($this->context->get('action'));
if(!$cmd-execute($this->context)){
//错误处理
}else{
//成功 分发视图
}
}
}
$controller =new Controller();
$context = $controller->get_context();
$context->add_param('action','login');
$context->add_param('username','404_k');
$context->add_param('pass','123456');
$controller->process();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有