Proxy
Publié le 2 Juin 2025Proxy est un patron de conception structurel qui fournit un substitut d’un autre objet pour contrôler l’accès à celui-ci. Il est très utile pour ajouter un comportement à un objet sans modifier son code.
Principe
Exemples d’utilisations
Proxy d'authentification et contrôle d’accès d’une API
Proxy pour la mise en cache d’appels à une API externe
Points d’attentions
Implémenter un Proxy
Voici un exemple en Go :
package weather
import (
"fmt"
"time"
)
// WeatherService Interface
type WeatherService interface {
GetWeather(city string) string
}
// RealWeatherService Real service (simule un appel long)
type RealWeatherService struct{}
func (r *RealWeatherService) GetWeather(city string) string {
time.Sleep(2 * time.Second) // simulation d’un appel lent
return fmt.Sprintf("Météo en %s : Ensoleillé", city)
}
package weather
import "fmt"
type WeatherServiceProxy struct {
realService WeatherService
cache map[string]string
}
func NewWeatherServiceProxy(real WeatherService) *WeatherServiceProxy {
return &WeatherServiceProxy{
realService: real,
cache: make(map[string]string),
}
}
func (p *WeatherServiceProxy) GetWeather(city string) string {
if val, found := p.cache[city]; found {
return fmt.Sprintf("[CACHE] %s", val)
}
val := p.realService.GetWeather(city)
p.cache[city] = val
return val
}
package main
import (
"fmt"
apiweather "designpatterns/proxy/weather"
)
func main() {
realService := &apiweather.RealWeatherService{}
proxy := apiweather.NewWeatherServiceProxy(realService)
fmt.Println(proxy.GetWeather("Paris"))
fmt.Println(proxy.GetWeather("Paris")) // Doit être en cache
}
Le même exemple en PHP :
namespace Practice\DesignPatterns\Proxy;
class RealWeatherService implements WeatherServiceInterface
{
public function getWeather(string $city): string
{
sleep(2); // Simule un appel lent ou coûteux
return "Météo à $city : Ensoleillé";
}
}
namespace Practice\DesignPatterns\Proxy;
interface WeatherServiceInterface
{
public function getWeather(string $city): string;
}
namespace Practice\DesignPatterns\Proxy;
class WeatherServiceProxy implements WeatherServiceInterface
{
private WeatherServiceInterface $realService;
private array $cache = [];
public function __construct(WeatherServiceInterface $realService)
{
$this->realService = $realService;
}
public function getWeather(string $city): string
{
if (isset($this->cache[$city])) {
return "[CACHE] " . $this->cache[$city];
}
$result = $this->realService->getWeather($city);
$this->cache[$city] = $result;
return $result;
}
}
declare(strict_types=1);
use Practice\DesignPatterns\Proxy\RealWeatherService;
use Practice\DesignPatterns\Proxy\WeatherServiceProxy;
require "./vendor/autoload.php";
$realService = new RealWeatherService();
$proxy = new WeatherServiceProxy($realService);
// Premier appel — lent
echo $proxy->getWeather('Paris') . PHP_EOL;
// Deuxième appel — depuis le cache
echo $proxy->getWeather('Paris') . PHP_EOL;
// Autre ville — non encore en cache
echo $proxy->getWeather('Lyon') . PHP_EOL;