Saturday, December 6, 2008

Grow Arrays

You've got a simple class that's an implementation of a growable array. Each time an object is put beyond the end of the array, a new array of twice the size of the previous (or large enough to store the object, whichever is larger) is created, the elements are copied over, and the new array is used.

The issue is that you have some legacy code that needs access to an actual array rather than your wrapping class. You can't touch the old code, but you can do anything you want in your code. The old code has a .setArray(array) method that allows you to specify that it should use a different array than the one it's currently using. There may be more than one legacy object using your growable array class.

What are some possible ways to make the legacy code work with your GrowArray? What are the drawbacks and benefits of each?

No comments: