0

I am developing a multi player game, which have a module which works on the basis of notifications send by server. For example: Action of other player, score update, action to do etc.

I am receiving notification in json format. I am wondering if there is some codding pattern exist which automatically deliver different notifications to their corresponding handlers. Many thanks for your help.

3
  • A simple approach would to be to use Broadcasting and let each handler listen for the appropriate intent, this way only picking those relevant. Just out of curiosity, are you using GCM or do you repeatedly pull the server for updates? Sep 28, 2013 at 13:13
  • Actually there are about 3 activities which controls game based on notification and each receive 5 to 20 different notifications.
    – Adnan
    Sep 28, 2013 at 13:16
  • No, I am using signal R.
    – Adnan
    Sep 28, 2013 at 13:17

1 Answer 1

1

Well, cannot say if this classifies as a pattern:

My take on it would be to simply create a separate class, lets call it JSONGameStateFilter, to filter the JSON object based on the received value plus the state of the game

Something like:

public class JSONGameStateFilter() {

    public interface GameInterface1 {
        // callback methods for activity 1
        // example: public void newPlayerArrived(String name, int score);
        // ...
    }

    public interface GameInterface2 {
        // callback methods for activity 2
    }

    public interface GameInterface3 {
        // callback methods for activity 3
    }

    private GameInterface1 callback1;
    private GameInterface2 callback2;
    private GameInterface3 callback3;

    private JSONGameStateFilter instance;

    public static JSONGameStateFilter getInstance() {
        if (instance != null) {
            return instance = new JSONGameStateFilter();
        }
    }
    private JSONGameStateFilter() {}

    public void registerListener(GameInterface1 callback) {
        // called by Activity1 implementing GameInterface1 
        // by JSONGameStateFilter.newInstance().registerListener(this);
        this.callback1 = callback;
    }

    public void registerListener(GameInterface2 callback) {
        this.callback2 = callback;
    }

    public void registerListener(GameInterface3 callback) {
        this.callback3 = callback;
    }

    public void filterJSON(JSONObject object) {
        // read JSON and gamestate
        // depending on situation call the right callback
        // example: if (callback1 != null) callback1.newPlayerArrived(name, score)
    }
}

The design of this approach would be to implement varies of callbacks on each activity (known pattern for fragments to communicate back to activity).

This is untested and written just now but I am pretty confident that it would work well.

2
  • Thanks cYrixmorten. I was thinking for same like design but with a little bit difference. It was about using Observer pattern but having HashMap to store each Observer/controller with a key. And using same like filter, instead of registering different GameInterface as above simply add every GameInterface with a key. Please share your views about it.
    – Adnan
    Sep 28, 2013 at 13:52
  • That might indicate that this path is not a bad idea :) Regarding the Observer Pattern, then I think it is a matter of taste. In my mind, though, the Observer Pattern is more generic, if that makes sense, in that anyone can register to the Observer, whereas here there is a predefined set of Objects that should receive tailored information. Both methods would work i am sure. Sep 28, 2013 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.