Let self-populating objects do work!


So, you're using a graphical user interface (GUI) control to pick from a standard set of values, say, a day of the week. Your code creates a drop down list then populates it kind of like this:

void MyViewClass:: constructGUI()
{
  DropdownControl dropDown;
  dropDown.append("Monday");
  dropDown.append("Tuesday");
  dropDown.append("Wednesday");
  ...
  ControlPanel panel;
  panel.add(dropDown );
}

Now what happens if you need to use an identical drop down list elsewhere?

You could copy and paste, ending up with:

void SomeOtherClass::doSomething()
{
  DropdownControl ctrl;
  ctrl.append("Monday");
  ctrl.append("Tuesday");
  ctrl.append("Wednesday");
  ...

  ControlPanel anotherPanel;
  anotherPanel.add(dropDown );
}

but that would be bad, making maintenance difficult and making it harder to understand the code. On top of bloating the code, you may improperly copy it. If you make changes to it you’ll then have to go find every other matching lines and update them too. Worse, someone later on makes changes to one set of lines but not both (now you’re starting to have a tangled ball of yarn in your system). If you’re lucky, you can recover the reasoning for the odd-ball change...

Well, you could create a static utility module pass in the DropdownControl as an argument then populate it there.

#include "MyUtility.h"

void MyViewClass::constructGUI()
{
  DropdownControl dropDown;
  MyUtility::populateWithWeekdays( dropdown );
  ControlPanel panel;
  panel.add(dropDown );
}

void SomeOtherClass::doSomething()
{
  DropdownControl dropDown;
  MyUtility::populateWithWeekdays( dropdown );
  ControlPanel anotherPanel;
  anotherPanel.add(dropDown );
}

Now you have less code and the desired logic is contained in one re-usable function; but hold on, you still have to include all the other utility code from MyUtility.h just to get at the one function you need. This bloats your executable and makes your code still more complicated than it should be because you’re referencing a module and function to which you’re passing an argument. The utility function arguably has too much knowledge of the object being populated. You’ll find having to create utility functions like this when working in ANSI C.

Do we really care or even want to worry about how the drop down list gets populated? We don’t want to know the detail - just give us the functionality and we’ll be on our way. That’s encapsulation and object oriented programming. You can do this in C++ and Java. Wouldn’t it be nice if we could just write:

#include "WeekdayDropdownControl.h"

void MyViewClass::constructGUI()
{
  WeekdayDropdownControl dropDown;
  ControlPanel panel;
  panel.add(dropDown );
}

void SomeOtherClass::doSomething()
{
  WeekdayDropdownControl dropDown;
  ControlPanel anotherPanel;
  anotherPanel.add(dropDown );
}

You can do this by deriving a class from DropdownControl and only providing a default constructor. The constructor populates the drop down on construction. Finished! The problem is sealed in a jar and literally kept on the shelf for use in another recipe.

#include "DropdownControl.h"

class WeekdayDropdownControl : public DropdownControl
{
  public:
    WeekdayDropdownControl() : DropdownControl()
    {
      append("Monday");
      append("Tuesday");
      append("Wednesday");
      ...
    }
};

Now, you can use your self-populating objects easily wherever and whenever you want, including only the code you need and not an entire utility module. You don’t need to worry about how to populate the values. You can keep building your application knowing your object will exhibit a consistent and predictable behavior everywhere. If you need to make a change to the drop down, you won’t have to fish through your code to tie up all loose ends. You’ll know exactly where to look and the change will be immediate all across the board. If you need to move the drop down somewhere else or get rid of it altogether, it will be extremely easy. Your solutions will be more readable, elegant, less bug-prone, and you’ll save yourself time.

Permalink:
http://coderomp.blogspot.com/2010/03/let-self-populating-objects-do-work.html

Similar Articles:

Post a Comment

What are your thoughts?