Java Reference
In-Depth Information
make the getOpeningTag() method of the class Text return the content of the
element. When the serialize_all() method is invoked with a Text as argument
it would print the content, then will skip the iteration code (since Text is not
composed), finally the getClosingTag() method would return an empty string,
thus not affecting the output at all.
Decision point
We want to define a better serialization procedure that does not violate the
information hiding principle.
The solution provided above for serialization has some defects; in partic-
ular it partly violates the information hiding principle. In fact it accesses the
internal structure of each element in order to serialize its children.
A better solution consists in decentralizing the serialization algorithm and
delegating each element to serialize itself. Since all the objects are (indirect)
instances of Element , and since the algorithm is symmetric, the decentral-
ized algorithm can be defined in Element . Therefore all the objects of the
document graph will execute the same algorithm. An improved version of
the serialize method of Element can be written as follows:
print( this.getOpeningTag() );
if ( this .isComposite() ){
Element child # firstSibling;
while (child! # null ){
child # child.nextSibling();
println( this.getClosingTag() );
The structure of the method is practically the same as the serialize_all()
method. The signature of the method has changed since the previous
argument has become implicit, i.e. it is the object on which the method is
invoked ( this ).
Decision point
How can we capture the common characteristics of the elements?
While two elements ( Text and Head ) are simple elements, the others can
contain other elements. This common feature can be captured introducing a
new class, Composite .
Search WWH ::

Custom Search