Properties:
- Abstract class defines steps to achieve some task. At some points it defers actions to subclasses.
- Common steps are done within abstract class so code repetition is avoided.
- Subclasses defines subclass specific steps.
Implementation:
- AbstractClass defines template method in which generic algorithm is formed. At subclass specific points abstract mehods are called to defer specific tasks to subclasses.
- ConcreteClass extends abstract class and defines abstract methods to implement tasks in a specific way to it.
Java Standard Library Implementations:
Example Usage:
abstract class AbstractList {
// template method: generic algorithm is defined
public boolean addAll(int paramInt, Collection<? extends E> paramCollection){
boolean bool = false;
for (Iterator localIterator = paramCollection.iterator(); localIterator.hasNext(); ) {
Object localObject = localIterator.next();
add(paramInt++, localObject);//implementation specific section is deferred to subclasses
bool = true;
}
return bool;
}
...
}
class ArrayList {
// subclass specific section is defined in subclass
public boolean add(E paramE)
{
this.elementData[(this.size++)] = paramE;
return true;
}
...
}
Comments
Post a Comment