What I ended up doing was having my Computer class be the observable and then having the instance of antivirus that is created by each computer observe for virus infection. Whenever a Computers boolean isInfected variable is set to true, changed() is called which updates the antivirus instance. When antivirus gets wind of an infection, it checks to make sure that it's enabled and then calls setInfected(false) which sets the isInfected boolean in the computer class to false. This would cause antivirus to get updated again, but since the computer isn't infected, it doesn't do anything.
The second place I implemented the pattern was to have each instance of Computer be an observer and antivirus the observable. This lets the computer's know when their antivirus has been turned on or off. I'm not sure I'll end up using this implementation farther down the line, but it seemed like as good of a place as any to stick the second observer implementation for now as I'm still running with a fairly small amount of classes.
I'd like to mention that I found the extra lecture and lab time devoted to the observer pattern extremely beneficial for my understanding of the pattern. I also met with Daniel earlier in the week to run my ideas past him, which was very positive as well.
No comments:
Post a Comment