user11937
user11937

Reputation:

OOD and responsibility

I'd like to think I've been in this field for quite some time, but sometimes the simple things make you question yourself... deciding what responsibility a class has, SRP, and that type of thing.

So, in the context of a messaging system, is it:

Message m = new Message();
// Some properties set
m.Send();

or

Message m = new Message();
// Some Message properties set...
MessagingSystem ms = new MessagingSystem();
ms.SendMessage(m);

Do you use Controller/Manager type of class? If not, how can a message know how to send itself?

Upvotes: 1

Views: 257

Answers (5)

Teoman Soygul
Teoman Soygul

Reputation: 25742

Just look into the .NET framework itself to see how it is done, each object should only have a single reponsiblity:

        SmtpClient client = new SmtpClient();
        MailAddress from = new MailAddress("[email protected]", "Jane Clayton");
        MailAddress to = new MailAddress("[email protected]");
        MailMessage message = new MailMessage(from, to);
        message.Body = "This is a test e-mail message sent by an application.";
        message.Subject = "Test message";
        client.Send(message);

Upvotes: 1

HappyDeveloper
HappyDeveloper

Reputation: 12805

Both designs are fine, depending on your requirements. It's a matter of usage simplicity vs decoupling

Just take a look at the difference between Active Record and Data Mapper patterns. It's pretty much the same case as your example.

Upvotes: 0

GETah
GETah

Reputation: 21459

Mother nature has the solution. If we look to the real world, a message (email, letter, sms...) does not send itself but needs a device, a controller, a manager or a utility to send it out. So I think I would go for having a controller/manager/utility class who knows how to send a message.

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44605

Probably there is not only black and white out there so any blended approach which might not be optimal and the best for every body, could work in some cases and fail in others.

my starting consideration is that if the message only contains some data and can be sent in different ways (asynchronously, synchronously...) or via different channels (email, pager, wcf, tcp, fax, speech...) then the message class responsibility is only to hold the data and there will be managers like MessagingManager or other classes which will know what to do and how to handle the message, how to send it and how to receive it.

as I said, there is probably no perfection or best for every case solution.

Upvotes: 0

k.m
k.m

Reputation: 31484

I'd say second example is better one. What message has to do with send? Send where? How? This raises too many questions, which should be first alarming thing that it's not the way things should work.

On the other hand, eg. MessagingSystem.SendToCustomerInbox(Message message) states perfectly what it does. Sends message to customer inbox. All question we would've asked ourselves in first case are perfectly clear.

Message itself is more of a data representation, and that would be its single responsibility. Same with message sending service - its responsibility is to just send message, nothing more, nothing less.

Upvotes: 0

Related Questions