NEWS PAPER SUBSCRIPTIONa newspaper publisher goes into a business andbegins publishing a newspaper.You subscribe to a publisher.Every time a new release is published , it getsdelivered.Once you unsubscribe, no longer issues are delivered.
DR BADR EXPLAINSDr badr is a big fan of Ilham shaheen
Dr badr subscribes to Ilham shaeens activity.
""ﺣﺳب ﷲ و ﻧﻌم اﻟوﻛﯾلBadr.. Unsubscribes
so now when "ilham" does any thing
BADRBadr doesnt get informed and thus be him self
HAPPY ENDINGwhich in the end makes ilham happy
TERMINOLOGY Subject :News Paper Observer :Subscriber(s)SUBJECT +OBSERVER= OBSERVER PATTERN
USES iphoneeventlisterns push fb
EXAMPLES Im Back
WEATHERcet a apiainta poie tredslyeeet;urn cnii rae n plcto ht rvds he ipa lmnscret odto ,wahrsaisadasml frcs aludt i ratm n ete ttc n ipe oeat l pae n elie
GET HANDS DIRTY
TIPS & HINTSObserver can be a subject at the same time.Power of loose coupling.Java observer pattern.
POWER OF LOOSE COUPLING Loose coupling occurs when the dependent classcontains a pointer only to an interface, which can then be implemented by one or many concrete classes
STRONG COUPLING Strong coupling :occurs when a dependent class contains a pointer directly to a concrete class whichprovides the required behavior. The dependency cannotbe substituted, or changed, without requiring a change to the dependent class
POWER OF LOOSE COUPLINGThe only thing the subject knows about the observer isthat it implements a certain interface.We can add new observers any time.We can reuse subjects or observers independently ofeach other.Changes to either the subject or an observer will notaffect the other.
POWER OF LOOSE COUPLINGLoosely coupled designs allow us to build flexible OO systems that can handle change.Observer design pattern is a good example for making classes loosely coupled
JAVA OBSERVER PATTERN
JAVA:CLOSER LOOKFor an object to be an observer , just implement the "Observer Interface" For a subject implment the "Observable" class
JAVA:NOTIFYSUBJECT: setChanged(); notifiyObservers() or notifiyObservers(Object arg)Observer : update(Observable o, Object arg )
JAVA IS EVIL
JAVA IS EVIL Observable is a CLASS! limitations (a class already sub classing another class cant subclass Observable class) Observable protects methodseg: setChanged() you cant change it unless you subclass it, thus cant create a composed observable class with your changes.
SUMMARYObserver pattern defines one -to-many relationship.Subject (observable) updates observers usingcommon interface.Observers are loosely coupled;observables knownothing about them other than they implementobserver interface.Java has a built in Observable CLASS.
REFERENCESAstarApps,http://astarapps.com/.Ron McFadyen http://ion.uwinnipeg.ca/~rmcfadye/.Elisabeth Freeman,Head First Design Patterns.