極簡架構模式-業務代表模式

long2ge發表於2021-12-18

解釋

在表示層和業務層之間新增了一個抽象層。用於解耦表示層和業務層。

結構中包含的角色

  1. 客戶端(Client) - 表示層程式碼。在MVC裡面就是V層。
  2. 業務代表類(BusinessDelegate)
  3. 抽象業務服務(BusinessService)
  4. 具體業務服務(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 協議》,轉載必須註明作者和本文連結
Long2Ge

相關文章