Friday, April 18, 2008

Singleton and Threading

I've realized that the best(only) way to correctly implement the MVC + Log design of RPChess is to have each part Model, View, and Controller as a separate thread. And have the log be a singleton that all of the classes have as a member. Now, the problem of thread safety. I could get away without thread safety (or threads) in the current implementation, since it will be turn-based (and non graphical). But for future implementations the 2D graphics, definitely the 3D graphics will need threads and thread safety. Who writes to the Log? Who reads from the Log? How often does everyone read and write from the log? To signal the need for an update when a move is added to the log, an event should be raised so all the threads update their perspective assets synchronously.

The other issue. Do I really need thread safety now? I'm starting to like Test Driven and KISS based methodology. Like Extreme Programming. It's all about getting results now. I've always had a problem with adding to many features. I recently removed the BoardVector constructor in the Movement class, because a) anything you can do with BoardVector you can do with BoardLocation and 2) I didn't need it, it was making things confusing and bloating the code. I already needed to have XML Documentation for all public things, I don't need to be writing documentation for a hundred things. The documentation for all the BoardVector methods paralleled the BoardLocation. I think I'll start by reading up on thread safety and singletons, as well as their implementations in C# and then I'll make a better decision. But I feel like at least I'll start on the singleton interface.

[Update]
The way I wanted to implement the singleton pattern is actually called a Monostate. Here there are many instances of Log, but the important member data is all static. I am not implementing thread safety.

No comments: