Uma página é constituída por vários elementos, que podem, ou não, ter eles próprios estrutura: imagens (são elementos atómicos que representam entidades gráficas); blocos de texto (elementos atómicos que apenas podem conter texto); figuras (contêm uma ou mais imagens e podem ter uma legenda - um bloco de texto); parágrafos (podem vários blocos de texto). A página pode conter um ou mais elementos dos apresentados anteriormente. Todos os elementos implementam o método render, que permite apresentar a página. Escreva um método main que crie uma página e lhe adicione múltiplos elementos, por forma a permitir exemplificar a funcionalidade.
A solução contempla uma classe abstracta, que representa os elementos em geral, e uma classe concreta para cada conceito.
Na implementação que se apresenta abaixo, as páginas podem conter outras páginas.
<java5> /**
* This is an abstract page element. */
public abstract class PageElement { /** * @return "graphical" version of the element. */ public abstract String render(); } </java5>
Uma representação abstracta de uma imagem.
<java5> /**
* An abstract image representation. */
public class Image extends Element {
/** * @see Element#render() */ @Override @SuppressWarnings("nls") public String render() { return "<image></image>"; }
} </java5>
Esta classe representa um bloco de texto.
<java5> /**
* A simple text block. */
public class TextBlock extends Element {
/** * Text in this block. */ private String _text;
/** * @param text */ public TextBlock(String text) { _text = text; }
/** * @see Element#render() */ @Override @SuppressWarnings("nls") public String render() { return "<textblock>" + _text + "</textblock>"; }
} </java5>
Esta classe representa um conjunto de blocos de texto formando um parágrafo.
<java5> import java.util.ArrayList;
/**
* A paragraph has several text blocks. */
public class Paragraph extends Element { /** * Text blocks in this paragraph. */ ArrayList<TextBlock> _textBlocks = new ArrayList<TextBlock>();
/** * @param textBlock */ void addTextBlock(TextBlock textBlock) { _textBlocks.add(textBlock); }
/** * @see Element#render() */ @Override @SuppressWarnings("nls") public String render() { String text = "<paragraph>\n"; for (TextBlock textBlock : _textBlocks) text += textBlock.render() + "\n"; text += "</paragraph>"; return text; } } </java5>
Uma figura contém múltiplos elementos gráficos e uma legenda.
<java5> import java.util.ArrayList;
/**
* A figure with images and a caption. */
public class Figure extends Element { /** * Images in this figure. */ ArrayList<Image> _images = new ArrayList<Image>();
/** * Legend for this figure. */ TextBlock _caption = new TextBlock(""); //$NON-NLS-1$
/** * @param image */ void addImage(Image image) { _images.add(image); }
/** * @param legend */ void setLegend(TextBlock legend) { _caption = legend; }
/** * @see Element#render() */ @Override @SuppressWarnings("nls") public String render() { String text = "<figure>\n"; for (Image image : _images) text += image.render() + "\n"; text += _caption.render() + "\n"; text += "</figure>"; return text; } } </java5>
Classe que representa uma página. Uma página pode conter qualquer elemento, inclusivamente outras páginas.
<java5> import java.util.ArrayList;
/**
* A simple text block. */
public class Page extends Element {
/** * Elements in this page (note that we admit recursive pages). */ private ArrayList<Element> _elements = new ArrayList<Element>();
/** * @param element element to be added to the page. */ public void addElement(Element element) { _elements.add(element); }
/** * @see Element#render() */ @Override @SuppressWarnings("nls") public String render() { String text = "<page>\n"; for (Element element : _elements) text += element.render() + "\n"; text += "</page>"; return text; } } </java5>
Aplicação exemplo.
<java5> /**
* Simple application. */
public class App {
/** * @param args */ public static void main(String[] args) { Page page = new Page(); Paragraph p1 = new Paragraph(); p1.addTextBlock(new TextBlock("Início.")); p1.addTextBlock(new TextBlock("Segundo bloco com palavras,")); Image i1 = new Image(); Image i2 = new Image(); Figure figure = new Figure(); figure.addImage(i1); figure.setLegend(new TextBlock("Imagem na figura.")); page.addElement(p1); page.addElement(figure); page.addElement(i2); System.out.println(page.render()); }
} </java5>
java App
Resultado:
<page> <paragraph> <textblock>Início.</textblock> <textblock>Segundo bloco com palavras,</textblock> </paragraph> <figure> <image></image> <textblock>Imagem na figura.</textblock> </figure> <image></image> </page>