[Javalist] Generics kerdes
aszomor at szomor.hu
aszomor at szomor.hu
2012. Május. 9., Sze, 19:14:22 CEST
Hi Zamek,
Kicsit a kódot átrendezve
interface POJO { int asd(); }
abstract class AbstractPOJO implements POJO { }
OK
interface DAO<P extends POJO> {
P getPojo();
void setPojo(P pojo);
void update();
}
abstract class AbstractDAO<P extends POJO> implements DAO<P> {
P pojo;
@Override public P getPojo() { return this.pojo; }
@Override public void setPojo(P pojo) { this.pojo = pojo; }
}
OK
abstract class AbstractFactory<T> {
T target;
abstract T createTarget(Node n);
T getTarget() { return this.target; }
void processParam(Node n) { }
}
abstract class AbstractDaoFactory<D extends AbstractDAO<P>, P
extends POJO> extends AbstractFactory<D> { }
OK, de a factory miért nincs interface-elve ?
class POJOA extends AbstractPOJO {
@Override public int asd() { return 1; }
public int pojoAValue() { return 1; }
}
class POJOB extends AbstractPOJO {
@Override public int asd() { return 2; }
public int pojoBValue() { return 2; }
}
class POJOC extends POJOB {
@Override public int asd() { return 3; }
public int pojoCValue() { return 3; }
}
OK
class DAOA<P extends POJOA> extends AbstractDAO<P> {
@Override
public void update() {
System.out.println(String.format("in daoa pojoa value is
%d", this.getPojo().pojoAValue())); //$NON-NLS-1$
}
}
class DAOB<P extends POJOB> extends AbstractDAO<P> {
@Override
public void update() {
System.out.println(String.format("in daob pojob value is
%d", this.getPojo().pojoBValue())); //$NON-NLS-1$
}
}
class DAOC<P extends POJOC> extends DAOB<P> {
@Override
public void update() {
System.out.println(String.format("in daoa pojoc value is
%d", this.getPojo().pojoCValue())); //$NON-NLS-1$
}
}
OK
class DAOAFactory extends AbstractDaoFactory<DAOA<POJOA>, POJOA> {
@Override DAOA<POJOA> createTarget(Node n) { return new DAOA<>(); }
}
OK
class DAOBFactory<D extends DAOB<P>, P extends POJOB> extends
AbstractDaoFactory<D, P> {
@Override D createTarget(Node n) { return return new
DAOB<>(); } // Type mismatch: cannot convert from Q.DAOB<Q.POJOB> to D
}
NOT
class DAOBFactory extends AbstractDaoFactory<DAOB<POJOB>, POJOB> {
@Override DAOB<POJOB> createTarget(Node n) { return new DAOB<>(); }
}
OK
-------------------------------------------------------------------------------------------------^
class DAOCFactory extends DAOBFactory<DAOC<POJOC>, POJOC> {
@Override DAOC<POJOC> createTarget(Node n) { return new
DAOC<>(); }
}
NOT
class DAOCFactory extends AbstractDaoFactory<DAOC<POJOC>, POJOC> {
@Override DAOC<POJOC> createTarget(Node n) { return new DAOC<>(); }
}
OK
class Store {
private Map<String, DAO<? extends POJO>> store;
public Store() {
DAOA<POJOA> daoa = new DAOA<>();
this.store.put("", daoa); //$NON-NLS-1$
}
}
}
Az egészet sokkal egyszerűbben is lehet szerintem, holnap majd kereslek.
Attila.
További információk a(z) Javalist levelezőlistáról