A nice pattern

Michael van der Gulik squeakml at gulik.co.nz
Mon Aug 21 11:09:46 UTC 2006


Stéphane Ducasse wrote:
> I'm happy to find it on the web
> for this is a nice pattern that is the first level towards metamodeling.
> 
> http://www.ksc.com/article3.htm
> 
> (as an old CLOS Moper I do not like the aka MetaObject because the  
> typeObject is not really that
> but this is a really cool and important pattern).

I've got a better one:

The Aggregator pattern
by Michael van der Gulik

Intent

This pattern allows the programmer to associate many different instances 
of one class with a number of other instances of that same class. This 
allows the programmer, using only a single reference, to locate any of 
the instances of those classes within the grouping.

Also Known As

PowerBag[EeL93], OrderedCollection[ToAd94], BetaSet[fiSH98].

Motivation

Sometimes the complexity of a system requires that many objects of the 
same class need to be aggregated into the same location of the system. 
The programmer often needs to locate instances of unknown classes in the 
system depending on some common attribute of the instance's type.

Consider, for example, a system for tracking the videotapes in a video 
rental store's inventory. The system would obviously require a class 
called "Videotape." Each instance of Videotape would represent one of 
the videotapes in the store's inventory.

However, since many of the videotapes are very similar, it will be 
difficult to locate different instances of Videotape. Many copies of The 
Terminator would stored in the same general location as copies of Star Wars.

One way to solve this problem is to have a lot of instance variables, 
each pointing to a unique instance of Videotape. Thus, there would be 
separate instance variables pointing to "Star Wars" and "The 
Terminator". In order to keep the system flexible, the programmer would 
also need to create a number of spare instance variables for future use.

This solution works, but not very well. If the programmer wants to add 
another tape, such as "Bambi", and the pool of instance variables is 
depleted, then the programmer will need to add new instance variables 
and recompile the program.

Instead, since the number of instances of VideoTape is unknown 
beforehand, an Aggregator pattern could be used. The aggregator is a 
single reference to a complex data structure, which can hold any number 
of instances of VideoTape:

Aggregator ---* Videotape
             |
             +-* Videotape

This class diagram illustrates how a single Aggregator pattern can hold 
any number of Videotapes, where the number of Videotapes is not known at 
compile time. The "has a reference to"-relationship between the 
Aggregator reference and the Videotapes is an example of an Aggregator 
pattern.

--

I could go on, but I run the risk of having my work referenced by other 
academic papers...

Michael.




More information about the Squeak-dev mailing list