Java Reference
In-Depth Information
The central class in our design is Element , which represents the generic
element of an HTML page. It offers methods for iteration and serialization:
getOpeningTag() , getClosingTag() and serialize() . This class, together with
Composite forms an instance of the Composite pattern. The attribute info
contains generic information, whose specific semantics are defined in each
concrete derived class. The attribute tag contains the name of the tag, (e.g.
BODY, H1).
The one-to-many association children between Composite and Element is
implemented by decomposing it into two associations: a child one-to-one
association from Composite to Element and a sibling recursive association on
Element (see idiom One-to-many association decomposition in Chapter 4
(p. 84)). The latter is implemented by attribute sibling .
public abstract class Element {
protected String info;
protected String tag;
Element sibling;
Element(String p_info, String p_tag){
info # p_info;
tag # p_tag;
public String getOpeningTag() { return "<" ! tag ! ">"; }
public String getClosingTag() { return ""; }
public void serialize(PrintWriter os){
os.print(" ");
public boolean isComposite() { return false ; }
public boolean hasNext() { return sibling ! # null ; }
public Element nextSibling() { return sibling; }
void addSibling(Element newSibling){
sibling # newSibling;
The other class that forms the core of the composite pattern is Composite ,
which represents the composite HTML element. This class redefines the
isComposite() method to always return true. It also redefines the serialize()
method to iterate over the components. Two new methods are added:
addChild() to add a new component and firstChild() to navigate the first semi-
association resulting from the split of the children association.
public abstract class Composite extends Element {
Element child;
Composite(String info, String tag){
super (info, tag);
Search WWH ::

Custom Search