Я работаю над проектом для Design Patterns, и я пытаюсь реализовать итератор в моем составном базовом классе. Проблема в том, что я получаю ошибки от компилятора, не зная, что такое символ T. Я использую generics в интерфейсе для моего итератора.Iterator Pattern, реализованный внутри Composite Pattern
Вот мой код для интерфейса итератора:
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() {
Я не уверен, что реализую это правильно, но знаю, что для проекта нам нужно реализовать итератор в составном базовом классе. Буду признателен за любую оказанную помощь.
Что такое «Компонент»? – Andrew
Я добавил код для компонента – Scott
Возможный дубликат [Реализация составного шаблона с шаблоном итератора] (http://stackoverflow.com/questions/39991800/implementing-composite-pattern-with-an-iterator-pattern) – ChiefTwoPencils