解釋
在表示層和業務層之間新增了一個抽象層。用於解耦表示層和業務層。
結構中包含的角色
- 客戶端(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();
本作品採用《CC 協議》,轉載必須註明作者和本文連結