我的项目的第一部分是构建一个超图工厂模式时抽象产品是通用类
这是一个迅速,吸引了UML图
顶点类
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 我怎样才能避免呢?
不,这不会编译 –
对不起疗法是一个错字 – Alepac
好吧,这将编译,但不再遵循抽象工厂模式。 –