implementing the Observer Pattern (way more tedious than it had to be, thanks to me)

hello everybody! I have almost finished the damn code by now!!!! I decided that making it REAL weird would be the best way to squeeze all the fun out of it with my white knuckled fist, and then throw it aside to cry itself to sleep on some cold wet rock.

ANYWAY, here’s how the process went for me. I first off wrote the extension of my code concerning the Observer Pattern down on paper, this included what I had already constructed with the strat pattern. This was no problem, I believe it was a good comprehensible diagram, the only ambiguous bits were more game affiliated than pattern affiliated. The interfaces were easy to understand and the over all idea was pretty rock solid in my head.

Second, I began working, and it went smoothly laying out the foundation of the code, I had decided to NOT use the Java API observer thing because of the limitations it may present with needing to extend the subject (I think it’s the subject, I’ll have to look that up). I felt that while perhaps their may be a slightly smaller bit of work involved, I would be better for it later when I realized the whole hierarchy of what I am doing is compromised because I decided to extend the subject instead of implement it(so avoiding this hypothetical mishap). Now, coding the stucture worked, but once I came upon the ambiguous bits of my code, and also realized I had payed way to much attention to the interfaces and not enough to the other specifics, I began to run into problems. Namely, I was really worried about not knowing how to differentiate my two observers (wolf and bear) because at the moment my code affects both objects data fields when setSituation() is called. That is big problem number one. My second big worry that I hope to have resolved soon is that I am not sure what is a second application of the observer pattern. Does this mean I need two subjects? Or does it mean that I need them to react to a completely different setSituation() method that apply’s to something totally different in my game? answering these questions is what has taken up most of my time.

In my journey to try and somehow remedy my two issues, I tried several different approaches. Concerning number one, trying to differentiate wolf and bear so that setSituation() would only affect one or the other or both, depending on how I choose to do it, I created a method called getStats(). Now ultimately I abandoned the idea because I realized that I was really going to go nowhere without a little guidance. The getStats was intended to retrieve a specific object(such as wolf) and its related attributes. I had hoped that I could use this somehow in combination with setSituation so that it would only apply to either wolf or bear. Initially i had getStats() looking like this:

getStats(Subject s, int hp…) where the subject would be whatever object I needed retrieving, but ultimately, I was not able to logic it out, so I scrapped it. That’s my first big question, how do I differentiate two different observers that share the same set method and same  Subject?

Second issue,  the second application of the observer pattern. I had planned on working this out by having an out of combat observer pattern. Now technically, this will either initiate combat, scare away an object, or gain an object as an ally, and is a way to build interactions between objects and resolve their future relationship. So upon realizing I had no idea how to differentiate update() methods, I decided to go ahead and make two observer interfaces (<—bad idea) each with it’s own specific update method. this way, things that you couldn’t be diplomatic with but could still do you harm (like napalm) would only implement CombatObserver, while things that can both fight and chat will implement both interfaces and both methods. I ran in circles a good deal here trying ot get them to work, ultimately realizing “now how do I make two different update()s print out on one display()?” answer, I couldn’t figure it out. So I crunched down the interfaces to one, renamed it observer, deleted the second subject interface I had made while running nowhere, and renamed CombatDATA to gameDATA, and deleted the other observer. The update method now takes in five different ints, and the display now works for both wolf and bear, and I intend to make a display that reacts to the differing values much more interestingly. At the moment however, it merely reads off whether something is being wounded.  So the second question is, what would I do for the second implementation of the observer method? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.