|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
In addition to storing its usual data, a class might allow users to "decorate" it with some extra temporary data. For example, we might say that a vertex of a graph is colored red or black, or that it is marked as visited, or that it stores a level number or a back pointer.
A Decorable object can store any number of extra temporary fields. Each is like a temporary instance variable, called an "attribute," that we can add or remove from the object. For example, the object's COLOR attribute could be "red" or "black," or the object might not have a COLOR attribute right now.
To make a new attribute, COLOR, use the constructor for the Attribute class. Now you can do p.put(COLOR,RED) and p.get(COLOR) for a position p. Here RED would be an object of class Color, or perhaps just the string "red" if you are too lazy to make such a class.
The textbook discusses decorations (section 12.3) and gives examples (e.g., code fragment 12.4), but it does not use an Attribute class as we do. See Attribute.java for discussion. Also, the textbook sticks to decorable positions, whereas any object could implement Decorable interface given here.
| Method Summary | |
java.lang.Object |
get(container.Attribute a)
|
boolean |
has(container.Attribute a)
Does this object currently have any value for this attribute? |
java.lang.Object |
put(container.Attribute a,
java.lang.Object o)
Makes this object store o as the value of attribute a. |
java.lang.Object |
remove(container.Attribute a)
Removes attribute a and its associated value from this object. |
| Method Detail |
public java.lang.Object get(container.Attribute a)
public boolean has(container.Attribute a)
public java.lang.Object put(container.Attribute a,
java.lang.Object o)
public java.lang.Object remove(container.Attribute a)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||