简介
在《java面向对象编程》一书中的一个例子
内容
- 画圆,直线,长方形,三角形的例子
- 未使用配置文件的例子:
/**
* 图形类的抽象类
*/
package com.mmdxf.Dao;
public abstract class Shape {
public abstract void draw();
}
package com.mmdxf.Dao;
/**
* 圆
*/
public class Circle extends Shape {
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("画了个圆");
}
}
package com.mmdxf.Dao;
/**
* 线
*/
public class Line extends Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("画了个线");
}
}
package com.mmdxf.Dao;
/**
* 长方形
*/
public class Rectangle extends Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("画了个长方形");
}
}
package com.mmdxf.Dao;
/**
* 三角形
*/
public class Trangle extends Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("画了个三角形");
}
}
package com.mmdxf.Factory;
import java.io.InputStream;
import java.util.Properties;
import com.mmdxf.Dao.Shape;
/**
* 制造图形的工厂
* @author Administrator
*
*/
public class ShapeFactory {
/**定义整型常量*/
private static final int SHAPE_TYPE_CIRCLE = 1;
private static final int SHAPE_TYPE_LINE = 2;
private static final int SHAPE_TYPE_RECTANGLE = 3;
private static final int SHAPE_TYPE_TRANGLE = 4;
private static Map<Integer, String> shapes = new HashMap<Integer, String>();
/**
* 建立形状和形状类名的对应关系
*/
static {
shapes.put(new Integer(SHAPE_TYPE_CIRCLE), "com.mmdxf.Dao.Circle");
shapes.put(new Integer(SHAPE_TYPE_LINE), "com.mmdxf.Dao.Line");
shapes.put(new Integer(SHAPE_TYPE_RECTANGLE), "com.mmdxf.Dao.Rectangle");
shapes.put(new Integer(SHAPE_TYPE_TRANGLE), "com.mmdxf.Dao.Trangle");
}
/**
* 开放一个接口用来给Panel传递图形类型
* @param type Panel传递图形类型
* @return Shape
*/
public static Shape getShape(int type) {
try {
String className = shapes.get(new Inteter(type))
return (Shape)Class.forName(className).newInstance();
} catch (Exception e) {
return null;
}
}
}
package com.mmdxf.action;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.mmdxf.Dao.Shape;
import com.mmdxf.Factory.ShapeFactory;
/**
* 画图工具
*/
public class Panel {
public void selectShape() throws IOException {
System.out.println("请输入图形的类型:");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int type = Integer.parseInt(bufferedReader.readLine());
Shape shape = ShapeFactory.getShape(type);
if(shape == null ) {
System.out.println("输入的图形类型不存在!");
}else {
shape.draw();
}
}
public static void main(String[] args) throws IOException {
new Panel().selectShape();
}
}
以上是使用一个简单工厂写的画图系统。使用map将图形的code与对应类的系统路径保存,如果需要添加新的图形,需要在ShapeFactory这个类中添加常量,再添加一个图形的类即可。
- 使用配置文件的例子:
配置文件内容
1=com.mmdxf.Dao.Circle
2=com.mmdxf.Dao.Line
3=com.mmdxf.Dao.Rectangle
4=com.mmdxf.Dao.Trangle
package com.mmdxf.Factory;
import java.io.InputStream;
import java.util.Properties;
import com.mmdxf.Dao.Shape;
/**
* 制造图形的读取配置文件工厂
* @author Administrator
*
*/
public class ShapeFactory {
private static Properties shapes = new Properties();
/**
* 建立形状和形状类名的对应关系
*/
static {
try {
InputStream in = ShapeFactory.class.getResourceAsStream("/panel.properties");
shapes.load(in);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 开放一个接口用来给Panel传递图形类型
* @param type Panel传递图形类型
* @return Shape
*/
public static Shape getShape(int type) {
try {
String className = (String) shapes.get(String.valueOf(type));
return (Shape)Class.forName(className).newInstance();
} catch (Exception e) {
return null;
}
}
}
将上第一个例子的ShapeFactory改成上述代码,便可使用读取配置文件的方法来创建图形。相比于第一种方法,这个方法在添加新的图形时不需要改动原有代码,只需要在配置文件添加图形的code和与之对应的类即可。这样可以更加提高系统的可扩展性(ps:第一次知道配置文件原来是这样读出来的。又双叒叕涨姿势了。)