December 21, 2024

An example of using SortedCollection

 

By extending the gallery scenario, it is possible to demonstrate a use for SortedCollection.

For many years the gallery have represented Bob Beattie, a dyed in the wool "action painter", or as his detractors would say, a "slap and tickler" of the old St. Martins School of Art. Unfortunately for Bob, after years of critical neglect, he died shortly after winning the coveted Chivas Regal painting prize.
"Darling, the shock must have killed him." was Cordelia's initial response.
But never one to let the grim reaper spoil an opportunity, Cordelia sets to work and organizes a retrospective of Bob's work.

Bob was, if nothing else, prolific. Four wives, eight children and a recent paternity claim against him, are testament to his prodigious output. He also produced a lot of art. Painting, drawing, print-making and sculpture all yielded to Bob's touch. Mind you, so did Cordelia, which is why the gallery holds an indecent amount of Bob's work.

A sorted collection is deemed necessary for cataloguing purposes. The intention is to sort Bob's work by the date it was made.

A variable retrospectiveCollection will reference this collection. A sort block needs to be constructed that will set the sorting criterion. Finally, all the instances of Painting, Drawing, Print and Sculpture will be added. So how is this achieved? Well, to quote Bob, "Don't fight it, just enjoy it."

A class method sortBlock: is used that not only creates the new instance of the class, but also takes, as its argument, a block specifying the sorting criterion.

retrospectiveCollection := SortedCollection sortBlock: [:predecessor :successor| predecessor yearCreated <= successor yearCreated].
retrospectiveCollection add: painting15;
                        add: painting27;
                        add: print7;
                        add: drawing132;
                        add: sculpture4       "and so on and so forth"

Objects of a different class have been added to the sorted collection. You might think that this would create problems when attempting to sort them. However, in this case, all the added objects possess the message selector yearCreated in their protocol. This message has been inherited from the abstract superclass Artwork. A sort block has been constructed by setting a sorting criterion that compares the values referenced by an instance variable of those classes. This has to be carried out prior to the addition of the objects. If not an exception would occur, because the default sorting criterion would not allow these objects to be added to the collection. For instance, painting15 would not "understand" the message
<= painting27

The collection of Bob's work is sorted from oldest to most recent and the gallery can now press ahead with the show.

Next page » Queues

Previous page « SortedCollection

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page