I am really reading a book on design patterns in java and I am newbie :)
http://www.amazon.com/Design-Patterns-Java-TM-Software/dp/0321333020/ in the chapter on the composite template, I came across a code that puzzles me, throws me into an abstract class, I also do not quite understand what happens when an abstract superclass constructor calls the subclass, can you help me!
The listing I'm talking about is in isTree (Set visited)
MachineComponent c = (MachineComponent) i.next(); if (visited.contains(c) || !c.isTree(visited))
How can we call the isTree method of a subclass after it is thrown at its abstract superclass and the isTree superclass isTree is abstract?
Here are fragments of two classes:
package com.oozinoz.machine; import java.util.*; import com.oozinoz.iterator.ComponentIterator; public abstract class MachineComponent { protected abstract boolean isTree(Set s);
2:
package com.oozinoz.machine; import java.util.*; import com.oozinoz.iterator.ComponentIterator; import com.oozinoz.iterator.CompositeIterator; public class MachineComposite extends MachineComponent { protected List components = new ArrayList(); protected boolean isTree(Set visited) { visited.add(this); Iterator i = components.iterator(); while (i.hasNext()) { MachineComponent c = (MachineComponent) i.next(); if (visited.contains(c) || !c.isTree(visited)) return false; } return true; }
source share