java反射(3)--反射的应用

1.写一个简单的工厂模式

package org.sh.factoryDemo01;

interface Fruit{
	public void eat();
}
class Apple implements Fruit {

	@Override
	public void eat() {
		System.out.println("吃苹果");
	}
}
class Orange implements Fruit{

	@Override
	public void eat() {
		System.out.println("吃橘子");
		
	}
}
class Factory{
	public static Fruit getInstance(String className){
		Fruit f = null;
		if(className.equals("apple")){
			f = new Apple();
		}
		if(className.equals("orange")){
			f = new Orange();
		}
		return f;
	}
}

public class FactoryDemo01{
	public static void main(String[] args) {
		Fruit f = Factory.getInstance("apple");
		f.eat();
	}
}

这样的模式正确 但是存在缺点,在工厂操作中因为每次只要以增加子类,则必须修改工厂,那么这个时候可以根据反射机制完成


通过发射机制来实现:

package org.sh.factoryDemo01;

interface Fruit{
	public void eat();
}
class Apple implements Fruit {

	@Override
	public void eat() {
		System.out.println("吃苹果");
	}
}
class Orange implements Fruit{

	@Override
	public void eat() {
		System.out.println("吃橘子");
		
	}
}
class Factory{
	public static Fruit getInstance(String className){
		Fruit f = null;
		try {
			f = (Fruit) Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return f;
	}
}

public class FactoryDemo01{
	public static void main(String[] args) {
		Fruit f = Factory.getInstance("org.sh.factoryDemo01.Apple");
		f.eat();
	}
}

以上实现 工厂可以完全不用改变,但是每次都要输入很长的包.类名称

改进:使用属性配置文件

package org.sh.factoryDemo02.copy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

interface Fruit{
	public void eat();
}
class Apple implements Fruit {

	@Override
	public void eat() {
		System.out.println("吃苹果");
	}
}
class Orange implements Fruit{

	@Override
	public void eat() {
		System.out.println("吃橘子");
		
	}
}
class PropertiesOperate{
	private Properties pro = null;
	private File file = new File("D:"+File.separator+"fruit.properties");
	public PropertiesOperate(){
		pro = new Properties();
		if(file.exists()){
			try {
				pro.loadFromXML(new FileInputStream(file));
			} catch (InvalidPropertiesFormatException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			this.save();
		}
	}
	
	public void save(){
		this.pro.setProperty("apple", "org.sh.factoryDemo02.copy.Apple");
		this.pro.setProperty("orange", "org.sh.factoryDemo02.copy.Orange");
		try {
			this.pro.storeToXML(new FileOutputStream(this.file), "Fruit");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public Properties getProperties(){
		return this.pro;
	}
}

class Factory{
	public static Fruit getInstance(String className){
		Fruit f = null;
		try {
			f = (Fruit) Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return f;
	}
}

public class FactoryDemo01{
	public static void main(String[] args) {
		Properties pro = new PropertiesOperate().getProperties();
		System.out.println("**********"+pro.getProperty("apple"));
		Fruit f = Factory.getInstance(pro.getProperty("apple"));
		f.eat();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值