Observer in Java
2016.05.30 21:02
Observer design pattern, class inheritance vs type inheritance
SensorSystem
is the "subject". Lighting
, Gates
, and Surveillance
are the "views". The subject is only coupled to the "abstraction" of AlarmListener
.
An object's class defines how the object is implemented. In contrast, an object's type only refers to its interface. Class inheritance defines an object's implementation in terms of another object's implementation. Type inheritance describes when an object can be used in place of another.
interface AlarmListener { public void alarm(); }
class SensorSystem {
private java.util.Vector listeners = new java.util.Vector();
public void register( AlarmListener al ) { listeners.addElement( al ); }
public void soundTheAlarm() {
for (java.util.Enumeration e=listeners.elements(); e.hasMoreElements(); )
((AlarmListener)e.nextElement()).alarm();
} }
class Lighting implements AlarmListener {
public void alarm() { System.out.println( "lights up" ); }
}
class Gates implements AlarmListener {
public void alarm() { System.out.println( "gates close" ); }
}
class CheckList {
public void byTheNumbers() { // Template Method design pattern
localize();
isolate();
identify(); }
protected void localize() {
System.out.println( " establish a perimeter" ); }
protected void isolate() {
System.out.println( " isolate the grid" ); }
protected void identify() {
System.out.println( " identify the source" ); }
}
// class inheri. // type inheritance
class Surveillance extends CheckList implements AlarmListener {
public void alarm() {
System.out.println( "Surveillance - by the numbers:" );
byTheNumbers(); }
protected void isolate() { System.out.println( " train the cameras" ); }
}
public class ClassVersusInterface {
public static void main( String[] args ) {
SensorSystem ss = new SensorSystem();
ss.register( new Gates() );
ss.register( new Lighting() );
ss.register( new Surveillance() );
ss.soundTheAlarm();
} }
Output
gates close lights up Surveillance - by the numbers: establish a perimeter train the cameras identify the source
[출처] https://sourcemaking.com/design_patterns/observer/java/2
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.