jeudi 13 octobre 2016

Call inferface methods in different order

I have one interface with several methods:

interface IExample {
 methodA();
 methodB();
 methodC();
 ...
 methodN();
}

I also have several implementations of that interface (e.g class A, class B...). I also have HashMap where I put those implementations based on specific key:

commands = new HashMap<String, IExample>();
commands.put("key1", new A());
.
.
commands.put("keyN", new N());

I use strategy design pattern to fetch each implementation when some event occur:

Event event = ...
IExample example = UtilClass.commands.get(event.getKey());

and now I am able to call methods on particular implementation:

example.methodA();
...

The problem is that depending on the event, method call order is different. So, for key1 call order is:

example.methodC();
example.methodA();
...

but for different key, let's say key2, method call order is:

example.methodA()
example.methodC()
...

What design pattern or approach I can use in order solve this problem in a easy and clean way? Not to use something like this:

 if (example instance of A) {    call order for A... }

 if (example instance of B) {    call order for B... }

Aucun commentaire:

Enregistrer un commentaire