I’m currently thinking about an interface to a class I’m writing. This class contains styles for a character, for example whether the character is bold, italic, underlined, etc. I’ve been debating with myself for two days whether I should use getters/setters or logical names for the methods which change the values to these styles. While I tend to prefer logical names, it does mean writing code that is not as efficient and not as logical. Let me give you an example.
I’ve got a class CharacterStyles
which has member variables bold
, italic
, underline
(and some others, but I’ll leave them out to keep it simple). The easiest way to allow other parts of the program to access these variables, would be to write getter/setter methods, so that you can do styles.setBold(true)
and styles.setItalic(false)
.
But I do not like this. Not only because a lot of people say that getters/setters break encapsulation (is it really that bad?), but mostly because it doesn’t seem logical to me. I expect to style a character through one method, styles.format("bold", true)
or something like that, but not through all these methods.
There is one problem though. Since you can’t access an object member variable by the contents of a string in C++, I would either have to write a big if-statement/switch container for all styles, or I would have to store the styles in an associative array (map).
I can’t figure out what the best way is. One moment I think I should write the getters/setters, and the next moment I lean towards the other way. My question is: what would you do? And why would you do that?
4
Yes, getters/setters do break encapsulation – they basically are just an extra layer between directly accessing the underlying field. You might as well just access it directly.
Now, if you want more complex methods to access the field, that’s valid, but instead of exposing the field, you need to think what methods the class should be offering instead. ie. instead of a Bank class with a Money value that is exposed using a property, you need to think what types of access a Bank object should offer (add money, withdraw, get balance) and implement those instead. A property on the Money variable is only syntactically different from exposing the Money variable directly.
DrDobbs has an article that says more.
For your problem, I’d have 2 methods setStyle and clearStyle (or whatever) that take an enum of the possible styles. A switch statement inside these methods would then apply the relevant values to the appropriate class variables. This way, you can change the internal representation of the styles to something else if you later decide to store them as a string (for use in HTML for example) – something that would require all users of your class to be changed too if you used get/set properties.
You can still switch on strings if you want to take arbitrary values, either have a big if-then statement (if there’s a few of them), or a map of string values to method pointers using std::mem_fun (or std::function) so “bold” would be stored in a map key with its value being a sts::mem_fun to a method that sets the variable bold to true (if the strings are the same as the member variable names, then you can also use the stringifying macro to reduce the amount of code you need to write)
6
One idea you might not have considered is the Decorator Pattern. Rather than setting flags in an object and then applying those flags to whatever you’re writing, you wrap the class that does the writing in Decorators, which in turn apply the styles.
The calling code doesn’t need to know how many of these wrappers you’ve put around your text, you just call a method on the outer object and it calls down the stack.
For a pseudocode example:
class TextWriter : TextDrawingInterface {
public:
void WriteString(string x) {
// write some text somewhere somehow
}
}
class BoldDecorator : TextDrawingInterface {
public:
void WriteString(string x) {
// bold application on
m_textWriter.WriteString(x);
// bold application off
}
ctor (TextDrawingInterface textWriter) {
m_textWriter = textWriter;
}
private:
TextWriter m_TextWriter;
}
And so on, for each decorating style. In its simplest usage, you can then say
TextDrawingInterface GetDecoratedTextWriter() {
return new BoldDecorator(new ItalicDecorator(new TextWriter()));
}
And the code that calls this method doesn’t need to know the detail of what it’s receiving. As long as it’s SOMETHING that can draw text through a WriteString method.
13
I’d lean towards the second solution. It looks more elegant and flexible. Although it’s difficult to imagine other types than bold, italic and underline (overline?), it would be difficult to add new types using member variables.
I used this approach in one of my latest projects. I have a class which can have multiple boolean attributes. The number and names of the attributes may change in time. I store them in a dictionary. If an attribute is not present I assume its value is “false”. I also have to store a list of available attribute names, but that’s another story.
4
I think you are over-thinking the issue.
styles.setBold(true)
and styles.setItalic(false)
are OK