"Simple" Factory (padrão de desenho): Difference between revisions
From Wiki**3
| Line 7: | Line 7: | ||
== Utilização de Método Independente da Linguagem == | == Utilização de Método Independente da Linguagem == | ||
<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> | |||
<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> | |||
<java5> | |||
/** | |||
* @param fields | |||
* 0 - type; 1 - id/null; 2 - agent; other - pub-specific | |||
* @throws PublicationExistsException | |||
* we throw it because we may be called from outside (i.e., we | |||
* are not responsible for communicating the problem to the | |||
* end-user) | |||
* @throws UnknownAgentException | |||
* we throw it because we may be called from outside (i.e., we | |||
* are not responsible for communicating the problem to the | |||
* end-user) | |||
* @throws UnknownDataException | |||
* we throw it because we may be called from outside (i.e., we | |||
* are not responsible for communicating the problem to the | |||
* end-user) | |||
*/ | |||
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> | |||
<java5> | |||
/** | |||
* Format: type|id-or-null|name|email|phone | |||
* | |||
* @param fields | |||
* @throws ClientExistsException | |||
* we throw it because we may be called from outside (i.e., we | |||
* are not responsible for communicating the problem to the | |||
* end-user) | |||
* @throws UnknownDataException | |||
* we throw it because we may be called from outside (i.e., we | |||
* are not responsible for communicating the problem to the | |||
* end-user) | |||
*/ | |||
@SuppressWarnings("nls") | |||
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> | |||
<java5> | |||
/** | |||
* This method DOES NOT check message permissions when registering messages | |||
* with agents inboxes/outboxes. It must be private. | |||
* | |||
* @param fields | |||
* @throws UnknownAgentException | |||
* @throws UnknownDataException | |||
* @throws InvalidIdentifierException | |||
*/ | |||
@SuppressWarnings("nls") | |||
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> | |||
=== Connections between agents === | |||
Unrelated exceptions that are detected in this function are simply forwarded. | |||
<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 == | == Utilização de Informação de Tipos em Tempo de Execução == | ||
Revision as of 11:10, 14 November 2013
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
<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>
<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>
<java5>
/**
* @param fields
* 0 - type; 1 - id/null; 2 - agent; other - pub-specific
* @throws PublicationExistsException
* we throw it because we may be called from outside (i.e., we
* are not responsible for communicating the problem to the
* end-user)
* @throws UnknownAgentException
* we throw it because we may be called from outside (i.e., we
* are not responsible for communicating the problem to the
* end-user)
* @throws UnknownDataException
* we throw it because we may be called from outside (i.e., we
* are not responsible for communicating the problem to the
* end-user)
*/
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>
<java5>
/**
* Format: type|id-or-null|name|email|phone
*
* @param fields
* @throws ClientExistsException
* we throw it because we may be called from outside (i.e., we
* are not responsible for communicating the problem to the
* end-user)
* @throws UnknownDataException
* we throw it because we may be called from outside (i.e., we
* are not responsible for communicating the problem to the
* end-user)
*/
@SuppressWarnings("nls")
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>
<java5>
/**
* This method DOES NOT check message permissions when registering messages
* with agents inboxes/outboxes. It must be private.
*
* @param fields
* @throws UnknownAgentException
* @throws UnknownDataException
* @throws InvalidIdentifierException
*/
@SuppressWarnings("nls")
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>
Connections between agents
Unrelated exceptions that are detected in this function are simply forwarded.
<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.