| Introduction to OOP | Chapter 24: Design Patterns : | next | previous | audio | real | text |
Solution: Hide creation within a method, have the method declare a return type that is more general than its actual return type.
class SortedList {
...
Enumerator elements () { return new SortedListEnumerator(); }
...
private class SortedListEnumerator implements Enumerator {
...
}
}
The method is the ``factory'' in the name. Users don't need to know
the exact type the factory returns, only the declared type.
The factory could even return different types, depending upon circumstances.