I just answered another question (Select method based on field in class), and I was wondering if the pattern had a name.
Call action.applyX(a)
, where X
depends on some property of a
(e.g. type
in the example), so you instead call a.apply(action)
and let a
(or Type
) call the appropriate applyX
.
Is there a name for that?
public enum Type {
INTEGER {
@Override
public void apply(Action action, A a) {
action.applyInteger(a);
}
},
STRING {
@Override
public void apply(Action action, A a) {
action.applyString(a);
}
};
public abstract void apply(Action action, A a);
}
public interface Action {
public void applyInteger(A a);
public void applyString(A a);
}
public class A {
private Type type;
...
public void apply(Action action) {
this.type.apply(action, this);
}
}
Aucun commentaire:
Enregistrer un commentaire