Hello!
I am currently making a turn based game written in Java. I would like to implement a 'Combat Log' feature that displays what is currently happening in the game (e.g. "Orc hits you for X damage!") . The game will not utilize the console, so I can not just print these messages to the console as they occur. I was studying the Observer Pattern and I think that I can use it to my advantage; however, I am not sure how I should implement the Observer into my system. I have the UML for three different approaches, and I would like to know which one (if any) you all would recommend. The classes that I have provided in the following UML are simplified so that I can get my point across without wasting too much of your time:
1. Static Observer
The first thought I had was making the Observer class static. The Observer class would have a private static member variable in the form of a collection; the collection would be used to store each message. The Observer class would also have a public static method used to add a new message to the collection and a public static method used to retrieve the collection. I am really not fond of this design because it reminds me of a global variable !
2. Singleton Observer
The second design I thought of was implementing the Observer class as a Singleton. A Singleton version of the Observer class would have a collection in the form of an instance variable instead of a static variable. I like this design more than the Static Observer design that I previously explained, but it still makes me feel like I am cheating. I have also read that most people are not very fond of the Singleton pattern for various reasons.
3. 'Has-A' Observer
The third design that I have contemplated is having the Observer be a part of the class composition of each class that it needs to observe. This design seems like it follows the Observer Pattern to a better degree. My only concern with this design is the number of objects that the Observer will be observing. There will be 8 x Hero objects to observe, and there will be 64 x Skill objects to observe; there will eventually be many conditional effects on each of the Heros that I think the Observer will need to keep an eye on as well. Should I worry about the number of objects that the Observer is watching?
So, these are the three designs that I have drawn up to help me solve this problem. I would appreciate any insight on which one of these may be the best option to use (or an alternative if anyone has an ideas). Thank you for your help, and I hope that I made my thoughts clear to you.