AutoList Design Pattern

Description: The AutoList design pattern is used when a list of all instances of a class is desired. If a class inherits from AutoList, all instances of that class are inserted to the list as they are created, and are removed when they are destroyed.

Eclipse + OSGI
This is a pretty interesting pattern because it essentially creates a dynamic list objects that are of a certain type (or interface to be exact). This concept is prevalent throughout many other patterns like location services and Eclipse Extension Points. The Eclipse extension point when mixed with OSGI Dynamic loading and unloading is basically the AutoList pattern done in a declarative way. When you provide an extension in Eclipse and your extension implements an interface your newly created object is now part of the Extension registry (somewhat). This means the plugin that has instantiated your object has added it to its list of objects. Mix this with the Extension Registry listener concepts and your plugin can now remove that object from its list when the parent plugin gets unloaded.

So in short, this is a great pattern and can easily be implemented in Java using technologies like OSGI bundles and Eclipse Extensions.

Java 5 Generics
However, how do you do this with basic Java? It is not like you can have multiple inheritance! But wait, Java 5 has the concept of Generics which are implemented as Interfaces. So could you actually write the AutoList pattern using the basic Java language? Almost, you would have to have your implementers write all of the code to properly implement the pattern (much uglier than C++).

Aspect Oriented Programming
Lastly, one thing I thought of was the use of Aspect Oriented Programming (AspectJ) to accomplish this on existing objects that declare similar methods and behavior. Your aspects could inject code in the right places to add and remove the instances from the list. I might have to look into this and see if it is even possible.

Game Programming Gems III
Java 5 Generics
Eclipse AspectJ

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.