PHP设计模式实例之(观察者模式、策略模式、简单工厂模式)

/**
* 定义观察接口
*/
interface 
Subject
{
    public function 
Attach($Observer); 
//添加观察者
    
public function Detach($Observer); 
//踢出观察者
    
public function Notify(); 
//满足条件时通知观察者
    
public function SubjectState($Subject); 
//观察条件
}

/**
* 观察类的具体实现
*/
class Boss Implements 
Subject 
{
    public 
$_action
;
    
    private 
$_Observer
;
    
    public function 
Attach($Observer
)
    {
        
$this->_Observer[] = $Observer
;
    }
    
    public function 
Detach($Observer
)
    {
        
$ObserverKey array_search($Observer$this->_Observer
);
        
        if(
$ObserverKey !== false
)
        {
            unset(
$this->_Observer[$ObserverKey
]);
        }
    }
    
    public function 
Notify
()
    {
        foreach(
$this->_Observer as $value 
)
        {
            
$value->Update
();
        }
    }
    
    public function 
SubjectState($Subject
)
    {
        
$this->_action $Subject
;
    }
}

/**
* 抽象观察者
*
*/
abstract class 
Observer
{
    protected 
$_UserName
;
    
    protected 
$_Sub
;
    
    public function 
__construct($Name,$Sub
)
    {
        
$this->_UserName $Name
;
        
$this->_Sub $Sub
;
    }
    
    public abstract function 
Update(); 
//接收通过方法
}

/**
* 观察者
*/
class StockObserver extends 
Observer
{
    public function 
__construct($name,$sub
)
    {
        
parent::__construct($name,$sub
);
    }
    
    public function 
Update
()
    {
        echo 
$this->_Sub->_action.$this->_UserName." 你赶快跑..."
;
    }
}

$huhansan = new Boss(); 
//被观察者

$gongshil = new StockObserver("三毛",$huhansan); 
//初始化观察者

$huhansan->Attach($gongshil); 
//添加一个观察者
$huhansan->Attach($gongshil); 
//添加一个相同的观察者
$huhansan->Detach($gongshil); 
//踢出基中一个观察者

$huhansan->SubjectState("警察来了"); 
//达到满足的条件

$huhansan->Notify(); //通过所有有效的观察者

 

 

/**
* 定义支持算法的接口
* 省略模式
*/
abstract class 
Strategy
{
    abstract public function 
AlgorithmInterface
();
}

class 
ConcreateStratA extends 
Strategy 
{
    public function 
AlgorithmInterface
()
    {
        echo 
"算法A"
;
    }
}

class 
ConcreateStratB extends 
Strategy 
{
    public function 
AlgorithmInterface
()
    {
        echo 
"算法B"
;
    }
}


class 
ConcreateStratC extends 
Strategy 
{
    public function 
AlgorithmInterface
()
    {
        echo 
"算法C"
;
    }
}

class 
Context
{
    private 
$_StrObj
;
    
    public function 
__construct($strobj
)
    {
        
$this->_StrObj $strobj
;
    }
    
    public function 
ContextInterface
()
    {
        
$this->_StrObj->AlgorithmInterface
();
    }
}

$context = new Context(new ConcreateStratA
);
$context->ContextInterface
();
$context = new Context(new ConcreateStratC
);
$context->ContextInterface
();
$context = new Context(new ConcreateStratB
);
$context->ContextInterface();

 

/**
* 定义运算类
* 简单工厂模式
*/
abstract class 
Operation
{
    
//存储第一个数字
    
protected $_NumberA 0
;
    
    
//存储第二个数字
    
protected $_NumberB 0
;
    
    
//存储运算结果
    
protected $_Result 0
;
    
    public function 
__construct
()
    {
        
//empty
    
}
    
    
/**
     * 设定需要运行的二个数字
     *
     * @param 第一个数字 $A
     * @param 第二个数字 $B
     */
    
public function SetNumber($A=0,$B=0
)
    {
        
$this->_NumberA $A
;
        
$this->_NumberB $B
;
    }
    
    
/**
     * 清除所有数据
     *
     */
    
protected function ClearResult
()
    {
        
$this->_Result 0
;
    }
    
    abstract function 
GetResult
();
}


/**
* 数据相加
*/
class OperactionAdd extends 
Operation 
{
    public function 
GetResult
()
    {
        
$this->_Result $this->_NumberA $this->_NumberB
;
        
        return 
$this->_Result
;
    }
}


/**
* 数据相减
*/
class OperactionSub extends 
Operation 
{
    public function 
GetResult
()
    {
        
$this->_Result $this->_NumberA $this->_NumberB
;
        
        return 
$this->_Result
;
    }
}


/**
* 数据相乘
*/
class OperactionMul extends 
Operation 
{
    public function 
GetResult
()
    {
        
$this->_Result $this->_NumberA $this->_NumberB
;
        
        return 
$this->_Result
;
    }
}


/**
* 数据相除
*/
class OperactionDiv extends 
Operation 
{
    public function 
GetResult
()
    {
        
$this->_Result $this->_NumberA $this->_NumberB
;
        
        return 
$this->_Result
;
    }
}

class 
OperactionFactory
{
    private static 
$_Obj
;
    
    public static function 
CreateOperaction($type
)
    {
         switch(
$type
)
         {
             case 
"+"
:
                 
self::$_Obj = new OperactionAdd
;
                 break;
             case 
"-"
:
                 
self::$_Obj = new OperactionSub
;
                 break;
             case 
"*"
:
                 
self::$_Obj = new OperactionMul
;
                 break;
             case 
"/"
:
                 
self::$_Obj = new OperactionDiv
;
                 break;
         }
         
         return 
self::$_Obj
;
    }
}

$Obj OperactionFactory::CreateOperaction("*"
);
$Obj->SetNumber(6,4
);
$num $Obj->GetResult
();
var_dump($num);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值