我正在浏览依赖注入文档,我已经看到下面的内容,在这段代码中,他们创建了一个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
}
}