这是一种观察者设计模式。所有的 GOF 模式都将最大限度地使用 OO 原则。如果职责(和状态)在许多子类中是通用的,则需要在基类层次结构中将其升级到可能的最上层。以下演示代码和评论将回答上述问题。确实,某些设计模式(Observer 就是其中之一。请参阅 update() 中的类型转换)违反了 OO 原则。违反这些原则完全没问题,只需要知道我们违反了哪条原则以及为什么违反。并检查 -我们是否获得了比实施中引入的可能的不灵活/损坏更多的好处?
public interface Observer {
/**
* IF THIS METHOD IS NOT HERE SUBJECT WILL NOT BE ABLE TO CALL IT
* REFER TO OO BASICS - INHERITANCE
*/
void update(Subject subject);
}
public abstract class Subject {
// THE COMPLETE LOGIC HERE IS INDEPENDENT OF
// WHO THE CONCRETE SUBJECT IS.
// HENCE THIS IS A RIGHT PLACE FOR IT AS PER OO PRINCIPLES
// NOTE THAT THIS CLASS KNOWS ONLY ABOUT ABSTRACT OBSERVER
private Set<Observer> observers;
public void add(Observer o){/*...*/}
public void remove(Observer o){/*...*/}
protected void notifyAllObservers(){
for (Observer observer : observers) {
observer.update(this);
}
}
}
class ConcreteSubject extends Subject {
/**
* HERE THERE IS NO NEED TO KNOW THE CONCRETE OBSERVER-
* By DEFINITION OF THIS PATTERN.
* THE JOB HERE IS TO ONLY NOTIFY ALL OBSERVERS THAT "I HAVE CHANGED"
* ITS OBSERVER'S RESPONSIBILITY TO REACT IN PULL METHOD
*/
// Concrete Subject state
// Concrete Subject behavior
public void specificMethod(){
//... concrete class specific logic
}
}
class ConcreteObserver implements Observer{
@Override
public void update(Subject subject) {
/**
* OBSERVE HERE WE NEED TO TYPE CAST, WHICH LEADS TO VIOLATION OF
* OO PRINCIPLES. BUT WE GET BENEFIT OF LOOSELY COUPLED OBSERVERS
* AGAINST THIS EXTRA COST
* ITS SAFE TO VIOLATE HERE AS OBSERVER INSTANCES MAKE ONLY
* SENSE IN CONTEXT WITH SUBJECT AND WILL NEVER BE REUSED AS
* OBSERVERS IN UNRELATED CONTEXT.
*/
((ConcreteSubject)subject).specificMethod();
}
}