设计模式

设计模式-迭代器模式

2020-05-16  本文已影响0人  isLJli

迭代器设计模式的定义

提供一种方法顺序访问一个容器中的各个元素,而又不需要暴露该对象的内部显示。
最常见的就是我们安卓的数据库中的查询数据,我们可以通过next()方法,顺序的访问类中的某个属性值。

迭代器的举例

微信可以通过微信账号和QQ账号登陆,我们在登陆时,会先去微信的用户系统中查找然后去QQ的系统中查找。
UML图

image.png

定义迭代器的接口
一般提供访问下一个元素的方法和判断是否还有下一个元素

public interface Iterator<T> {

  /**
   * 获得下一个
   * @return
   */
  T next();

  /**
   * 是否有下一个
   * @return
   */
  public boolean hasNext();

}

迭代器的真正实现

/**
* 微信具体的迭代器
*/
public class WXIterator implements Iterator<com.haiming.myapplication.simple1.UserInfo> {

  com.haiming.myapplication.simple1.UserInfo[] mUserInfos;

  int index = 0;

  public WXIterator(com.haiming.myapplication.simple1.UserInfo[] userInfos){
      this.mUserInfos = userInfos;
  }

  @Override
  public com.haiming.myapplication.simple1.UserInfo next() {
      return mUserInfos[index++];
  }

  @Override
  public boolean hasNext() {
      return index<mUserInfos.length;
  }
}
public class QQIterator implements Iterator<com.haiming.myapplication.simple1.UserInfo> {
  List<com.haiming.myapplication.simple1.UserInfo> mUserInfos;

  int index = 0;

  public QQIterator(List<com.haiming.myapplication.simple1.UserInfo> userInfos){
      this.mUserInfos = new ArrayList<>();
      this.mUserInfos = userInfos;
  }

  @Override
  public com.haiming.myapplication.simple1.UserInfo next() {
      return mUserInfos.get(index++);
  }

  @Override
  public boolean hasNext() {
      return index<mUserInfos.size();
  }
}

提供返回哪个迭代器的接口

public interface Aggregate<T> {

  Iterator<T>  iterator();
}

用户信息系统,决定返回哪个迭代器

public class QQUserSystem  implements Aggregate<UserInfo> {

  private List<UserInfo> mUserInfos;

  public QQUserSystem(){
      mUserInfos = new ArrayList<>();
      mUserInfos.add(new UserInfo("LJli","1234","1","男"));
      mUserInfos.add(new UserInfo("LJfg","1234","2","男"));
      mUserInfos.add(new UserInfo("LJlfgi","1234","3","男"));
  }

  public List<UserInfo> getUserInfos() {
      return mUserInfos;
  }

  @Override
  public Iterator<UserInfo> iterator() {
      return new QQIterator(mUserInfos);
  }
}
public class WXUserSystem  implements Aggregate<UserInfo> {

  UserInfo[] mUserInfos ;

  public WXUserSystem(){
      mUserInfos = new UserInfo[3];

      mUserInfos[0] = new UserInfo("LJli","1234","1","男");
      mUserInfos[1] =  new UserInfo("LJfg","1234","2","男");
      mUserInfos[2] =  new UserInfo("LJlfgi","1234","3","男");
  }

  public UserInfo[] getUserInfos() {
      return mUserInfos;
  }

  @Override
  public Iterator<UserInfo> iterator() {
      return new WXIterator(mUserInfos);
  }
}

客户端可以通过方法顺序拿到元素

@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      com.haiming.myapplication.simple1.WXUserSystem  wxUserSystem =  new com.haiming.myapplication.simple1.WXUserSystem();

      //根据用户名和密码 查询用户信息
      UserInfo loginUserInfo = queryUserInfo("LJli","1234",wxUserSystem.iterator());

      if (loginUserInfo == null){
          com.haiming.myapplication.simple1.QQUserSystem qqUserSystem = new com.haiming.myapplication.simple1.QQUserSystem();
          loginUserInfo = queryUserInfo("LJli","1234",wxUserSystem.iterator());
      }

      if (loginUserInfo == null){
          //登陆失败
      }
  }

  private com.haiming.myapplication.simple1.UserInfo queryUserInfo(String lJli, String s, Iterator<com.haiming.myapplication.simple1.UserInfo> iterator) {
      while (iterator .hasNext()){
          com.haiming.myapplication.simple1.UserInfo userInfo = iterator.next();

          if (userInfo.getUserNmae().equals(lJli) && userInfo.getUserPwd().equals(s)){
              return userInfo;
          }
  }
      return null;
  }

上一篇下一篇

猜你喜欢

热点阅读