java: puzzle about generics structure -


i have puzzle creation of structure: interface transform represent general physical transformation, , class poincaretransform represent specific type of transformation. thing this

public interface transform {    public transform compose(transform t); }  public class poincaretransform implements transform {    private matrix matrix;    public transform compose(transform t) {    ...    } } 

but method compose(transform t) take poicaretransform because necessary have matrix compose with. possible solution using generics

public interface transform<t extends transform<t>> {    public t compose(t t); }  public class poincaretransform implements transform<poincaretransform> {    private matrix matrix;    public poincaretransform compose(poincaretransform t) {    ...    } } 

but not satisfactory because not conceptually clean, elegant , there trouble again: poincaretransform subclasses have same problem before. also, definition, have generics around project!

i'm conceptually wrong? how build structure have in mind?

i think go first solution, return error if unable matrix given transform in poincaretransform.transform(transform t).

for example:

public class poincaretransform implements transform {    private matrix matrix;    public transform compose(transform t) {      if (!t.hasmatrix()) {        // throw exception, or other meaningful error.      }      ...    } } 

this way, generic if end different kind of transform matrix, can use it.

on other hand, if want operate on poincaretransforms, can check see if instance passed in correct using instanceof operator.

public class poincaretransform implements transform {    private matrix matrix;    public transform compose(transform t) {      if (!t instanceof poincartransform) {        // throw exception, or other meaningful error.      }      poincartransform p = (poincartransform)t;      ...    } } 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -