2013-04-12 3 views
0

la première partie de mon projet est de construire un hypergraphemodèle usine lorsque le produit abstrait est classe générique

Ceci est un rapide-a appelé diagramme UML enter image description here

Vertex classe

public abstract class Vertex <T>{ 

    int vertexId ; 
    T vertexValue ; 
    public abstract T computeVertexValue(); 
} 

Imagevertex Classe

public class ImageVertex extends Vertex<Map<String, Instance>>{ 


    public ImageVertex(int id) { 
     this.vertexId=id; 
    } 

    @Override 
    public Map<String, Instance> computeVertexValue(){ 
     return null; 
    } 
} 

RésuméVertexFactory

public abstract class AbstractVertexFactory { 

    public abstract Vertex createVertex(int id); 

    public Vertex produceVertex(int id) { 

     Vertex vertex = createVertex(id); 
     vertex.computeVertexValue(); 
     return vertex; 
    } 
} 

classe imagefactory

public class ImageFactory extends AbstractVertexFactory { 

    @Override 
    public Vertex createVertex(int id) { 
     // TODO Auto-generated method stub 
     return new ImageVertex(id); 
    } 
} 

Simulator

public class ImageFactorySimulator { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 


     AbstractVertexFactory imFactory= new ImageFactory(); 

     ImageVertex im = (ImageVertex) imFactory.createVertex(0); 

    } 

} 

l'utilisation de fonte dans le simulateur est boared Comment puis-je éviter?

Répondre

2

Vous pouvez utiliser

public abstract class AbstractVertexFactory <T extends Vertex> { 
    public abstract T createVertex(int id); 
} 

Et

public class ImageFactory extends AbstractVertexFactory<ImageVertex> { 

    @Override 
    public ImageVertex createVertex(int id) { 
     // TODO Auto-generated method stub 
     return new ImageVertex(id); 
    } 
} 
1

essayer

abstract class AbstractVertexFactory<T extends Vertex> { 
    public abstract Vertex createVertex(int id); 
} 

class ImageFactory extends AbstractVertexFactory<ImageVertex> { 
    @Override 
    public ImageVertex createVertex(int id) { 
     return new ImageVertex(id); 
    } 
} 
0

Vous pouvez utiliser ceci:

public class ImageFactory extends AbstractVertexFactory { 

    @Override 
    public Vertex createVertex(int id) { 
     // TODO Auto-generated method stub 
     return createImageVertex(id); 
    } 

    public ImageVertex createImageVertex(int id) { 
     // TODO Auto-generated method stub 
     return new ImageVertex(id); 
    } 
} 

ET

public class ImageFactorySimulator { 

    /** 
     * @param args 
    */ 
    public static void main(String[] args) { 


     AbstractVertexFactory imFactory= new ImageFactory(); 

     ImageVertex im = imFactory.createImageVertex(0); 

    } 
} 
+0

Non, ce ne compilera pas ce compilera –

+0

Désolé ther était une faute de frappe – Alepac

+0

Ok, mais ne suit plus le modèle de fabrique abstraite. –

1

Une réponse alternative est:

public abstract class Vertex <T>{ 

    int vertexId ; 
    T vertexValue ; 
    public abstract T computeVertexValue(); 
} 
public class ImageVertex extends Vertex<Map<String, Object>>{ 


    public ImageVertex(int id) { 
     this.vertexId=id; 
    } 

    @Override 
    public Map<String, Object> computeVertexValue(){ 
     return null; 
    } 
} 
public abstract class AbstractVertexFactory<T extends Vertex> { 

    public abstract T createVertex(int id); 

    public T produceVertex(int id) { 

     T vertex = createVertex(id); 
     vertex.computeVertexValue(); 
     return vertex; 
    } 
} 

public class ImageFactory extends AbstractVertexFactory<ImageVertex> { 

    @Override 
    public ImageVertex createVertex(int id) { 
     // TODO Auto-generated method stub 
     return new ImageVertex(id); 
    } 
} 

public class ImageFactorySimulator { 

    /** 
    * @param args 
    */ 
    public void ttt(String[] args) { 


     ImageFactory imFactory= new ImageFactory(); 

     ImageVertex im = imFactory.createVertex(0); 

    } 

} 
Questions connexes