首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在复合模式内部实现的迭代器模式

在复合模式内部实现的迭代器模式
EN

Stack Overflow用户
提问于 2016-10-13 23:52:05
回答 1查看 848关注 0票数 0

我正在为设计模式做一个项目,并且我试图在我的复合基类中实现一个迭代器。但问题是,我从编译器那里得到错误,不知道符号T是什么。我在我的迭代器的接口中使用泛型。

下面是Iterator接口的代码:

代码语言:javascript
复制
interface Iter<T> {
  public void first();
  public void next();
  public boolean isDone();
  public T currentItem();
}

下面是复合基类的代码:

代码语言:javascript
复制
abstract class Composite extends Component {

  public Iter<T> makeIter() {
    return new Iter<T>() {
      private Component component = Composite.this;
      private int _count = 0;

      public void first() {
        // position the iterator to the first element
        _count = 0;
      }

      public void next() {
        // advances the current element
        _count += 1;
      }

      public boolean isDone() {
        // returns true if iterator has cycled all the way through
        return _count >= component.getSize();
      }

      public Component currentItem() {
        // returns current item iterator is positioned on
        return component.getChild(_count);
      }
    };
  }

  //abstract primitive methods to implement
  @Override
  public abstract Component getChild(int number);
  @Override
  protected abstract void doAdd(Component part);
  @Override
  protected abstract void doRemove(Component part);


}

组件代码:

代码语言:javascript
复制
abstract class Component {
  //member variables, in this case a reference to parent
  private Component parent = null;
  protected int instanceID = 0;

  //composite methods
  final public Component add(Component part) {
    try {
      // let composites define how children are managed
      doAdd(part);

      // set this Component as the parent of the added part
      part.parent = this;
    } catch(RuntimeException e) {
      throw e;
    }
    return this;
  }

  final public Component remove(Component part) {
    try {
      // let composites define how children are managed
      doRemove(part);

      //remove this Component as the parent of the added parent
      part.parent = null;
    } catch(RuntimeException e) {
      throw e;
    }
    return this;
  }

  protected Component getParent() {
    return parent;
  }

  // Methods that Composites need to Override
  abstract public Iter<T> makeIter();

  public int getSize() {
    return 1;
  }

  public Component getChild(int number) {
    return null;
  }

  protected void doAdd(Component part) {
    throw new RuntimeException("add() not supported");
  }

  protected void doRemove(Component part) {
    throw new RuntimeException("remove() not supported");
  }

  //toString
  final public String toString() {
    return (parent == null) ?
      instanceID + " is the root." :
      instanceID + " is the child of " + parent;
  }
}

下面是我收到的错误消息:

代码语言:javascript
复制
Component.java:38: error: cannot find symbol
  abstract public Iter<T> makeIter();
                   ^
  symbol:   class T
  location: class Component
Composite.java:5: error: cannot find symbol
  public Iter<T> makeIter() {

我并不是百分之百肯定我是以正确的方式实现这一点,但我知道,对于这个项目,我们需要在复合基类中实现迭代器。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-13 23:59:58

Iter<T>定义了一个泛型类型T,它可以用于抽象情况,但您的Composite类使用的是特定类型Component,需要声明该类型:

代码语言:javascript
复制
public Iter<Component> makeIter() {
  return new Iter<Component>() {
    ...
  }
}

Component类也是如此:

代码语言:javascript
复制
abstract public Iter<Component> makeIter();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40032828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档