1 # observe.tcl --
2 #
3 # This file implements the Observer pattern. An Observable
4 # object can attach one or more Observer objects to itself.
5 # This provide a convinient and extensible way of calling
6 # a given method on a set of objects of different classes.
7 #
8 # Copyright (c) 1997-2002 The Regents of the University of California.
9 # All rights reserved.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are met:
13 #
14 # A. Redistributions of source code must retain the above copyright notice,
15 # this list of conditions and the following disclaimer.
16 # B. Redistributions in binary form must reproduce the above copyright notice,
17 # this list of conditions and the following disclaimer in the documentation
18 # and/or other materials provided with the distribution.
19 # C. Neither the names of the copyright holders nor the names of its
20 # contributors may be used to endorse or promote products derived from this
21 # software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
24 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #
34 # @(#) $Header: /usr/mash/src/repository/mash/mash-1/tcl/common/observe.tcl,v 1.8 2002/02/03 04:25:43 lim Exp $
35
36
37 import mashutils
38
39 Class Observer
40
41 Observer instproc init { args } {
42 eval [list $self] next $args
43 }
44
45 Observer instproc update { method args } {
46 if [$self has_method $method] {
47 eval [list $self] [list $method] $args
48 }
49 }
50
51 Class Observable
52
53 Observable instproc init { args } {
54 eval [list $self] next $args
55 $self set observers_ { }
56 }
57
58 Observable instproc attach_observer { observer } {
59 $self instvar observers_
60 lappend observers_ $observer
61 }
62
63 Observable instproc detach_observer { observer } {
64 $self instvar observers_
65 set idx [lsearch $observers_ $observer]
66 if { $idx != -1 } {
67 set observers_ [lreplace $observers_ $idx $idx]
68 }
69 }
70
71 Observable instproc notify_observers { method args } {
72 $self instvar observers_
73 if [info exists observers_] {
74 foreach observer $observers_ {
75 eval [list $observer] update [list $method] $args
76 }
77 }
78 }
79
80
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.