一、场景模拟
public interface Human {public void goHome();}
public interface Car {void start();void stop();void turnLeft();void turnRight();}
public class AudiCar implements Car{public void start() {System.out.println(this.getClass().getSimpleName() + ": start !!!");}public void stop() {System.out.println(this.getClass().getSimpleName() + ": stop !!!");}public void turnLeft() {System.out.println(this.getClass().getSimpleName() + ": turnLeft !!!");}public void turnRight() {System.out.println(this.getClass().getSimpleName() + ": turnRight !!!");} }
public class BuickCar implements Car{public void start() {System.out.println(this.getClass().getSimpleName() + ": start !!!");}public void stop() {System.out.println(this.getClass().getSimpleName() + ": stop !!!");}public void turnLeft() {System.out.println(this.getClass().getSimpleName() + ": turnLeft !!!");}public void turnRight() {System.out.println(this.getClass().getSimpleName() + ": turnRight !!!");} }
public abstract class HumenWithCar implements Human{protected final Car car;public HumenWithCar(Car car) {this.car = car;}public abstract void goHome();}
public class Zhang3Human extends HumenWithCar{public Zhang3Human(Car car) {super(car);}public void goHome() {car.start();car.turnLeft();car.stop();} }
public class Li4Human extends HumenWithCar{public Li4Human(Car car) {super(car);}public void goHome() {car.start();car.turnRight();car.stop();} }
二、定义IoC容器
public class IocContrainner {/*** 用来存储beans,String为bean的id,Object为bean本身*/private final Map<String, Object> beans = new HashMap<String, Object>();/*** 根据beanId获取一个bean* @param beanId beanId* @return 返回bean*/public Object getBean(String beanId) {return beans.get(beanId);}/*** 委托IoC容器创建一个bean* @param clazz 要创建的bean的class* @param beanId beanId* @param paramBeanIds 要创建的bean的class的构造方法所依赖的bean*/public void registerBean(Class<?> clazz, String beanId, String... paramBeanIds) {Object[] paramValues = new Object[paramBeanIds.length];for (int i = 0; i < paramBeanIds.length; i++) {Object paramValue = getBean(paramBeanIds[i]);if (paramValue == null) {throw new RuntimeException("Can't find bean of id: " + paramBeanIds[i]);}paramValues[i] = paramValue;}Object bean = null;// 获取并循环所有的构造方法Constructor<?>[] constructors = clazz.getConstructors();for (Constructor<?> constructor : constructors) {try {bean = constructor.newInstance(paramValues);} catch (InstantiationException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}}if (bean == null) {throw new RuntimeException("找不到合适的构造方法:" + paramBeanIds);}beans.put(beanId, bean);} }
三、测试
public class IocContrainnerTest {private final IocContrainner iocContrainner = new IocContrainner();@Beforepublic void before() {iocContrainner.registerBean(AudiCar.class, "audi");iocContrainner.registerBean(PorscheCar.class, "porsche");iocContrainner.registerBean(BuickCar.class, "buick");iocContrainner.registerBean(Zhang3Human.class, "zhang3", "audi");iocContrainner.registerBean(Li4Human.class, "li4", "porsche");}@Testpublic void test() {Human zhang3 = (Human) iocContrainner.getBean("zhang3");zhang3.goHome();Human li4 = (Human) iocContrainner.getBean("li4");li4.goHome();}}