1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
</copyright>
<chapter name="Object-Oriented Design Libraries">
<p/>
One of the interesting things about Ruby is the way it blurs the
distinction between design and implementation. Ideas that have to be
expressed at the design level in other languages can be implemented
directly in Ruby.
<p/>
To help in this process, Ruby has support for some design-level strategies.
<p/>
<ul>
<li> <b>The Visitor pattern</b> (Design Patterns,
) is a way of traversing a collection
without having to know the internal organization of that collection.
</li><li> <b>Delegation</b> is a way of composing classes more flexibly and
dynamically than can be done using standard inheritance.
</li><li> <b>The Singleton pattern</b> is a way of ensuring that only
one instantiation of a particular class exists at a time.
</li><li> <b>The Observer pattern</b> implements a protocol allowing one
object to notify a set of interested objects when certain changes
have occurred.
</li></ul>
<p/>
Normally, all four of these strategies require explicit code each time
they're implemented. With Ruby, they can be abstracted into a library
and reused freely and transparently.
<p/>
Before we get into the proper library descriptions, let's get the
simplest strategy out of the way.
<p/>
<section>The Visitor Pattern</section>
<p/>
It's the method <meth>each</meth>.
<p/>
<library name="delegate">
<p/>
Object delegation is a way of <em>composing</em> objects---extending an
object with the capabilities of another---at runtime. This promotes
writing flexible, decoupled code, as there are no compile-time
dependencies between the users of the overall class and the delegates.
<p/>
The Ruby <classname>Delegator</classname> class implements a simple but powerful
delegation scheme, where requests are automatically forwarded from a
master class to delegates or their ancestors, and where the delegate
can be changed at runtime with a single method call.
<p/>
The <tt>delegate.rb</tt> library provides two mechanisms for allowing an
object to forward messages to a delegate.
<p/>
<ol>
<p/>
<li> For simple cases where the class of the delegate is fixed,
arrange for the master class to be a subclass of
<classname>DelegateClass</classname>, passing the name of the class to be
delegated as a parameter (Example 1). Then, in your class's
<meth>initialize</meth> method, you'd call the superclass, passing in
the object to be delegated. For example, to declare a class
<classname>Fred</classname> that also supports all the methods in <classname>Flintstone</classname>, you'd
write
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ class Fred < DelegateClass(Flintstone)
def initialize
# ...
super(Flintstone.new(...))
end
# ...
end
]]></fullcode>
class<nbsp/>Fred<nbsp/><<nbsp/>DelegateClass(Flintstone)
<nbsp/><nbsp/>def<nbsp/>initialize
<nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>...
<nbsp/><nbsp/><nbsp/><nbsp/>super(Flintstone.new(...))
<nbsp/><nbsp/>end
<nbsp/><nbsp/>#<nbsp/>...
<nbsp/>end
</alltt>
</codefragment>
This is subtly different from using subclassing. With subclassing,
there is only one object, which has the methods and the defined
class, its parent, and their ancestors. With delegation there are
two objects, linked so that calls to one may be delegated to the
other.
<p/>
</li><li> For cases where the delegate needs to be dynamic, make the
master class a subclass of <classname>SimpleDelegator</classname> (Example 2). You
can also add delegation capabilities to an existing object using
<classname>SimpleDelegator</classname> (Example 3). In these cases, you can call the
<meth>__setobj__</meth> method in <classname>SimpleDelegator</classname> to
change the object being delegated at runtime.
<p/>
</li></ol>
<p/>
<b>Example 1.</b> Use the <classname>DelegateClass</classname> method and subclass
the result when you need a class with its own behavior that also
delegates to an object of another class. In this example, we assume
that the <tt>@sizeInInches</tt> array is large, so we want only one copy
of it. We then define a class that accesses it, converting the
values to feet.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ require 'delegate'
sizeInInches = [ 10, 15, 22, 120 ]
class Feet < DelegateClass(Array)
def initialize(arr)
super(arr)
end
def [](*n)
val = super(*n)
case val.type
when Numeric; val/12.0
else; val.collect {|i| i/12.0}
end
end
end
]]></fullcode>
require<nbsp/>'delegate'
<p/>
sizeInInches<nbsp/>=<nbsp/>[<nbsp/>10,<nbsp/>15,<nbsp/>22,<nbsp/>120<nbsp/>]
<p/>
class<nbsp/>Feet<nbsp/><<nbsp/>DelegateClass(Array)
<nbsp/><nbsp/>def<nbsp/>initialize(arr)
<nbsp/><nbsp/><nbsp/><nbsp/>super(arr)
<nbsp/><nbsp/>end
<nbsp/><nbsp/>def<nbsp/>[](*n)
<nbsp/><nbsp/><nbsp/><nbsp/>val<nbsp/>=<nbsp/>super(*n)
<nbsp/><nbsp/><nbsp/><nbsp/>case<nbsp/>val.type
<nbsp/><nbsp/><nbsp/><nbsp/>when<nbsp/>Numeric;<nbsp/>val/12.0
<nbsp/><nbsp/><nbsp/><nbsp/>else;<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>val.collect<nbsp/>{|i|<nbsp/>i/12.0}
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
end
</alltt>
</codefragment>
<p/>
<codefragment>
<fullcode><![CDATA[!- require 'delegate'
!- sizeInInches = [ 10, 15, 22, 120 ]
!-
!- class Feet < DelegateClass(Array)
!- def initialize(arr)
!- super(arr)
!- end
!- def [](*n)
!- val = super(*n)
!- case val.type
!- when Numeric; val/12.0
!- else; val.collect {|i| i/12.0}
!- end
!- end
!- end
sizeInFeet = Feet.new(sizeInInches)
sizeInInches[0..3]
sizeInFeet[0..3]
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>sizeInFeet<nbsp/>=<nbsp/>Feet.new(sizeInInches)</tt></td>
</tr>
<tr>
<td><tt>sizeInInches[0..3]</tt></td>
<td>»</td>
<td><tt>[10,<nbsp/>15,<nbsp/>22,<nbsp/>120]</tt></td>
</tr>
<tr>
<td><tt>sizeInFeet[0..3]</tt></td>
<td>»</td>
<td><tt>[0.8333333333,<nbsp/>1.25,<nbsp/>1.833333333,<nbsp/>10.0]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
<b>Example 2.</b> Use subclass <classname>SimpleDelegator</classname> when you want an
object that both has its own behavior <em>and</em> delegates to
different objects during its lifetime. This is an example of the
State
pattern.
Objects of class <classname>TicketOffice</classname> sell tickets if a
seller is available, or tell you to come back tomorrow if there is
no seller.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ require 'delegate'
class TicketSeller
def sellTicket()
return 'Here is a ticket'
end
end
class NoTicketSeller
def sellTicket()
"Sorry-come back tomorrow"
end
end
class TicketOffice < SimpleDelegator
def initialize
@seller = TicketSeller.new
@noseller = NoTicketSeller.new
super(@seller)
end
def allowSales(allow = true)
__setobj__(allow ? @seller : @noseller)
allow
end
end
]]></fullcode>
require<nbsp/>'delegate'
<p/>
class<nbsp/>TicketSeller
<nbsp/><nbsp/>def<nbsp/>sellTicket()
<nbsp/><nbsp/><nbsp/><nbsp/>return<nbsp/>'Here<nbsp/>is<nbsp/>a<nbsp/>ticket'
<nbsp/><nbsp/>end
end
<p/>
class<nbsp/>NoTicketSeller
<nbsp/><nbsp/>def<nbsp/>sellTicket()
<nbsp/><nbsp/><nbsp/><nbsp/>"Sorry-come<nbsp/>back<nbsp/>tomorrow"
<nbsp/><nbsp/><nbsp/>end
end
<p/>
class<nbsp/>TicketOffice<nbsp/><<nbsp/>SimpleDelegator
<nbsp/><nbsp/>def<nbsp/>initialize
<nbsp/><nbsp/><nbsp/><nbsp/>@seller<nbsp/>=<nbsp/>TicketSeller.new
<nbsp/><nbsp/><nbsp/><nbsp/>@noseller<nbsp/>=<nbsp/>NoTicketSeller.new
<nbsp/><nbsp/><nbsp/><nbsp/>super(@seller)
<nbsp/><nbsp/>end
<nbsp/><nbsp/>def<nbsp/>allowSales(allow<nbsp/>=<nbsp/>true)
<nbsp/><nbsp/><nbsp/><nbsp/>__setobj__(allow<nbsp/>?<nbsp/>@seller<nbsp/>:<nbsp/>@noseller)
<nbsp/><nbsp/><nbsp/><nbsp/>allow
<nbsp/><nbsp/>end
end
</alltt>
</codefragment>
<p/>
<codefragment>
<fullcode><![CDATA[!- require 'delegate'
!-
!- class TicketSeller
!- def sellTicket()
!- return 'Here is a ticket'
!- end
!- end
!-
!- class NoTicketSeller
!- def sellTicket()
!- "Sorry-come back tomorrow"
!- end
!- end
!-
!- class TicketOffice < SimpleDelegator
!- def initialize
!- @seller = TicketSeller.new
!- @noseller = NoTicketSeller.new
!- super(@seller)
!- end
!- def allowSales(allow = true)
!- __setobj__(allow ? @seller : @noseller)
!- allow
!- end
!- end
to = TicketOffice.new
to.sellTicket
to.allowSales(false)
to.sellTicket
to.allowSales(true)
to.sellTicket
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>to<nbsp/>=<nbsp/>TicketOffice.new</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Here<nbsp/>is<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
<tr>
<td><tt>to.allowSales(false)</tt></td>
<td>»</td>
<td><tt>false</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Sorry-come<nbsp/>back<nbsp/>tomorrow"</tt></td>
</tr>
<tr>
<td><tt>to.allowSales(true)</tt></td>
<td>»</td>
<td><tt>true</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Here<nbsp/>is<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
<b>Example 3.</b> Create <classname>SimpleDelegator</classname> objects when you want
a single object to delegate all its methods to two or more other
objects.
<p/>
<codefragment>
<fullcode><![CDATA[ # Example 3 - delegate from existing object
!- require 'delegate'
!- class TicketSeller
!- def sellTicket()
!- "Here's a ticket"
!- end
!- end
!-
!- class NoTicketSeller
!- def sellTicket()
!- "Sorry-come back tomorrow"
!- end
!- end
!-
seller = TicketSeller.new
noseller = NoTicketSeller.new
to = SimpleDelegator.new(seller)
to.sellTicket
to.sellTicket
to.__setobj__(noseller) #!sh!
to.sellTicket
to.__setobj__(seller) #!sh!
to.sellTicket
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>#<nbsp/>Example<nbsp/>3<nbsp/>-<nbsp/>delegate<nbsp/>from<nbsp/>existing<nbsp/>object</tt></td>
</tr>
<tr>
<td colspan="3"><tt>seller<nbsp/><nbsp/><nbsp/>=<nbsp/>TicketSeller.new</tt></td>
</tr>
<tr>
<td colspan="3"><tt>noseller<nbsp/>=<nbsp/>NoTicketSeller.new</tt></td>
</tr>
<tr>
<td colspan="3"><tt>to<nbsp/>=<nbsp/>SimpleDelegator.new(seller)</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Here's<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Here's<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>to.__setobj__(noseller)</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Sorry-come<nbsp/>back<nbsp/>tomorrow"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>to.__setobj__(seller)</tt></td>
</tr>
<tr>
<td><tt>to.sellTicket</tt></td>
<td>»</td>
<td><tt>"Here's<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</library>
<p/>
<library name="observer">
<p/>
The Observer pattern, also known as
Publish/Subscribe,
provides a simple mechanism for one object to
inform a set of interested third-party objects when its state
changes.
<p/>
In the Ruby implementation, the notifying class mixes in the
<modulename>Observable</modulename> module, which provides the methods for managing the
associated observer objects.
<table>
<p/>
<toprule/><tr>
<td>add_observer(<obj>obj</obj>)</td>
<td>Add <obj>obj</obj> as an observer on this
object. <obj>obj</obj> will now receive notifications.</td>
</tr>
<tr>
<td>delete_observer(<obj>obj</obj>)</td>
<td>Delete <obj>obj</obj> as an observer on this
object. It will no longer receive notifications.</td>
</tr>
<tr>
<td>delete_observers</td>
<td>Delete all observers associated with this
object.</td>
</tr>
<tr>
<td>count_observers</td>
<td>Return the count of observers associated with this
object.</td>
</tr>
<tr>
<td>changed(<obj>newState</obj>=<const>true</const>)</td>
<td>Set the changed state of this
object. Notifications will be sent only if the changed state is
<const>true</const>.</td>
</tr>
<tr>
<td>changed?</td>
<td>Query the changed state of this object.</td>
</tr>
<tr>
<td>notify_observers(<obj>*args</obj>)</td>
<td>If this object's changed state is
true, invoke the <meth>update</meth> method in each currently associated
observer in turn, passing it the given arguments. The changed state is then
set to <const>false</const>.</td>
</tr>
<bottomrule/></table>
<p/>
The observers must implement the <meth>update</meth> method to receive
notifications.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[!-class Price
!- @p = [ 83, 75, 90, 134, 134, 112, 79 ]
!- @i = 0
!- def Price.fetch(symbol)
!- exit if @i >= @p.length
!- p = @p[@i]
!- @i += 1
!- p
!- end
!-end
require "observer"
class Ticker # Periodically fetch a stock price
include Observable
def initialize(symbol)
@symbol = symbol
end
def run
lastPrice = nil
loop do
price = Price.fetch(@symbol)
print "Current price: #{price}\n"
if price != lastPrice
changed # notify observers
lastPrice = price
notify_observers(Time.now, price)
end
end
end
end
class Warner
def initialize(ticker, limit)
@limit = limit
ticker.add_observer(self) # all warners are observers
end
end
class WarnLow < Warner
def update(time, price) # callback for observer
if price < @limit
print "--- #{time.to_s}: Price below #@limit: #{price}\n"
end
end
end
class WarnHigh < Warner
def update(time, price) # callback for observer
if price > @limit
print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
end
end
end
ticker = Ticker.new("MSFT")
WarnLow.new(ticker, 80)
WarnHigh.new(ticker, 120)
ticker.run
]]></fullcode>
require<nbsp/>"observer"
<p/>
<nbsp/><nbsp/>class<nbsp/>Ticker<nbsp/>#<nbsp/>Periodically<nbsp/>fetch<nbsp/>a<nbsp/>stock<nbsp/>price
<nbsp/><nbsp/><nbsp/><nbsp/>include<nbsp/>Observable
<p/>
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>initialize(symbol)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>@symbol<nbsp/>=<nbsp/>symbol
<nbsp/><nbsp/><nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>run
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>lastPrice<nbsp/>=<nbsp/>nil
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>loop<nbsp/>do
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>price<nbsp/>=<nbsp/>Price.fetch(@symbol)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"Current<nbsp/>price:<nbsp/>#{price}\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>if<nbsp/>price<nbsp/>!=<nbsp/>lastPrice
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>changed<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>notify<nbsp/>observers
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>lastPrice<nbsp/>=<nbsp/>price
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>notify_observers(Time.now,<nbsp/>price)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/>class<nbsp/>Warner
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>initialize(ticker,<nbsp/>limit)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>@limit<nbsp/>=<nbsp/>limit
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>ticker.add_observer(self)<nbsp/><nbsp/><nbsp/>#<nbsp/>all<nbsp/>warners<nbsp/>are<nbsp/>observers
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/>class<nbsp/>WarnLow<nbsp/><<nbsp/>Warner
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>update(time,<nbsp/>price)<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>callback<nbsp/>for<nbsp/>observer
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>if<nbsp/>price<nbsp/><<nbsp/>@limit
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"---<nbsp/>#{time.to_s}:<nbsp/>Price<nbsp/>below<nbsp/>#@limit:<nbsp/>#{price}\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/>class<nbsp/>WarnHigh<nbsp/><<nbsp/>Warner
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>update(time,<nbsp/>price)<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>callback<nbsp/>for<nbsp/>observer
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>if<nbsp/>price<nbsp/>><nbsp/>@limit
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"+++<nbsp/>#{time.to_s}:<nbsp/>Price<nbsp/>above<nbsp/>#@limit:<nbsp/>#{price}\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
ticker<nbsp/>=<nbsp/>Ticker.new("MSFT")
WarnLow.new(ticker,<nbsp/>80)
WarnHigh.new(ticker,<nbsp/>120)
ticker.run
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Current<nbsp/>price:<nbsp/>83
Current<nbsp/>price:<nbsp/>75
---<nbsp/>Sun<nbsp/>Mar<nbsp/>04<nbsp/>23:26:31<nbsp/>CST<nbsp/>2001:<nbsp/>Price<nbsp/>below<nbsp/>80:<nbsp/>75
Current<nbsp/>price:<nbsp/>90
Current<nbsp/>price:<nbsp/>134
+++<nbsp/>Sun<nbsp/>Mar<nbsp/>04<nbsp/>23:26:31<nbsp/>CST<nbsp/>2001:<nbsp/>Price<nbsp/>above<nbsp/>120:<nbsp/>134
Current<nbsp/>price:<nbsp/>134
Current<nbsp/>price:<nbsp/>112
Current<nbsp/>price:<nbsp/>79
---<nbsp/>Sun<nbsp/>Mar<nbsp/>04<nbsp/>23:26:31<nbsp/>CST<nbsp/>2001:<nbsp/>Price<nbsp/>below<nbsp/>80:<nbsp/>79
</alltt>
</codefragment>
<p/>
</library>
<p/>
<library name="singleton">
<p/>
The Singleton design pattern ensures that only
one instance of a particular class may be
created.
<p/>
The <tt>singleton</tt> library makes this simple to implement. Mix
the <modulename>Singleton</modulename> module into each class that is to be a singleton,
and that class's <meth>new</meth> method will be made private. In its
place, users of the class call the method <meth>instance</meth>, which
returns a singleton instance of that class.
<p/>
In this example, the two instances of <tt>MyClass</tt> are the same object.
<p/>
<codefragment>
<fullcode><![CDATA[ require 'singleton'
class MyClass
include Singleton
end
a = MyClass.instance
b = MyClass.instance
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>require<nbsp/>'singleton'</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td colspan="3"><tt>class<nbsp/>MyClass</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>include<nbsp/>Singleton</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td><tt>a<nbsp/>=<nbsp/>MyClass.instance</tt></td>
<td>»</td>
<td><tt>#<MyClass:0x4018c924></tt></td>
</tr>
<tr>
<td><tt>b<nbsp/>=<nbsp/>MyClass.instance</tt></td>
<td>»</td>
<td><tt>#<MyClass:0x4018c924></tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</library>
<p/>
</chapter>
</ppdoc>
|