Java Reference
In-Depth Information
12. if (implementor.supportsOrdering()){
13. implementor.addItem(item, position);
14. }
15. }
16.
17. public void remove(String item){
18. implementor.removeItem(item);
19. }
20.
21. public String get(int index){
22. return implementor.getItem(index);
23. }
24.
25. public int count(){
26. return implementor.getNumberOfItems();
27. }
28. }
Note that all the operations are delegated to the implementer variable, which represents the list implementation.
Whenever operations are requested of the
List
, they are actually delegated “across the bridge” to the associated
ListImpl
object.
It's easy to extend the features provided by the
BaseList
—you subclass the
BaseList
and add additional
functionality. The
NumberedList
class demonstrates the power of the Bridge; by overriding the get method, the
class is able to provide numbering of the items on the list.
Example 3.8
NumberedList.java
1. public class NumberedList extends BaseList{
2. public String get(int index){
3. return (index + 1) + ". " + super.get(index);
4. }
5. }