Understanding Dependency

  • 700 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
700
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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