欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第十四章 类型信息

程序员文章站 2022-05-07 10:54:13
...

.class和Class.forName()

        有时候需要获得Class对象的引用,可以通过类名.class或者Class.forName("类名")来获得。两者的区别是,使用.class来创建对Class对象的引用时,不会自动地初始化该Class对象,而Class.forName()会。

RTTI和反射机制

        RTTI:运行时类型信息(Run-Time Type Information)

        RTTI和反射之间的区别在于,对RTTI来说,编译器在编译时打开和检查.class文件,而对反射机制来讲,.class文件在编译时是不可获取的,所以是在运行时打开和检查.class文件。

工厂方法

1. 简单工厂

public abstract class Shape {

	private String name;

	public abstract void draw();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Shape(String name) {
		super();
		this.name = name;
	}

}
public class Circle extends Shape {

	public Circle(String name) {
		super(name);
	}

	@Override
	public void draw() {
		System.out.printf("draw %s.\n", this.getName());
	}
	
}
public class Square extends Shape {

	public Square(String name) {
		super(name);
	}

	@Override
	public void draw() {
		System.out.printf("draw %s.\n", this.getName());
	}

}
public class SimpleFactory {

	public static Shape drawShape(String shapeType) {
		if (shapeType == null || "".equals(shapeType))
			return null;
		if ("circle".equalsIgnoreCase(shapeType)) {
			return new Circle(shapeType);
		} else if ("square".equalsIgnoreCase(shapeType)) {
			return new Square(shapeType);
		}
		return null;
	}

	// 测试方法
	public static void main(String[] args) {
		Shape circle = SimpleFactory.drawShape("circle");
		circle.draw();
		Shape square = SimpleFactory.drawShape("square");
		square.draw();
	}
}

2. 工厂方法

    增加一个ShapeFactory抽象类,CircleFactory和SquareFactory均继承自ShapeFactory,来实现获取Shape的功能,分离出简单工厂中的SimpleFactory的if else判断。

public abstract class ShapeFactory {

	public abstract Shape drawShape(String shapeType);
}

public class CircleFactory extends ShapeFactory {

	@Override
	public Shape drawShape(String shapeType) {
		return new Circle(shapeType);
	}

}

public class SquareFactory extends ShapeFactory {

	@Override
	public Shape drawShape(String shapeType) {
		return new Square(shapeType);
	}

}

public static void main(String[] args) {
	ShapeFactory abstractDraw = new CircleFactory();
	Shape circle = abstractDraw.drawShape("circle");
	circle.draw();
}

3. 抽象工厂

新增加和Shape同一级别的Color,添加一个AbstractFactory来获取Shape和Color,添加一个FactoryProducer来获取ShapeFactory或ColorFactory。

public abstract class Color {

	private String name;

	public abstract void fill();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Color(String name) {
		super();
		this.name = name;
	}

}
public class Blue extends Color {

	public Blue(String name) {
		super(name);
	}

	@Override
	public void fill() {
		System.out.printf("fill %s color.\n", this.getName());
	}

}
public class Red extends Color {

	public Red(String name) {
		super(name);
	}

	@Override
	public void fill() {
		System.out.printf("fill %s color.\n", this.getName());
	}

}
public abstract class AbstractFactory {

	public abstract Color getColor(String color);

	public abstract Shape getShape(String shapeType);

}

public class ColorFactory extends AbstractFactory {

	@Override
	public Color getColor(String color) {
		if (color == null || "".equals(color))
			return null;
		if ("red".equalsIgnoreCase(color)) {
			return new Red(color);
		} else if ("blue".equalsIgnoreCase(color)) {
			return new Blue(color);
		}
		return null;
	}

	@Override
	public Shape getShape(String shapeType) {
		return null;
	}

}

public class ShapeFactory extends AbstractFactory {

	@Override
	public Color getColor(String color) {
		return null;
	}

	@Override
	public Shape getShape(String shapeType) {
		if (shapeType == null || "".equals(shapeType))
			return null;
		if ("circle".equalsIgnoreCase(shapeType)) {
			return new Circle(shapeType);
		} else if ("square".equalsIgnoreCase(shapeType)) {
			return new Square(shapeType);
		}
		return null;
	}

}
public class FactoryProducer {

	public static AbstractFactory getFactory(String choice) {
		if (choice == null || "".equals(choice))
			return null;
		if ("shape".equalsIgnoreCase(choice)) {
			return new ShapeFactory();
		} else if ("color".equalsIgnoreCase(choice)) {
			return new ColorFactory();
		}
		return null;
	}

	public static void main(String[] args) {
		AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");
		Shape circle = shapeFactory.getShape("circle");
		circle.draw();
		AbstractFactory colorFactory = FactoryProducer.getFactory("color");
		Color red = colorFactory.getColor("red");
		red.fill();
	}
}