Skip to Main Content
Prefactoring
book

Prefactoring

by Ken Pugh
September 2005
Intermediate to advanced content levelIntermediate to advanced
240 pages
6h 28m
English
O'Reilly Media, Inc.
Content preview from Prefactoring

5.5. More Than One

You often work with more than one instance of an object. You can aggregate those instances using an array or a collection class. I like to distinguish between two types of aggregations. The first is termed a group, the second a collection. A group is a set of objects, typically coded using an array or a linked list. When I use the [] symbol in a class description, I am implying a group, not necessarily a particular implementation of a group.

A collection is a set of objects, typically implemented by a collection class or template (Vector, List, etc.). Operations on a collection can include performing an operation on the collection as a whole, such as calculating the average value of an attribute for all objects in the collection, or finding a particular object matching a key value.

If you find you are performing more operations on a group, other than just passing it to other methods or enumerating it, you might want to turn it into its own class. Creating a specific collection class, instead of using classwide (static) methods, separates the concerns of operations on all the objects in a collection, from those operations on a single object.[*]

[*] A reviewer noted that this is not true in languages such as Ruby and Groovy, which support closures. Closures let you apply arbitrary operations to generic collections without resorting to creating a specific collection class.

For example, suppose that Sam wanted to keep track of the rental history of each ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Understanding Unittest.Mock

Understanding Unittest.Mock

Mario Corchero
Java™ Performance

Java™ Performance

Charlie Hunt, Binu John

Publisher Resources

ISBN: 0596008740Supplemental ContentCatalog PageErrata