2013-04-12 34 views
0

我的項目的第一部分是構建一個超圖工廠模式時抽象產品是通用類

這是一個迅速,吸引了UML圖enter image description here

頂點類

public abstract class Vertex <T>{ 

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

Imagevertex

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


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

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

AbstractVertexFactory

public abstract class AbstractVertexFactory { 

    public abstract Vertex createVertex(int id); 

    public Vertex produceVertex(int id) { 

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

ImageFactory類

public class ImageFactory extends AbstractVertexFactory { 

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

模擬器

public class ImageFactorySimulator { 

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


     AbstractVertexFactory imFactory= new ImageFactory(); 

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

    } 

} 

在模擬器中使用鑄鐵的是boared 我怎樣才能避免呢?

回答

2

你可以使用

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

而且

public class ImageFactory extends AbstractVertexFactory<ImageVertex> { 

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

嘗試

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

您可以使用此:

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); 
    } 
} 

public class ImageFactorySimulator { 

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


     AbstractVertexFactory imFactory= new ImageFactory(); 

     ImageVertex im = imFactory.createImageVertex(0); 

    } 
} 
+0

不,這不會編譯 –

+0

對不起療法是一個錯字 – Alepac

+0

好吧,這將編譯,但不再遵循抽象工廠模式。 –

1

另一種回答是:

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); 

    } 

}