之前对于为什么要用spring感到迷惑,看了《spring in action 中文版》后终于明白。这本书极力推荐。
1.IOS(控制反转)
书中用了一个很好的例子,圆桌骑士和探险任务。大概描述一下:
classBigSoldiers {
String name;
HorseQuest quest;//寻找好马的任务
BigSoldiers (String name){
this.name = name; //构造士兵
quest = new HorseQuest (); //获得探险任务
}
quest.runQuest(); //执行任务
}
class HorseQuest {
HorseQuest (){..}
public void runHorseQuest throws HorseNotFoundException() {
...、//执行任务
}
}
可以发现在BigSoldiers 中因为要用到HorseQuest类,所以BigSoldiers 中充斥了很多的HorseQuest类的代码。
当我们测试BigSoldiers 类的时候也就间接测试了HorseQuest类,
这样做的后果是带来两个类的紧密耦合。并且我们不能测试所有的情况,(如HorseQuest
返回null或抛出HorseNotFoundException的情况)。当HorseQuest有修改的时候,可能也
要修改BigSoldiers 类。
这对oop来说不是好的状态。我们应该给与BigSoldiers 更好的独立性。
面对紧耦合,我们应想办法解耦合。把类隐藏在接口下是解耦合的一种好方法。
对代码作如下修改:
士兵是一个人,先定义士兵的接口
public interface People{
public Object runQuest()
}
我们让BigSoldiers 类继承People
class BigSoldiers implements People{
String name;
Quest quest;//寻找好马的任务
BigSoldiers (String name){
this.name = name; //构造士兵
quest = new HorseQuest (); //获得探险任务
}
public Object runQuest(){
return quest.runQuest(); //执行任务
}
}
到用的时候,我们就直接调用接口,实现类隐藏在接口下,到时修改类的时候
就不会有造成牵一发而动全身。按作者所说的,这样,我们朝正确方向又进了一步。
同样,我们修改任务类
定义任务
public interface Quest {
public ObjectquestOver();
}
class HorseQuest implements Quest{
HorseQuest (){..}
public ObjectquestOver throws HorseNotFoundException() { //任务结束
...、//找马
}
}
这样,我们调用接口就能赋予士兵任务,但不足的是士兵现在只能同样一个任务(在这里是找好马),
也就是一个士兵对应一个任务实例。并且士兵是他自己主动去找任务来执行的。这不符合我们的思维习惯。
士兵应该是被动接受任务,而不是自己决定的。我们应这样再修改
class BigSoldiers implements People{
String name;
Quest quest;//寻找好马的任务
BigSoldiers (String name){
this.name = name; //构造士兵
quest = new HorseQuest (); //获得探险任务
}
public Object runQuest(){
return quest.runQuest(); //执行任务
}
pblic void setQuest(Quest quest){
this.quest = quest;
}
}
注意加粗字体的改变,这样我们将采用方法注入依赖对象。
也就是士兵将是被动地接受任务了。当然,我们还需要在
applicationContext.xml中具体装配起来。
sping的ioc就是基于这样的思想,通过依赖注入的方法,把类与依赖类的关系统一在xml文件
中管理,易于管理修改。而且使各个类的耦合松散,功能清晰。
分享到:
相关推荐
【标题解析】:“2015年春八年级英语下册 Unit 1 Lesson 6 Stories about Spring学案(无答案)(新版)冀教版”指的是一个针对初中二年级春季学期英语课程的学习方案,主题围绕“春天的故事”。这个学案是冀教版...
2015春八年级英语下册 Unit 1 lesson 6 Stories about Spring课件 (新版)冀教版
Prepare for the Pivotal Certified Spring Web Application Developer exam and learn about Spring MVC DispatcherServlet configuration, Spring MVC programming model essentials, Spring MVC views and form ...
About spring-boot-plus框架是spring boot框架的基础上进行高度扩展的一个框架, 高度集成了Spring Session, Spring Data, Spring Cache, Spring WebSocket,Spring Mockito, Spring validation, Redssion,MQ, ...
5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. Getting Started 8. Introducing Spring Boot 9. System Requirements 9.1. Servlet Containers 10. Installing Spring ...
文档概述、获取帮助、First Steps、Upgrading From an Earlier Version、Developing with Spring Boot、Learning About Spring Boot Features、Web、Data、Messaging 和 IO 等。 在文档中,我们可以找到 Spring ...
- **了解Spring Boot特性**(Learning about Spring Boot features):深入探讨了Spring Boot提供的各种功能。 - **迁移至生产环境**(Moving to production):讲述了如何将Spring Boot应用部署到生产环境。 - **...
If you are an experienced Java developer, with prior experience in web technologies, and want to step up in your career and stay up-to-date or learn more about Spring Web scalability, this book is for...
Spring in Action5最新英文版 After nearly 15 years of working ...with something exciting and new to say about Spring when writing the preface for this book. But nothing could be further from the truth!
Spring Boot 学习示例 Spring Boot 2.0 Mysql 5.6 JDK 1.8 Maven license Spring Boot 使用的各种示例,以最简单、最实用为标准,此开源项目中的每个示例都以最小依赖,最简单为标准,帮助初学者快速掌握 Spring ...
标题 "some simple demo about hibernate3.2 and spring2.5" 暗示这是一个关于如何集成和使用 Hibernate 3.2 和 Spring 2.5 的简单演示项目。这两个库在Java开发中扮演着重要角色,Hibernate是流行的对象关系映射...
问题询问的是喜欢春天的什么(What do you like about spring?),而不是对春天的看法(How do you think about spring? 或者 What do you think of spring?)。因此,答案是D,询问的是具体的事物,如花朵和绿树。 ...
About This Book Step into more advanced features of aspect-oriented programming and API components to build enterprise grade systems Build lightning-fast web applications and REST APIs using Spring ...
process by reading about [the lifecycle of an issue][]. Think you've found a bug? Please consider submitting a reproduction project via the [spring-framework-issues][] GitHub repository. The [readme]...
ousel of Progress, I feel the same way about Spring as"Progress Dad felt about the 20th century. Each and every Spring application seems to make the lives of developers so much better. Just ...
a senior solutions architect and Principal Technical instructor with Pivotal, the company behind the Spring Framework, shares his experience, insights and first-hand knowledge about how Spring Boot ...