• Post Reply Bookmark Topic Watch Topic
  • New Topic

DRY principle  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing code, and so far it's looking like this:

MailboxFlags {
BitSet flags;
public boolean getFlag (MailboxFlagType type){
return flags.get (type.id);
}
enum MailboxFlagType {
MailboxClosed(0),
MailboxEmpty(1),
MailboxFull(2)
int id;
}
}

SenderFlags {
BitSet flags;
public boolean getFlag (SenderFlagType type){
return flags.get (type.id);
}
enum SenderFlagType {
AllowedToSend (0),
HasSentBefore (1)
int id;
}
}

There are a few other classes like this, that are basically collections of a class' s flags.But they seem to all share similar logic, apart from the definition of the enum. I've heard of the DRY principle, so how would I follow it in this case?
Thanks.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lawson Smith wrote:There are a few other classes like this, that are basically collections of a class' s flags. But they seem to all share similar logic, apart from the definition of the enum. I've heard of the DRY principle, so how would I follow it in this case?

Well, I don't specifically see that you're breaking it anywhere. Each flag, and each enum, has a meaning that is specific to that class.

However, if you're simply checking for combinations of flags, then you might be better off using an EnumSet rather than a BitSet, because (I think) it's closer to what you want, and you don't have to worry about bit indexes then.

I'd need a bit more detail before I can say definitively though.

Winston
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why can't you make those two classes extend a common abstract superclass with the getFlag method in?
Your code would look a lot better with code tags; I shall try to see if I can't add them to your post

[edit]Couldn't add them because you forgot to indent the code. Sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!