簡體   English   中英

在復合模式內部實現的迭代器模式

[英]Iterator Pattern implemented inside of Composite Pattern

我正在設計模式的項目,並且試圖在我的復合基類中實現一個迭代器。 但是問題是我從編譯器中得到錯誤,但不知道符號T是什么。 我在迭代器的接口中使用泛型。

這是我的Iterator接口代碼:

interface Iter<T> {
  public void first();
  public void next();
  public boolean isDone();
  public T currentItem();
}

這是我的Composite基類的代碼:

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);


}

組件代碼:

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;
  }
}

這是我收到的錯誤消息:

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() {

我不是100%確信我以正確的方式實現了這一點,但是我知道對於項目,我們需要在復合基類中實現迭代器。 任何幫助深表感謝。

Iter<T>定義了泛型T ,該類型適用於抽象情況,但是您的Composite類使用的是特定類型Component ,需要對其進行聲明:

public Iter<Component> makeIter() {
  return new Iter<Component>() {
    ...
  }
}

Component類也是如此:

abstract public Iter<Component> makeIter();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM