기본적으로 이전과 동일하게 작동합니다. 단지 Zend_Db_Table_Gateway
대신 My_UserGateway_Whatever
을 사용합니다. 먼저 인터페이스를 만들 :
interface UserGateway
{
/**
* @return array
* @throws UserGatewayException
*/
public function findAll();
}
을 우리는 소비 코드에 표시 콘크리트 게이트웨이에서 예외 싶지 않는, 그래서 우리는 모든 캐치로 UserGatewayException를 추가 : 다음
class UserGatewayException extends RuntimeException
{}
것을 구현하는 클래스를 추가 인터페이스 :
마찬가지로 데이터베이스 소스를 사용하려는 경우 Zend_Db_ * 클래스 용 어댑터를 작성할 수 있습니다. 다른 데이터 공급자가 필요한 경우
class My_UserGateway_Database implements UserGateway
{
private $tableDataGateway;
public function __construct(Zend_Db_Table_Abstract $tableDataGateway)
{
$this->tableDataGateway = $tableDataGateway;
}
public function findAll()
{
try {
return $this->tableDataGateway->select()->blah();
} catch (Exception $e) {
throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
}
}
// … more code
}
, 그들은 그 인터페이스를 구현해야합니다, 그래서 당신은 거기하는 findAll
방법에 의존 할 수 있습니다. 소비하는 클래스가 인터페이스 (예 : 이제
class SomethingUsingUsers
{
private $userGateway;
public function __construct(UserGateway $userGateway)
{
$this->userGateway = $userGateway;
}
public function something()
{
try {
$users = $this->userGateway->findAll();
// do something with array of user data from gateway
} catch (UserGatewayException $e) {
// handle Exception
}
}
// … more code
}
, 당신은 SomethingUsingUsers
를 만들 때 쉽게 하나를 주입 또는 생성자에 다른 게이트웨이와 코드를 사용한 관계없이의 게이트웨이를 작동 할 수 있습니다 WebService에 대한,
$foo = SomethingUsingUsers(
new My_UserGateway_Database(
new Zend_Db_Table('User')
)
)
또는 :
$foo = SomethingUsingUsers(
new My_UserGateway_Webservice(
// any additional dependencies
)
)