|
-
August 1st, 2009, 01:14 AM
#1
Polymorphism and Collections
Hi everybody, I'm quite sure this is a common question about OO design, but I'm really, really in hurry.. I hope you'll forgive me.
Let's say that I have an abstract class Message, with title and description:
Code:
public abstract class Message{
private String title;
private String body;
public Messaggio(String title, String body) {
super();
this.title = title;
this.body = body;
}
public String getTitle() {
return title;
}
public String getBody() {
return body;
}
public abstract String getPreview() ;
}
Let's say that I have also different kinds of Message with more attributes, for example:
Code:
public abstract class SignedMessage extends Message{
private Person author;
public Messaggio(String title, String body, Person author) {
super(title, body);
this.author = author
}
public String getAuthor() {
return author;
}
public String getPreview() {
return "A preview";
}
}
I'm writing an application which is meant to dispatch these Messages.
For each user to which i'm sending the messages, I have to provide a buffer wich *must* be unique (for reasons of timestamping and concurrent dispatching), and I mean I cannot have different kinds of Buffer for each user:
Buffer<SignedMessage>, Buffer<MessageWithDraw>....
but I *have* to have only one Buffer<Message> per user.
Now, my problem is: every message has to be treated differently by the receiver (it's a client-server system using RMI), but how can the sender or the receiver recognize the specific kind of message when it sends or receive it?
If I initialize Buffer<Message> aBuffer = new Buffer<Message>() and add a SignedMessage, then upcasting occurs: what if I want to call remote method onReceiveSignedMessage(SignedMessage) from the dispatcher, OR call onReceiveMessage(Message) and recognized SignedMessage inside the receiver?
Do I need to downcast? How?
Is there a better OO workaround?
Thank you sooo much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|