/**
* This Observer interface is the observer part of the Observer pattern.
* @author Robin Kadergran
*/
public interface Observer {
/**
* The override method for the observer pattern.
*/
default void update(){}
}
/**
* This Subject interface is the subject part of the Observer pattern.
* @author Robin Kadergran
*/
public interface Subject {
/**
* This override method registers the observer to the list.
* @param o Observer parameter.
*/
void register(Observer o);
/**
* This override method unregistered the observer to the list.
* @param o Observer parameter.
*/
void unRegister(Observer o);
/**
* This override method notifies all the observers in the list.
*/
void notifyObserver();
}
Observer in ”action”:
/**
* This subclass contains graphics, images and hitbox linked to the bass
character.
* @author Robin Kadergran
*/
public class Boss extends BaseCharacter implements Observer {
/**
* This constructor adds the Boss class to the observerList in
GameSettings.
* @param gameSetting class parameter.
*/
public Boss(GameSetting gameSetting) {
[Link](this);
}
/**
* This method overrides the superclass BaseCharacter and provides data to
the observer pattern to the viewer.
*/
@Override
public void update(){
LoadAnimations();
ImportEnemyDeathImg();
bossHitBox();
}
}
Subject in “action”:
/**
* The controller part of the MVC pattern and the subject part of the
Observer pattern.
* @author Robin Kadergran
*/
public class GameSetting implements Runnable, Subject {
/**
* This override method registers the observer to the observerList.
* @param newObserver Observer object.
*/
@Override
public void register(Observer newObserver) {
[Link](newObserver);
}
/**
* This override method is for future addition when enemies die and get
unregistered from the observerList.
* @param removeObserver Observer object.
*/
@Override
public void unRegister(Observer removeObserver) {
int observerIndex = [Link](removeObserver);
[Link]("Character " + (observerIndex + 1) + " is died and
looted!");
[Link](observerIndex);
}
/**
* This override method notifies all the observers in the observerlist.
*/
@Override
public void notifyObserver() {
for (Observer observer: observerList){
[Link]();
}
}
/**
* The override run() method of the StartGameLoop()'s fixedThreadPool and
uses the notifyObserver()
* from the Observer pattern.
*/
@Override
public void run() {
long lastFrame = [Link]();
while(true){
double timePerFrame = 1000000000.0 / FPS_SET;
long now = [Link]();
if (now - lastFrame >= timePerFrame){
[Link]();
lastFrame = now;
frames++;
}
if ([Link]() - lastcheck >= 1000){
lastcheck = [Link]();
notifyObserver();
frames = 0;
}
}
}