*** Beobachtermuster *** ist eine Art Entwurfsmuster, das beim Benachrichtigen anderer Objekte über Ereignisse (Ereignisse) eines Objekts im Programm verwendet wird. Dies wird aufgerufen, weil die zu benachrichtigende Objektseite von der zu benachrichtigenden Objektseite beobachtet wird (Englisch: beobachten). Wird auch als Publishing-Abonnement-Modell bezeichnet. Es ist eng mit dem Prinzip des impliziten Aufrufs verbunden. Es wird auch verwendet, um ein verteiltes Ereignisverarbeitungssystem zu implementieren. Abhängig von der Sprache werden die in diesem Muster behandelten Probleme von der Ereignisbehandlungssyntax der Sprache behandelt.
import java.util.ArrayList;
interface Observer {
public void update(float interest);
}
interface Subject {
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObservers();
}
class Loan implements Subject {
private ArrayList<Observer> observers = new ArrayList<Observer>();
private String type;
private float interest;
private String bank;
public Loan(String type, float interest, String bank) {
this.type = type;
this.interest = interest;
this.bank = bank;
}
public float getInterest() {
return interest;
}
public void setInterest(float interest) {
this.interest = interest;
notifyObservers();
}
public String getBank() {
return this.bank;
}
public String getType() {
return this.type;
}
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer ob : observers) {
System.out.println("Notifying Observers on change in Loan interest rate");
ob.update(this.interest);
}
}
}
class Newspaper implements Observer {
@Override
public void update(float interest) {
System.out.println("Newspaper: Interest Rate updated, new Rate is: " + interest);
}
}
class Internet implements Observer {
@Override
public void update(float interest) {
System.out.println("Internet: Interest Rate updated, new Rate is: "
+ interest);
}
}
public class ObserverTest {
public static void main(String args[]) {
// this will maintain all loans information
Newspaper printMedia = new Newspaper();
Internet onlineMedia = new Internet();
Loan personalLoan = new Loan("Personal Loan", 12.5f,
"Standard Charterd");
personalLoan.registerObserver(printMedia);
personalLoan.registerObserver(onlineMedia);
personalLoan.setInterest(3.5f);
}
}
<?php
abstract class AbstractObserver {
abstract function update(AbstractSubject $subject_in);
}
abstract class AbstractSubject {
abstract function attach(AbstractObserver $observer_in);
abstract function detach(AbstractObserver $observer_in);
abstract function notify();
}
function writeln($line_in) {
echo $line_in."<br/>";
}
class PatternObserver extends AbstractObserver {
public function __construct() {
}
public function update(AbstractSubject $subject) {
writeln('*IN PATTERN OBSERVER - NEW PATTERN GOSSIP ALERT*');
writeln(' new favorite patterns: '.$subject->getFavorites());
writeln('*IN PATTERN OBSERVER - PATTERN GOSSIP ALERT OVER*');
}
}
class PatternSubject extends AbstractSubject {
private $favoritePatterns = NULL;
private $observers = array();
function __construct() {
}
function attach(AbstractObserver $observer_in) {
//could also use array_push($this->observers, $observer_in);
$this->observers[] = $observer_in;
}
function detach(AbstractObserver $observer_in) {
//$key = array_search($observer_in, $this->observers);
foreach($this->observers as $okey => $oval) {
if ($oval == $observer_in) {
unset($this->observers[$okey]);
}
}
}
function notify() {
foreach($this->observers as $obs) {
$obs->update($this);
}
}
function updateFavorites($newFavorites) {
$this->favorites = $newFavorites;
$this->notify();
}
function getFavorites() {
return $this->favorites;
}
}
writeln('BEGIN TESTING OBSERVER PATTERN');
writeln('');
$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, paisley');
writeln('END TESTING OBSERVER PATTERN');
?>
Ausgabeergebnis
BEGIN TESTING OBSERVER PATTERN
*IN PATTERN OBSERVER - NEW PATTERN GOSSIP ALERT*
new favorite patterns: abstract factory, decorator, visitor
*IN PATTERN OBSERVER - PATTERN GOSSIP ALERT OVER*
*IN PATTERN OBSERVER - NEW PATTERN GOSSIP ALERT*
new favorite patterns: abstract factory, observer, decorator
*IN PATTERN OBSERVER - PATTERN GOSSIP ALERT OVER*
END TESTING OBSERVER PATTERN
"""
Define a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updatedautomatically.
"""
import abc
class Subject:
"""
Know its observers. Any number of Observer objects may observe a
subject.
Send a notification to its observers when its state changes.
"""
def __init__(self):
self._observers = set()
self._subject_state = None
def attach(self, observer):
observer._subject = self
self._observers.add(observer)
def detach(self, observer):
observer._subject = None
self._observers.discard(observer)
def _notify(self):
for observer in self._observers:
observer.update(self._subject_state)
@property
def subject_state(self):
return self._subject_state
@subject_state.setter
def subject_state(self, arg):
self._subject_state = arg
self._notify()
class Observer(metaclass=abc.ABCMeta):
"""
Define an updating interface for objects that should be notified of
changes in a subject.
"""
def __init__(self):
self._subject = None
self._observer_state = None
@abc.abstractmethod
def update(self, arg):
pass
class ConcreteObserver(Observer):
"""
Implement the Observer updating interface to keep its state
consistent with the subject's.
Store state that should stay consistent with the subject's.
"""
def update(self, arg):
self._observer_state = arg
# ...
def main():
subject = Subject()
concrete_observer = ConcreteObserver()
subject.attach(concrete_observer)
subject.subject_state = 123
if __name__ == "__main__":
main()
Recommended Posts