【发布时间】:2018-02-26 08:34:23
【问题描述】:
我正在浏览依赖注入文档,我在下面看到了一些内容,在这段代码中,他们创建了一个 inreface 的对象,然后调用了实现的函数。我在这里感到困惑,这是依赖注入中可用的选项还是 opps 功能。
class StoreService {
private $geolocationService;
public function __construct(GeolocationService $geolocationService)
{
$this->geolocationService = $geolocationService;
}
public function getStoreCoordinates($store) {
return $this->geolocationService->getCoordinatesFromAddress($store->getAddress());
}
}
interface GeolocationService {
public function getCoordinatesFromAddress($address);
}
//////////////////////////////////////////////////////
/////////////////////////////////////// /////////////// ///////////////////////////////////////// ////////////
class GoogleMaps implements GeolocationService
{
public function getCoordinatesFromAddress($address) {
// calls Google Maps webservice
}
}
class OpenStreetMap implements GeolocationService
{
public function getCoordinatesFromAddress($address) {
// calls OpenStreetMap webservice
}
}
【问题讨论】: