views:

59

answers:

2

Hi all,

I want to implement the concept of a Workspace. This is a global concept - all other code will interact with one instance of this Workspace. The Workspace will be responsible for maintaining the current system state (i.e. interacting with the system model, persisting the system state etc)

So what's the best design strategy for my Workspace, bearing in mind this will have to be testable (using RSpec now, but happy to look at alternatives).

Having read thru some open source projects out there and I've seen 3 strategies. None of which I can identify as "the best practice".

They are:

  1. Include the singleton class. But how testable is this? Will the global state of Workspace change between tests?
  2. Implemented all behaviour as class methods. Again how do you test this?
  3. Implemented all behaviour as module methods. Not sure about this one at all!

Which is best? Or is there another way?

Thanks, Gordon

Edit

As I started including the 'singleton' module in all my code and I realised just how tightly coupled I had made my code, with references to these global instances all over the place.

So I've started to remove them entirely and pass references to the global instances instead. But now I'm heading down the route of IOC - passing dependencies down through my constructers.

Is this a good idea in Ruby? Or am I missing something?

BTW you may have gathered I'm new to Ruby!

A: 

The singleton approach is best choice, performance wise. For it not to change during tests you can use a mock up.

dakull
Yes but how do you test the singleton?
Gordon McAllister
A: 

Maybe the Singleton module from the standard library would be useful? If you include this module in your class, only one instance of it can be created. You then can use instance variables for managing the state of your workspace etc.

If this was what you meant by "Include the singleton class", then I'm sorry, but the term "singleton" gets thrown around quite a bit in Ruby land, usually in this context.

Michael Kohl
Yeah! I tried not using the term at all! But alas ...Cheers!
Gordon McAllister