Skip to content

Commit

Permalink
Behavioral Observer FavoritePatternsGossip
Browse files Browse the repository at this point in the history
  • Loading branch information
davpons authored Oct 15, 2023
1 parent 3821d0d commit d963d90
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Behavioral/Observer/FavoritePatternsGossip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

abstract class AbstractSubject {
abstract public function attach(AbstractObserver $observerIn): void;
abstract public function detach(AbstractObserver $observerIn): void;
abstract public function notify(): void;
}

abstract class AbstractObserver {
abstract function update(AbstractSubject $subjectIn): void;
}

class PatternSubject extends AbstractSubject
{
private ?string $favorites = null;
private array $observers = [];

public function attach(AbstractObserver $observerIn): void
{
$this->observers[] = $observerIn;
}

public function detach(AbstractObserver $observerIn): void
{
$oKey = array_search($observerIn, $this->observers);
if (false !== $oKey) {
unset($this->observers[$oKey]);
}
}

public function notify(): void
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}

public function updateFavorites(string $newFavorites): void
{
$this->favorites = $newFavorites;
$this->notify();
}

public function getFavorites(): string
{
return $this->favorites;
}
}

class PatternObserver extends AbstractObserver
{
public function update(AbstractSubject $subject): void
{
echo '**OBSERVADOR - ALERTA DE CHISMES NUEVOS PATRONES FAVORITOS**<br>';
echo 'Nuevos patrones favoritos: ' . $subject->getFavorites() . '<br>';
}
}

$patternGossiper = new PatternSubject();
$patternGossipFan = new PatternObserver();

$patternGossiper->attach($patternGossipFan);
$patternGossiper->updateFavorites('abstract factory, decorator, visitor');
$patternGossiper->updateFavorites('abstract factory, observer, decorator');

$patternGossiper->detach($patternGossipFan);
$patternGossiper->updateFavorites('abstract factory, observer, visitor');

0 comments on commit d963d90

Please sign in to comment.