[推荐学习]什么是IOC,什么是容器,什么是服务组件,它们和普通用户类的区别

AuthenticationService 是类。它是一个服务组件。
微型容器,IOC,DI,都是在讨论服务组件,并不是一般的用户类。
容器中存在的都是服务。
AuthenticationService 是一个有用的服务。它可以被容器自动组装。
一般来说,容器里面有2个类有统一服务接口是没有意义的。
服务职能由一个提供。
比如接口 买票SellTicket,如果有2个买门票的,那有什么意义呢?
应该 一个接口等于一个服务,一个服务只能有一个实现
如AuthenticationService 这种,都是容器中的一个服务组件,属于公用的服务组件,被其他用户的类调用的。
它们的组装有容器来完成,用户类只涉及对他们的使用,不涉及他们的组装。
再次重申:AuthenticationService 等类,接口都是服务组件,都生活在容器里面,他们不是普通的用户代码类。在服务组件中,一个接口代表一个公共服务,接口的实现只有一个就可以了,多个无意义,也不必要(这样的话,例如pico,就可以直接注册,不用写出他们构造函数的参数,也不必要给同一个服务接口写出不同的实现。)

Introduction

This illustrates how to use IoC and why use IoC containers. The container used in this example is PicoContainer, others could be used also. 

Motivation

We want to build an AuthenticationService (AS) to authenticate users. This service should run as a component so it's easily usable and reusable. The AS component needs another component called UserManager (UM) to get users from the user base. We want to make the UM easily exchangeable. The AS should use the UM that's available. To satisfy the dependency we write a SimpleUserManager (SUM). 

AuthenticationService.java 

public class AuthenticationService {  private UserManager manager;

  public AuthenticationService(UserManager manager) {    this.manager = manager;  }  public boolean authenticate(String name, String password) {    if (manager.exists(name)) {      User user = manager.get(name);      return user.getPassword().equals(password);    } else {      return false;    }  }}

UserManger.java 

public interface UserManager {  public boolean exists(String login);  public User get(String login);}

SimpleUserManager.java 

public class SimpleUserManager implements UserManager {  public boolean exists(String login) {    return "stephan".equals(login);  }

  public User get(String login) {    return new User(login,login);  }}

User.java 

public class User {  private String login, password;

  public User(String login, String password) {    this.login = login;    this.password = password;  }

  public String getPassword() {    return password;  }

  public String getLogin() {    return login;  }

}

PicoContainer is only an interface so we have to use an implementation. For simpleness we use StringRegistrationNanoContainer from NanoContainer. NanoContainer is a project that supplies different containers which are PicoContainer compatible. We now can use the AuthenticationService this way: 

 

// Build container    StringRegistrationNanoContainer container =       new StringRegistrationNanoContainerImpl.Default();

    try {      container.registerComponent("AuthenticationService");      container.registerComponent("SimpleUserManager");      container.start();    } catch (Exception e) {      e.printStackTrace();    }

    // We only need to know about Container from here    // and we usually return container from our container    // creation so the container is also exchangeable    // (to an XML container e.g.)    Container c = container;

    AuthenticationService as = (AuthenticationService)        c.getComponent(AuthenticationService.class);    System.out.println("stephan.authenticated = " +       as.authenticate("stephan""stephan"));

    System.out.println("leo.authenticated = " +      as.authenticate("leo""wrong"));  }

The nice thing is that AS get's the UM that's available, SUM in this example. If we would change "SimpleUserManager" to "LDAPUserManager" then AS would get the LDAP manager for getting users. Easily exchangeable. 

Resources

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值