业务代表模式 - Business Delegate Pattern
解释
在表示层和业务层之间添加了一个抽象层。用于解耦表示层和业务层。
结构中包含的角色
- 客户端(Client) - 表示层代码。在MVC里面就是V层。
- 业务代表类(BusinessDelegate)
- 抽象业务服务(BusinessService)
- 具体业务服务(ConcreteBusinessService)
最小可表达代码
interface BusinessService
{
public function handle();
}
class ConcreteBusinessService implements BusinessService
{
public function handle()
{
echo 'Service : handle';
}
}
class BusinessDelegate
{
private $serviceType;
public function setServiceType(String $serviceType)
{
$this->serviceType = $serviceType;
}
public function handle()
{
// 这里可以写成一个查询服务,为了方便理解,写具体服务在这里
if ($this->serviceType) {}
$businessService = new ConcreteBusinessService();
$businessService->handle();
}
}
class Client
{
private $businessService;
public function __construct(BusinessDelegate $businessService)
{
$this->businessService = $businessService;
}
public function handle()
{
$this->businessService->handle();
}
}
$businessDelegate = new BusinessDelegate();
$businessDelegate->setServiceType("EJB");
$client = new Client($businessDelegate);
$client->handle();