<s>
In	O
software	O
design	O
and	O
engineering	B-General_Concept
,	O
the	O
observer	B-Language
pattern	I-Language
is	O
a	O
software	O
design	O
pattern	O
in	O
which	O
an	O
object	O
,	O
named	O
the	O
subject	O
,	O
maintains	O
a	O
list	O
of	O
its	O
dependents	O
,	O
called	O
observers	O
,	O
and	O
notifies	O
them	O
automatically	O
of	O
any	O
state	O
changes	O
,	O
usually	O
by	O
calling	O
one	O
of	O
their	O
methods	B-Language
.	O
</s>
<s>
It	O
is	O
often	O
used	O
for	O
implementing	O
distributed	O
event-handling	O
systems	O
in	O
event-driven	B-Application
software	I-Application
.	O
</s>
<s>
This	O
pattern	O
thus	O
suits	O
any	O
process	O
by	O
which	O
data	O
arrives	O
from	O
some	O
input	O
that	O
is	O
not	O
available	O
to	O
the	O
CPU	B-Device
at	O
startup	B-Operating_System
,	O
but	O
instead	O
arrives	O
seemingly	O
at	O
random	O
(	O
HTTP	O
requests	O
,	O
GPIO	B-Architecture
data	O
,	O
user	B-General_Concept
input	I-General_Concept
from	O
peripherals	O
,	O
distributed	B-Operating_System
databases	I-Operating_System
and	O
blockchains	B-Application
,	O
etc	O
.	O
</s>
<s>
The	O
observer	B-Language
design	I-Language
pattern	I-Language
is	O
a	O
behavioural	O
pattern	O
listed	O
among	O
the	O
23	O
well-known	O
"	O
Gang	O
of	O
Four	O
"	O
design	O
patterns	O
that	O
address	O
recurring	O
design	O
challenges	O
in	O
order	O
to	O
design	O
flexible	O
and	O
reusable	O
object-oriented	O
software	O
,	O
yielding	O
objects	O
that	O
are	O
easier	O
to	O
implement	O
,	O
change	O
,	O
test	O
and	O
reuse	O
.	O
</s>
<s>
The	O
observer	B-Language
pattern	I-Language
addresses	O
the	O
following	O
problems	O
:	O
</s>
<s>
A	O
one-to-many	O
dependency	B-Application
between	O
objects	O
should	O
be	O
defined	O
without	O
making	O
the	O
objects	O
tightly	B-Application
coupled	I-Application
.	O
</s>
<s>
Defining	O
a	O
one-to-many	O
dependency	B-Application
between	O
objects	O
by	O
defining	O
one	O
object	O
(	O
subject	O
)	O
that	O
updates	O
the	O
state	O
of	O
dependent	O
objects	O
directly	O
is	O
inflexible	O
because	O
it	O
couples	O
the	O
subject	O
to	O
particular	O
dependent	O
objects	O
.	O
</s>
<s>
However	O
,	O
it	O
might	O
be	O
applicable	O
from	O
a	O
performance	O
point	O
of	O
view	O
or	O
if	O
the	O
object	O
implementation	O
is	O
tightly	B-Application
coupled	I-Application
(	O
such	O
as	O
low-level	O
kernel	O
structures	O
that	O
execute	O
thousands	O
of	O
times	O
per	O
second	O
)	O
.	O
</s>
<s>
Tightly	B-Application
coupled	I-Application
objects	O
can	O
be	O
difficult	O
to	O
implement	O
in	O
some	O
scenarios	O
and	O
are	O
not	O
easily	O
reused	O
because	O
they	O
refer	O
to	O
and	O
are	O
aware	O
of	O
many	O
objects	O
with	O
different	O
interfaces	O
.	O
</s>
<s>
In	O
other	O
scenarios	O
,	O
tightly	B-Application
coupled	I-Application
objects	O
can	O
be	O
a	O
better	O
option	O
because	O
the	O
compiler	O
is	O
able	O
to	O
detect	O
errors	O
at	O
compile	O
time	O
and	O
optimize	O
the	O
code	O
at	O
the	O
CPU	B-Device
instruction	O
level	O
.	O
</s>
<s>
This	O
notification-registration	O
interaction	O
is	O
also	O
known	O
as	O
publish-subscribe	B-Operating_System
.	O
</s>
<s>
The	O
observer	B-Language
pattern	I-Language
can	O
cause	O
memory	B-Error_Name
leaks	I-Error_Name
,	O
known	O
as	O
the	O
lapsed	B-Error_Name
listener	I-Error_Name
problem	I-Error_Name
,	O
because	O
in	O
a	O
basic	O
implementation	O
,	O
it	O
requires	O
both	O
explicit	O
registration	O
and	O
explicit	O
deregistration	O
,	O
as	O
in	O
the	O
dispose	B-Application
pattern	I-Application
,	O
because	O
the	O
subject	O
holds	O
strong	B-General_Concept
references	I-General_Concept
to	O
the	O
observers	O
,	O
keeping	O
them	O
alive	O
.	O
</s>
<s>
This	O
can	O
be	O
prevented	O
if	O
the	O
subject	O
holds	O
weak	B-General_Concept
references	I-General_Concept
to	O
the	O
observers	O
.	O
</s>
<s>
Typically	O
,	O
the	O
observer	B-Language
pattern	I-Language
is	O
implemented	O
so	O
that	O
the	O
subject	O
being	O
observed	O
is	O
part	O
of	O
the	O
object	O
for	O
which	O
state	O
changes	O
are	O
being	O
observed	O
(	O
and	O
communicated	O
to	O
the	O
observers	O
)	O
.	O
</s>
<s>
This	O
type	O
of	O
implementation	O
is	O
considered	O
tightly	B-Application
coupled	I-Application
,	O
forcing	O
both	O
the	O
observers	O
and	O
the	O
subject	O
to	O
be	O
aware	O
of	O
each	O
other	O
and	O
have	O
access	O
to	O
their	O
internal	O
parts	O
,	O
creating	O
possible	O
issues	O
of	O
scalability	B-Architecture
,	O
speed	O
,	O
message	O
recovery	O
and	O
maintenance	O
(	O
also	O
called	O
event	O
or	O
notification	O
loss	O
)	O
,	O
the	O
lack	O
of	O
flexibility	O
in	O
conditional	O
dispersion	O
and	O
possible	O
hindrance	O
to	O
desired	O
security	O
measures	O
.	O
</s>
<s>
In	O
some	O
(	O
non-polling	B-General_Concept
)	O
implementations	O
of	O
the	O
publish-subscribe	B-Operating_System
pattern	I-Operating_System
,	O
this	O
is	O
solved	O
by	O
creating	O
a	O
dedicated	O
message	O
queue	O
server	O
(	O
and	O
sometimes	O
an	O
extra	O
message	O
handler	O
object	O
)	O
as	O
an	O
extra	O
stage	O
between	O
the	O
observer	O
and	O
the	O
object	O
being	O
observed	O
,	O
thus	O
decoupling	O
the	O
components	O
.	O
</s>
<s>
In	O
these	O
cases	O
,	O
the	O
message	O
queue	O
server	O
is	O
accessed	O
by	O
the	O
observers	O
with	O
the	O
observer	B-Language
pattern	I-Language
,	O
subscribing	O
to	O
certain	O
messages	O
and	O
knowing	O
(	O
or	O
not	O
knowing	O
,	O
in	O
some	O
cases	O
)	O
about	O
only	O
the	O
expected	O
message	O
,	O
while	O
knowing	O
nothing	O
about	O
the	O
message	O
sender	O
itself	O
;	O
the	O
sender	O
may	O
also	O
know	O
nothing	O
about	O
the	O
observers	O
.	O
</s>
<s>
Other	O
implementations	O
of	O
the	O
publish-subscribe	B-Operating_System
pattern	I-Operating_System
,	O
which	O
achieve	O
a	O
similar	O
effect	O
of	O
notification	O
and	O
communication	O
to	O
interested	O
parties	O
,	O
do	O
not	O
use	O
the	O
observer	B-Language
pattern	I-Language
.	O
</s>
<s>
In	O
early	O
implementations	O
of	O
multi-window	O
operating	O
systems	O
such	O
as	O
OS/2	B-Application
and	O
Windows	B-Application
,	O
the	O
terms	O
"	O
publish-subscribe	B-Operating_System
pattern	I-Operating_System
"	O
and	O
"	O
event-driven	B-Application
software	I-Application
development	O
"	O
were	O
used	O
as	O
synonyms	O
for	O
the	O
observer	B-Language
pattern	I-Language
.	O
</s>
<s>
The	O
observer	B-Language
pattern	I-Language
,	O
as	O
described	O
in	O
the	O
Design	O
Patterns	O
book	O
,	O
is	O
a	O
very	O
basic	O
concept	O
and	O
does	O
not	O
address	O
removing	O
interest	O
in	O
changes	O
to	O
the	O
observed	O
subject	O
or	O
special	O
logic	O
to	O
be	O
performed	O
by	O
the	O
observed	O
subject	O
before	O
or	O
after	O
notifying	O
the	O
observers	O
.	O
</s>
<s>
These	O
concerns	O
are	O
typically	O
handled	O
in	O
message-queueing	O
systems	O
,	O
in	O
which	O
the	O
observer	B-Language
pattern	I-Language
plays	O
only	O
a	O
small	O
part	O
.	O
</s>
<s>
Related	O
patterns	O
include	O
publish	B-Operating_System
–	I-Operating_System
subscribe	I-Operating_System
,	O
mediator	O
and	O
singleton	O
.	O
</s>
<s>
The	O
observer	B-Language
pattern	I-Language
may	O
be	O
used	O
in	O
the	O
absence	O
of	O
publish-subscribe	B-Operating_System
,	O
as	O
when	O
model	O
status	O
is	O
frequently	O
updated	O
.	O
</s>
<s>
Frequent	O
updates	O
may	O
cause	O
the	O
view	O
to	O
become	O
unresponsive	O
(	O
e.g.	O
,	O
by	O
invoking	O
many	O
repaint	B-Algorithm
calls	O
)	O
;	O
such	O
observers	O
should	O
instead	O
use	O
a	O
timer	O
.	O
</s>
<s>
In	O
this	O
UML	B-Language
class	O
diagram	O
,	O
the	O
Subject	O
class	O
does	O
not	O
update	O
the	O
state	O
of	O
dependent	O
objects	O
directly	O
.	O
</s>
<s>
The	O
UML	B-Language
sequence	O
diagram	O
shows	O
the	O
runtime	O
interactions	O
:	O
The	O
Observer1	O
and	O
Observer2	O
objects	O
call	O
attach(this )	O
on	O
Subject1	O
to	O
register	O
themselves	O
.	O
</s>
<s>
While	O
the	O
library	O
classes	O
and	O
exist	O
,	O
they	O
have	O
been	O
deprecated	B-General_Concept
in	O
Java	B-Language
9	O
because	O
the	O
model	O
implemented	O
was	O
quite	O
limited	O
.	O
</s>
<s>
Below	O
is	O
an	O
example	O
written	O
in	O
Java	B-Language
that	O
takes	O
keyboard	O
input	O
and	O
handles	O
each	O
input	O
line	O
as	O
an	O
event	O
.	O
</s>
<s>
When	O
a	O
string	O
is	O
supplied	O
from	O
System.in,	O
the	O
method	O
notifyObservers( )	O
is	O
then	O
called	O
in	O
order	O
to	O
notify	O
all	O
observers	O
of	O
the	O
event	O
's	O
occurrence	O
,	O
in	O
the	O
form	O
of	O
an	O
invocation	O
of	O
their	O
update	O
methods	B-Language
.	O
</s>
<s>
JavaScript	O
has	O
a	O
deprecated	B-General_Concept
function	O
that	O
was	O
a	O
more	O
accurate	O
implementation	O
of	O
the	O
observer	B-Language
pattern	I-Language
.	O
</s>
<s>
Without	O
the	O
deprecated	B-General_Concept
function	O
,	O
the	O
pattern	O
may	O
be	O
implemented	O
with	O
more	O
explicit	O
code	O
:	O
</s>
