Understanding Dependency

948 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
948
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Understanding Dependency

  1. 1. 理解依赖 Daniel Hu
  2. 2. 目录 <ul><li>依赖 </li></ul><ul><ul><li>什么是依赖 </li></ul></ul><ul><ul><li>为什么依赖是不好的 </li></ul></ul><ul><ul><li>依赖的类型 </li></ul></ul><ul><ul><li>依赖的一些特性 </li></ul></ul><ul><li>依赖注入 </li></ul><ul><ul><li>直接生成 </li></ul></ul><ul><ul><li>工厂模式 </li></ul></ul><ul><ul><li>依赖注入 </li></ul></ul><ul><li>Guice </li></ul>
  3. 3. 依赖 <ul><li>什么是依赖 </li></ul><ul><li>为什么依赖是不好的 </li></ul><ul><li>依赖的类型 </li></ul><ul><li>依赖的一些特性 </li></ul>
  4. 4. 什么是依赖 <ul><li>Whenever a class A uses another class or interface B, then A depends on B. A cannot carry out it's work without B, and A cannot be reused without also reusing B. A dependant depends on B. </li></ul>public class Bicycle{     /** 给轮胎充气 */     public void expand(Pump pump){     pump.blow();     } }
  5. 5. Why are Dependencies Bad <ul><li>Decreasing reuse. </li></ul><ul><ul><li>消极的影响 </li></ul></ul><ul><ul><li>开发速度 </li></ul></ul><ul><ul><li>代码质量 </li></ul></ul><ul><ul><li>代码可读性 </li></ul></ul><ul><ul><li>代码的可维护性 </li></ul></ul>
  6. 6. Why are Dependencies Bad <ul><li>public class CalendarReader </li></ul><ul><li>{ </li></ul><ul><li>public List readCalendarEvents(File calendarEventFile){ </li></ul><ul><li> //open InputStream from File and read calendar events. </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public class CalendarReader { </li></ul><ul><li>public List readCalendarEvents(InputStream calendarEventFile){ </li></ul><ul><li>//read calendar events from InputStream </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. Dependency Types <ul><li>Class Dependencies </li></ul><ul><li>public List readCalendarEvents(File calendarEventFile) … </li></ul><ul><li>Interface Dependencies </li></ul><ul><li>public List readCalendarEvents(InputStream calendarEventFile)… </li></ul><ul><li>Method / Field Dependencies </li></ul><ul><li>public byte[] readFileContents(Object fileNameContainer){ </li></ul><ul><li>Method method = fileNameContainer .getClass() .getMethod(&quot;getFileName&quot;, null); String fileName = method.invoke(fileNameContainer, null); </li></ul><ul><li>//open the file and return the contents as a byte array. </li></ul><ul><li>} </li></ul>
  8. 8. Additional Dependency Characteristics <ul><li>Interface Implementation Dependencies </li></ul><ul><ul><li>A class depends on an interface, that class also depends on an implementation </li></ul></ul><ul><ul><li>The larger and more complex an interface becomes, the tighter it is coupled to its default implementation! </li></ul></ul><ul><li>Compile-time and Runtime Dependencies </li></ul><ul><ul><li>Compile-time dependencies tend to be easier for developers to see than runtime dependencies, but sometimes runtime dependencies can be more flexible </li></ul></ul>
  9. 9. Additional Dependency Characteristics <ul><li>Direct and Indirect Dependencies </li></ul><ul><ul><li>A ->B->C </li></ul></ul><ul><ul><li>If you cannot use A without B, and cannot use B without C, then you cannot use A without C either. </li></ul></ul>
  10. 10. Additional Dependency Characteristics <ul><li>Unnecessarily Extensive Dependencies </li></ul><ul><li>public class LoginManager{ </li></ul><ul><li>public User login(HttpServletRequest request){ </li></ul><ul><li>String user = request.getParameter(&quot;user&quot;); </li></ul><ul><li>String password = request.getParameter(&quot;password&quot;); </li></ul><ul><li>//read user and return it. </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><ul><li>The LoginManager cannot be reused (called) without an HttpServletRequest instance </li></ul></ul><ul><ul><li>The LoginManager requires the names of the user name and password parameters to be called &quot;user&quot; and &quot;password&quot;. This is also an unnecessary dependency. </li></ul></ul>
  11. 11. Dependency Injection <ul><li>DI 的优势 </li></ul><ul><ul><li>Direct New </li></ul></ul><ul><ul><li>Factory </li></ul></ul><ul><ul><li>DI </li></ul></ul><ul><li>DI 的方式 </li></ul><ul><ul><li>Constructor </li></ul></ul><ul><ul><li>Exposing setter and getter </li></ul></ul><ul><ul><li>Interface implementation </li></ul></ul>
  12. 12. Factory VS Dependency Injection <ul><li>Factory </li></ul><ul><ul><li>增加了对 Factory 类的依赖 </li></ul></ul><ul><ul><li>…… </li></ul></ul>
  13. 13. Factory VS Dependency Injection <ul><li>Dependency Injection </li></ul><ul><ul><li>A class no longer obtains these instances itself. Instead, a dependency injection container creates the needed instances, and “ injects ” them into the object. – 优点 </li></ul></ul><ul><ul><li>缺点 - 需要一个 DI 的容器,而且需要加以配置。 </li></ul></ul>
  14. 14. DI 的方式 <ul><li>Constructor Injection </li></ul><ul><li>Setter Injection </li></ul><ul><li>Interface Injection </li></ul>
  15. 15. Constructor versus Setter Injection <ul><li>class MovieLister... </li></ul><ul><li>public MovieLister(MovieFinder finder) { </li></ul><ul><li>this.finder = finder; </li></ul><ul><li>} </li></ul><ul><li>…… </li></ul><ul><li>} </li></ul><ul><li>class MovieLister... </li></ul><ul><li>private MovieFinder finder; </li></ul><ul><li>public void setFinder(MovieFinder finder) </li></ul><ul><li> { </li></ul><ul><li>this.finder = finder; </li></ul><ul><li>} </li></ul>
  16. 16. 参考资料 <ul><li>http://tutorials.jenkov.com/ood/understanding-dependencies.html </li></ul><ul><li>http:// martinfowler.com/articles/injection.html </li></ul>

×