<?php
// 实现类接口
interface Implementor
{
public function operationImpl();
}
// 具体实现类A
class ConcreteImplementorA implements Implementor
{
public function operationImpl()
{
return “ConcreteImplementorA operation.”;
}
}
// 具体实现类B
class ConcreteImplementorB implements Implementor
{
public function operationImpl()
{
return “ConcreteImplementorB operation.”;
}
}
// 抽象类
abstract class Abstraction
{
protected $implementor;
public function __construct(Implementor $implementor)
{
$this->implementor=$implementor;
}
abstract public function operation();
}
// 扩展抽象类
class RefinedAbstraction extends Abstraction
{
public function operation()
{
return $this->implementor->operationImpl();
}
}
// 客户端代码
$implementorA=new ConcreteImplementorA();
$abstraction=new RefinedAbstraction($implementorA);
echo $abstraction->operation(); // 输出 “ConcreteImplementorA operation.”
// 实现类接口
interface Implementor
{
public function operationImpl();
}
// 具体实现类A
class ConcreteImplementorA implements Implementor
{
public function operationImpl()
{
return “ConcreteImplementorA operation.”;
}
}
// 具体实现类B
class ConcreteImplementorB implements Implementor
{
public function operationImpl()
{
return “ConcreteImplementorB operation.”;
}
}
// 抽象类
abstract class Abstraction
{
protected $implementor;
public function __construct(Implementor $implementor)
{
$this->implementor=$implementor;
}
abstract public function operation();
}
// 扩展抽象类
class RefinedAbstraction extends Abstraction
{
public function operation()
{
return $this->implementor->operationImpl();
}
}
// 客户端代码
$implementorA=new ConcreteImplementorA();
$abstraction=new RefinedAbstraction($implementorA);
echo $abstraction->operation(); // 输出 “ConcreteImplementorA operation.”
© 版权声明
文章版权归作者所有,未经允许请勿转载。