C++ Logo

std-discussion

Advanced search

Asking for better suggestions

From: Tim O'Neil <interval1066_at_[hidden]>
Date: Mon, 20 Dec 2021 08:07:12 -0800
I recently took on a project requiring me to build a UI in wxWidgets, but I
had to follow the lead after the architect. His methods were,
"interesting". I don't want to disparage the guy but he used an odd mix of
patterns in his design, a lot of old school stuff even though the project
was built to C++17 compliance. For example, he didn't like using smart
pointers opting instead to instantiate everything on the stack, and other
bizarre stuff. Anyway, the ui's header file had a huge pile of pointers to
all its widgets. I find this style of design painful, after a few dozen
pointer instances I found the code rather difficult to follow. I tried to
show the man a bit more manageable design using an abstract wxWidget
factory pattern with descendant factories for each widget we needed to
instantiate, and more composition for various things, and he found it
"interesting" but proceeded to keep doing things his way. No matter.

I've decided to take on a redesign on my own using that abstract factory
pattern.This works rather well and I've maintained a very loose coupling
between the widget declarations in the header and the implementations in
the impl file. The stuff the various factories create don't even need to be
wxwidgets, could be anything, this makes me happy. What doesn't make me
happy is how I'm handling the parameters necessary for each widget.

Loose example with lots of omission; widge1 factory: wxWidget1instance =
new wxWidget1(foo, bar, baz);
widget2instance = new wxwidget2(param1, param2);
 and so on. Of course the various params are of different types.

I'm passing the params using a stuct with a bunch of types necessary for
each different widget and simply leaving the unneeded params null. This
works, but naturally leaves a bad taste in my mouth. For one thing I'm sure
this breaks my carefully crafted loose coupling between instances and
implementation, and whatever else I'm not seeing yet but I know I'll
encounter other issues.

Curious to hear from others how they would do this? Pointers to functions?
Some kind of polymorphic object? Please share your thoughts, and much
appreciated.

Received on 2021-12-20 10:07:23