views:

47

answers:

2

I currently have a message system which writes to two tables, sent and received, which largely have the same schema.

I wrote a class called Message which populates the user inputted data before instantiating the two child classes which use a common method in Messageto set the rest of the properties and writing each to the database. The only significant difference is one field which is represented in the child class with its corresponding variable. All of the other properties are part of Message.

The thing is, Message has no purpose other than to provide common methods and properties for the objects created and facilitate access to the database class it's a descendant of.

Is this considered bad practice? Is the Message class monolithic or should I push it further and incorporate the child classes for the sake of one field? Would a better approach be to separate the classes entirely and have one for sent and one for received tables?

A: 

So you have class called Message that facilities common functions, and child classes (i.e.)

class SentMessage extends Message {

that facilitates specific tasks for a 'sent message'?

that makes perfect sense. It just depends how much functionality is seperate. If the only difference is that SentMessage and ReceivedMessage are marked 'sent' or 'received' - you'd be better off to have a getMessageType() function in your Message class instead. But if there is more complexity to it, then yes, you're on the right track.

Dan Heberden
+1  A: 

It is not bad design. You basically created an abstract class. Such classes are not intended to be instantiated but contain code that is common in several other classes.

You followed a principle: DRY - Don't repeat yourself.

Of course, if the difference is really really small, you probably make your design easier by putting everything in one class.

Felix Kling
The thing is the way I've written my class is that the `Message` class is instantiated by the user, hence my concern. The differences specific to the schema is `last_update` (datetime) vs `date_read` (datetime) and in the class the table name it's using. All of the other functions for sending messages (input validation, escaping input, writing to the tables etc) are either part of `Message` or are called as part of `Message`. The only difference being the query for displaying the inbox and outbox and even there I'm considering a common class if I can work it out.
@MrXexxed: If the class is instantiate by the user, how does the subclasses come into play?
Felix Kling
@Felix Kling The class is instantiated by (for example) the send message page, which then calls `Message->send_message`, which in turn creates the instance of `In_Msg` and `Out_Msg`. Which is why I'm wondering if I'm making things harder for myself than I need to.
For displaying messages `Message` is never instantiated, either of the other classes are created directly from the page in question which then call the common database class method for building queries etc.