Line 12: | Line 12: | ||
== Classe PageElement == | == Classe PageElement == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* This is an abstract page element. | * This is an abstract page element. | ||
Line 22: | Line 22: | ||
public abstract String render(); | public abstract String render(); | ||
} | } | ||
− | </ | + | </source> |
== Classe Image == | == Classe Image == | ||
Line 28: | Line 28: | ||
Uma representação abstracta de uma imagem. | Uma representação abstracta de uma imagem. | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* An abstract image representation. | * An abstract image representation. | ||
Line 44: | Line 44: | ||
} | } | ||
− | </ | + | </source> |
== Classe TextBlock == | == Classe TextBlock == | ||
Line 50: | Line 50: | ||
Esta classe representa um bloco de texto. | Esta classe representa um bloco de texto. | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* A simple text block. | * A simple text block. | ||
Line 78: | Line 78: | ||
} | } | ||
− | </ | + | </source> |
== Classe Paragraph == | == Classe Paragraph == | ||
Line 84: | Line 84: | ||
Esta classe representa um conjunto de blocos de texto formando um parágrafo. | Esta classe representa um conjunto de blocos de texto formando um parágrafo. | ||
− | < | + | <source lang="java"> |
import java.util.ArrayList; | import java.util.ArrayList; | ||
Line 116: | Line 116: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
== Classe Figure == | == Classe Figure == | ||
Line 122: | Line 122: | ||
Uma figura contém múltiplos elementos gráficos e uma legenda. | Uma figura contém múltiplos elementos gráficos e uma legenda. | ||
− | < | + | <source lang="java"> |
import java.util.ArrayList; | import java.util.ArrayList; | ||
Line 167: | Line 167: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
== Classe Page == | == Classe Page == | ||
Line 173: | Line 173: | ||
Classe que representa uma página. Uma página pode conter qualquer elemento, inclusivamente outras páginas. | Classe que representa uma página. Uma página pode conter qualquer elemento, inclusivamente outras páginas. | ||
− | < | + | <source lang="java"> |
import java.util.ArrayList; | import java.util.ArrayList; | ||
Line 206: | Line 206: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
== Classe App == | == Classe App == | ||
Line 212: | Line 212: | ||
Aplicação exemplo. | Aplicação exemplo. | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Simple application. | * Simple application. | ||
Line 238: | Line 238: | ||
} | } | ||
− | </ | + | </source> |
= Compilação e Execução = | = Compilação e Execução = | ||
Line 249: | Line 249: | ||
Resultado: | Resultado: | ||
− | + | <source lang="xml"> | |
<page> | <page> | ||
<paragraph> | <paragraph> | ||
Line 261: | Line 261: | ||
<image></image> | <image></image> | ||
</page> | </page> | ||
+ | </source> | ||
[[category:PO]] | [[category:PO]] | ||
[[category:Ensino]] | [[category:Ensino]] | ||
[[category:PO Exemplos]] | [[category:PO Exemplos]] |
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.
/**
* This is an abstract page element.
*/
public abstract class PageElement {
/**
* @return "graphical" version of the element.
*/
public abstract String render();
}
Uma representação abstracta de uma imagem.
/**
* An abstract image representation.
*/
public class Image extends PageElement {
/**
* @see PageElement#render()
*/
@Override
@SuppressWarnings("nls")
public String render() {
return "<image></image>";
}
}
Esta classe representa um bloco de texto.
/**
* A simple text block.
*/
public class TextBlock extends PageElement {
/**
* Text in this block.
*/
private String _text;
/**
* @param text
*/
public TextBlock(String text) {
_text = text;
}
/**
* @see PageElement#render()
*/
@Override
@SuppressWarnings("nls")
public String render() {
return "<textblock>" + _text + "</textblock>";
}
}
Esta classe representa um conjunto de blocos de texto formando um parágrafo.
import java.util.ArrayList;
/**
* A paragraph has several text blocks.
*/
public class Paragraph extends PageElement {
/**
* Text blocks in this paragraph.
*/
ArrayList<TextBlock> _textBlocks = new ArrayList<TextBlock>();
/**
* @param textBlock
*/
void addTextBlock(TextBlock textBlock) {
_textBlocks.add(textBlock);
}
/**
* @see PageElement#render()
*/
@Override
@SuppressWarnings("nls")
public String render() {
String text = "<paragraph>\n";
for (TextBlock textBlock : _textBlocks)
text += textBlock.render() + "\n";
text += "</paragraph>";
return text;
}
}
Uma figura contém múltiplos elementos gráficos e uma legenda.
import java.util.ArrayList;
/**
* A figure with images and a caption.
*/
public class Figure extends PageElement {
/**
* 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;
}
}
Classe que representa uma página. Uma página pode conter qualquer elemento, inclusivamente outras páginas.
import java.util.ArrayList;
/**
* A simple text block.
*/
public class Page extends PageElement {
/**
* Elements in this page (note that we admit recursive pages).
*/
private ArrayList<PageElement> _elements = new ArrayList<PageElement>();
/**
* @param element element to be added to the page.
*/
public void addElement(PageElement element) {
_elements.add(element);
}
/**
* @see Element#render()
*/
@Override
@SuppressWarnings("nls")
public String render() {
String text = "<page>\n";
for (PageElement element : _elements)
text += element.render() + "\n";
text += "</page>";
return text;
}
}
Aplicação exemplo.
/**
* 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());
}
}
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>