Das Bridge-Entwurfsmuster in PHP
Feb 23 2023
So implementieren Sie dieses strukturelle Entwurfsmuster in PHP
Was ist das Brückenentwurfsmuster? Das Bridge-Muster ist ein Entwurfsmuster, das eine Abstraktion von ihrer Implementierung entkoppelt, sodass beide unabhängig voneinander variieren können. In PHP kann das Bridge-Muster mithilfe von Schnittstellen, abstrakten Klassen und Vererbung implementiert werden.
Was ist das Brückenentwurfsmuster?
Das Bridge-Muster ist ein Entwurfsmuster, das eine Abstraktion von ihrer Implementierung entkoppelt, sodass beide unabhängig voneinander variieren können.
In PHP kann das Bridge-Muster mithilfe von Schnittstellen, abstrakten Klassen und Vererbung implementiert werden.
Hier ist eine Beispielimplementierung des Bridge-Musters in PHP:
<?php
interface DrawAPI {
public function drawCircle($x, $y, $radius);
}
class DrawAPI1 implements DrawAPI {
public function drawCircle($x, $y, $radius) {
echo "API1.circle at ($x,$y) radius $radius.\n";
}
}
class DrawAPI2 implements DrawAPI {
public function drawCircle($x, $y, $radius) {
echo "API2.circle at ($x,$y) radius $radius.\n";
}
}
abstract class Shape {
protected $drawAPI;
protected function __construct(DrawAPI $drawAPI) {
$this->drawAPI = $drawAPI;
}
public abstract function draw();
}
class CircleShape extends Shape {
private $x, $y, $radius;
public function __construct($x, $y, $radius, DrawAPI $drawAPI) {
parent::__construct($drawAPI);
$this->x = $x;
$this->y = $y;
$this->radius = $radius;
}
public function draw() {
$this->drawAPI->drawCircle($this->x, $this->y, $this->radius);
}
}
// Usage:
$shape1 = new CircleShape(1, 2, 3, new DrawAPI1());
$shape2 = new CircleShape(5, 7, 11, new DrawAPI2());
$shape1->draw(); // Output: API1.circle at (1,2) radius 3.
$shape2->draw(); // Output: API2.circle at (5,7) radius 11.
Indem wir die Abstraktion ( Shape) von ihrer Implementierung ( DrawAPI) trennen, können wir jede unabhängig voneinander variieren. Beispielsweise könnten wir eine DrawAPI3Implementierung hinzufügen, ohne eine der ShapeKlassen zu ändern.
Vielen Dank für Ihre Zeit!
Fühlen Sie sich frei, und zu folgen!

![Was ist überhaupt eine verknüpfte Liste? [Teil 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































