shapefactoryimplementation.java

来自「一个描述工厂设计模式的程序」· Java 代码 · 共 36 行

JAVA
36
字号

public class ShapeFactoryImplementation implements ShapeFactory{
	private static ShapeFactoryImplementation sfi;
	
	private ShapeFactoryImplementation(){
	}
	
	public static ShapeFactoryImplementation getInstance(){
		if (sfi == null){
			sfi = new ShapeFactoryImplementation();
		}
		return sfi;
	}
	
	public Shape makeShape(String shapeName){
		Class c = null;
		Shape shape = null;
		try {
			c = Class.forName(shapeName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			shape = (Shape) c.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return shape;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?