• Spring——IoC


        控制反转(Inversion ofControl,英文缩写为IoC)是一种能够解耦的方法,不是什么技术。是一种思想,也是轻量级的Spring框架的核心。控制反转一般分为两种类型。依赖注入(DependencyInjection。简称DI)和依赖查找。

    控制反转是,关于一个对象怎样获取他所依赖的对象的引用。这个责任的反转。

     

    我们通过一个样例体会IoC的优点:

    数据模型例如以下:

    Human接口:

    	package sping.tgb.ioc;
    	
    	public interface Human {
    	
    		public void eat();
    		public void sleep();
    		
    	}
    	
    

    Student类:


    	package sping.tgb.ioc;
    	
    	public class Student implements Human {
    	
    	
    		public void eat() {
    			System.out.println("学生吃饭");
    			
    		}
    	
    	
    		public void sleep() {
    			System.out.println("学生睡觉");
    			
    		}
    	
    	}
    

    Teacher类:


    	package sping.tgb.ioc;
    	
    	public class Teacher implements Human {
    	
    	
    		public void eat() {
    			System.out.println("老师吃饭");
    			
    		}
    	
    	
    		public void sleep() {
    			System.out.println("老师睡觉");
    			
    		}
    	
    	}
    

     

    用工厂模式:

       

       我们假设要在client调用这两个类,不用控制反转的话,能够用工厂实现,例如以下:

    工厂类:

    	package sping.tgb.ioc;
    	
    	public class Factory {
    	
    		public final static String TEACHER = "teacher";
    		public final static String STUDENT = "student";
    	
    		public Human getHuman(String ethnic) {
    			if (ethnic.equals(TEACHER))
    				return new Teacher();
    			else if (ethnic.equals(STUDENT))
    				return new Student();
    			else
    				throw new IllegalArgumentException("參数(职业)错误");
    		}
    	}
    
    client:

    	public static void main(String[] args) {
    		
    			Human human =null;
    			
    			human=new Factory().getHuman(Factory.STUDENT);
    			human.eat();
    			human.sleep();
    			
    			human=new Factory().getHuman(Factory.TEACHER);
    			human.eat();
    			human.sleep();
    	
    		}
    

    输出结果:



     Spring

           用工厂我们做到了解耦和。client不用知道详细的调用类,由工厂推断。以下我们看一下用控制反转我们怎样做:

           使用spring。我们就不用写工厂类,直接写配置文件,例如以下:

    client:

    	public static void main(String[] args) {
    		
    			BeanFactory beanFactory=null;
    			beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");
    			
    			Human human = null;
    			human = (Human) beanFactory.getBean("student");
    			human.eat();
    			human.sleep();
    			human = (Human) beanFactory.getBean("teacher");
    			human.eat();
    			human.sleep();
    	
    		}
    

    输出结果:


    对照:

           大家能够看到用IoC和用工厂的作用基本是一致的,那为什么还要用IoC呢?优势在哪里?

     

          Spring做到了解耦,上一层不依赖下一层的详细类,相同。工厂也做到了。

    可是用Spring当需求发生变化时仅仅要改动配置文件,不用又一次编译系统;而用工厂当需求发生变化须要添加或删除、改动类时,须要又一次编译。这样能够说Spring做到了热插拔。

     

    总结:

     

           IoC的灵活性是有代价的:设置步骤麻烦、生成对象的方式不直观、反射比正常生成对象在效率上慢一点。因此并非不论什么地方都适合用IoC的。用到工厂模式的地方都能够考虑用IoC模式。


  • 相关阅读:
    android ART hook
    Bind Enum to ListControl
    注意WPF中绑定使用的是引用类型
    Android开发第2篇
    Android开发第1篇
    Extension method for type
    DB2实用命令记录
    TDD三大定律
    【InstallShield】 为什么卸载后有的文件没有删掉
    GAC write failed when upgrade with InstallShield
  • 原文地址:https://www.cnblogs.com/llguanli/p/6907373.html
Copyright © 2020-2023  润新知