Singletons

Summary: Redefine or delegate methods of individual instances
Author: Avi Bryant
Owner: Avi Bryant (avi)
Co-maintainers: <None>
Categories:
Homepage:
PackageInfo name: <Not entered>
RSS feed:

Description:

*The delegation methods of this package make use of the ObjectsAsMethods VM support. In a 3.6 image, you must call #recreateSpecialObjectsArray before using this package.*

This package adds the #becomeSingleton method to Object, which will transform the receiver into an instance of a new, anonymous subclass of its original class. This lets you add or redefine methods on an object-by-object basis.

This is intended to be used primarily for delegation - replacing a method with a block or a message send to another object. The methods #to:run:, #to:send:to:, and #delegate:to: are provided for this purpose. The following three calls are all equivalent:


x delegate: #foo: to: someOtherObject.
x to: #foo: send: #foo: to: someOtherObject.
x to: #foo: run: [:arg | someOtherObject foo: arg].


In all three cases, sending #foo: to x will end up forwarding the message to someOtherObject.

My expectation is that this might be useful as a replacement for specifying pluggable behavior with block inst vars or target/selector pairs, particularly for UI classes. Rather than explicitly putting in hooks for pluggablity, a well factored object can be customized from the outside without its knowledge, by overriding existing methods - particularly those that are #subclassResponsibility - but without going to the trouble of adding a named subclass for each use.

Releases


Back