|
|
(13 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| {{NAVPO}}
| | #REDIRECT [[ist:"Simple" Factory (padrão de desenho)]] |
| <!--{{TOCright}}-->
| |
| O padrão "simple" factory fornece uma forma de criar objectos a partir de uma descrição externa.
| |
| | |
| O processo de criação de objectos a partir de texto é um processo semelhante à compilação e implica interpretar esse texto e construir a semântica.
| |
| | |
| == Utilização de Método Independente da Linguagem ==
| |
| | |
| Função principal de leitura do ficheiro com a informação textual.
| |
| | |
| <java5>
| |
| public void readImportFile(String name) throws IOException {
| |
| BufferedReader reader = new BufferedReader(new FileReader(name));
| |
| String line;
| |
| while ((line = reader.readLine()) != null) {
| |
| String[] fields = line.split("\\|");
| |
| try {
| |
| registerFromFields(fields);
| |
| } catch (UnknownDataException e) {
| |
| System.err.printf("WARNING: unknown data %s\n", e.getMessage());
| |
| e.printStackTrace();
| |
| } catch (PublicationExistsException e) {
| |
| e.printStackTrace();
| |
| } catch (UnknownAgentException e) {
| |
| e.printStackTrace();
| |
| } catch (ClientExistsException e) {
| |
| e.printStackTrace();
| |
| } catch (InvalidIdentifierException e) {
| |
| e.printStackTrace();
| |
| }
| |
| }
| |
| reader.close();
| |
| }
| |
| </java5>
| |
| | |
| === Registo de todos os tipos na rede social ===
| |
| | |
| Excepções não relacionadas com a função actual são simplesmente enviadas para o chamador.
| |
| | |
| <java5>
| |
| public void registerFromFields(String[] fields) throws UnknownDataException,
| |
| PublicationExistsException, UnknownAgentException, ClientExistsException,
| |
| InvalidIdentifierException {
| |
| // Regular expression pattern to match an agent.
| |
| Pattern patAgent = Pattern.compile("^(PERSON|ORGANIZATION)");
| |
| // Regular expression pattern to match a message.
| |
| Pattern patMessage = Pattern.compile("^(MESSAGE)");
| |
| // Regular expression pattern to match a publication.
| |
| Pattern parPublication = Pattern.compile("^(IMAGE|NOTE|URI)");
| |
| // Regular expression pattern to match a publication.
| |
| Pattern parConnection = Pattern.compile("^(CONNECTION)");
| |
| | |
| if (patAgent.matcher(fields[0]).matches()) {
| |
| registerAgent(fields);
| |
| } else if (patMessage.matcher(fields[0]).matches()) {
| |
| registerMessage(fields);
| |
| } else if (parPublication.matcher(fields[0]).matches()) {
| |
| registerPublication(fields);
| |
| } else if (parConnection.matcher(fields[0]).matches()) {
| |
| registerConnection(fields);
| |
| } else {
| |
| throw new UnknownDataException(fields[0]);
| |
| }
| |
| }
| |
| </java5>
| |
| | |
| === Registo de publicações na rede social ===
| |
| | |
| Os campos relevantes ('''fields''') são: 0 - type; 1 - id/null; 2 - agent; other - pub-specific
| |
| | |
| Excepções não relacionadas com a função actual são simplesmente enviadas para o chamador.
| |
| | |
| <java5>
| |
| public void registerPublication(String... fields) throws PublicationExistsException,
| |
| UnknownAgentException, UnknownDataException {
| |
| int pubId = (fields[1] != null) ? Integer.parseInt(fields[1]) : getUUID();
| |
| int agtId = Integer.parseInt(fields[2]);
| |
| if (fields[0].equals("IMAGE")) {
| |
| Image image = new Image(pubId, fields[3], fields[4]);
| |
| Agent owner = fetchAgent(agtId);
| |
| owner.addPublication(pubId, image);
| |
| setLastUUID(pubId);
| |
| changed();
| |
| } else if (fields[0].equals("NOTE")) {
| |
| TextNote note = new TextNote(pubId, fields[3], fields[4]);
| |
| Agent owner = fetchAgent(agtId);
| |
| owner.addPublication(pubId, note);
| |
| setLastUUID(pubId);
| |
| changed();
| |
| } else if (fields[0].equals("URI")) {
| |
| URI uri = new URI(pubId, fields[3], fields[4]);
| |
| Agent owner = fetchAgent(agtId);
| |
| owner.addPublication(pubId, uri);
| |
| setLastUUID(pubId);
| |
| changed();
| |
| } else {
| |
| throw new UnknownDataException(fields[0]);
| |
| }
| |
| }
| |
| </java5>
| |
| | |
| === Registo de agentes na rede social ===
| |
| | |
| Os campos relevantes ('''fields''') são: type|id-or-null|name|email|phone
| |
| | |
| Excepções não relacionadas com a função actual são simplesmente enviadas para o chamador.
| |
| | |
| <java5>
| |
| public void registerAgent(String... fields) throws ClientExistsException, UnknownDataException {
| |
| int id = (fields[1] != null) ? Integer.parseInt(fields[1]) : getUUID();
| |
| if (fields[0].equals("PERSON")) {
| |
| Person person = new Person(id, fields[2], fields[3], fields[4]);
| |
| addAgent(id, person);
| |
| setLastUUID(id);
| |
| changed();
| |
| } else if (fields[0].equals("ORGANIZATION")) {
| |
| Organization organization = new Organization(id, fields[2], fields[3], fields[4]);
| |
| addAgent(id, organization);
| |
| setLastUUID(id);
| |
| changed();
| |
| } else {
| |
| throw new UnknownDataException(fields[0]);
| |
| }
| |
| }
| |
| </java5>
| |
| | |
| | |
| === Registo de agentes na rede social ===
| |
| | |
| This method DOES NOT check message permissions when registering messages with agents inboxes/outboxes.
| |
| | |
| Os campos relevantes ('''fields''') são:
| |
| | |
| Excepções não relacionadas com a função actual são simplesmente enviadas para o chamador.
| |
| | |
| <java5>
| |
| private void registerMessage(String... fields) throws UnknownAgentException,
| |
| UnknownDataException, InvalidIdentifierException {
| |
| if (fields[0].equals("MESSAGE")) {
| |
| int msgId = Integer.parseInt(fields[1]);
| |
| Sender sender = fetchAgent(Integer.parseInt(fields[2]));
| |
| List<Recipient> recipients = new ArrayList<Recipient>();
| |
| for (String recipientId : fields[3].split(",")) {
| |
| Recipient recipient = fetchAgent(Integer.parseInt(recipientId));
| |
| recipients.add(recipient);
| |
| }
| |
| assertValidIdentifier(msgId);
| |
| EmailMessage message = new EmailMessage(msgId, fields[2], sender, fields[3],
| |
| recipients, fields[4], fields[5], "", null);
| |
| sendMessage(message);
| |
| setLastUUID(msgId);
| |
| changed();
| |
| } else {
| |
| throw new UnknownDataException(fields[0]);
| |
| }
| |
| }
| |
| </java5>
| |
| | |
| === Registo de agentes na rede social ===
| |
| | |
| Os campos relevantes ('''fields''') são:
| |
| | |
| Excepções não relacionadas com a função actual são simplesmente enviadas para o chamador.
| |
| | |
| <java5>
| |
| private void registerConnection(String[] fields) throws NumberFormatException,
| |
| UnknownAgentException, UnknownDataException {
| |
| if (fields[0].equals("CONNECTION")) {
| |
| Agent a1 = fetchAgent(Integer.parseInt(fields[1]));
| |
| Agent a2 = fetchAgent(Integer.parseInt(fields[2]));
| |
| boolean a1Accepting = (Integer.parseInt(fields[3]) != 0);
| |
| boolean a2Accepting = (Integer.parseInt(fields[4]) != 0);
| |
| a1.addConnection(new Connection(a1, a2, a1Accepting, a2Accepting));
| |
| a2.addConnection(new Connection(a2, a1, a2Accepting, a1Accepting));
| |
| changed();
| |
| } else {
| |
| throw new UnknownDataException(fields[0]);
| |
| }
| |
| }
| |
| </java5>
| |
| | |
| == Utilização de Informação de Tipos em Tempo de Execução ==
| |
| | |
| Neste caso, apresenta-se a criação dinâmica de objectos de um determinado tipo, a partir de descrições textuais desses objectos.
| |
| | |
| Este exemplo utiliza informação de tipos em tempo de execução. Note-se que este tipo de abordagem apenas é possível em linguagens que providenciem este tipo de informação em tempo de execução, como é o caso do Java.
| |
| | |
| * [[Informação de Tipos em Tempo de Execução (Java)]]
| |
| | |
| [[category:Ensino]]
| |
| [[category:PO]]
| |
| [[category:PO Exemplos|S]]
| |