Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Ok, the title might be a little misleading.

I have a Window class that draws widgets inside itself in the constructor. The widgets are all of the same type.

So I pass a list of dictionaries, which contain the parameters for each widget. This works quite nicely, but I am worried that the interface to callers is obfuscated.

That is, in order to use Window, one has to study the class, construct a correct list of dictionaries, and then call the constructor with only one parameter - widgets_params.

Is this good or bad design? What alternatives does the python syntax provide?

share|improve this question

2 Answers 2

The answer partly depends on exactly what your Widget class does. Is the number of child widgets fixed, or can there be a variable number of child widgets based on the input? Also, once created, does the caller need direct access to these child widgets?

For example, let's assume the number of children is dynamic and based on the number of input parameters. You could make the creation of the parent and children as separate steps. Thus, instead of Widget([list of widget parameters...]) you would do:

app = Widget()
app.addChild(label="Name:", color="red")
app.addChild(label="Phone:", color="green")
...

If the child widgets are first class objects, another solution would be to create them first, and pass them in to the parent class. For example:

child1 = ChildWidget(label="Name:", color="red")
child2 = ChildWidget(label="Phone:", color="green")
app = Widget(child1, child2)

Which is better depends a bit on the nature of the child widgets. If the caller needs complete control, I think the second method is better -- the caller has precise control over the children widgets.

If, on the other hand, the caller should not have complete control and should instead only be allowed to set a few attributes, the first solution might make more sense.

share|improve this answer

I don't know what your widget parameters are, but why not a Widget class ? Hence, you can check parameters inside the Widget constructor, and name the parameters:

class Widget:
    def __init__(self, x0, y0, x1, y1): # Given these are the parameters
        ... stuff including possibly some check

w = Window()
w.draw([Widget(x0=12,y0=20,x1=10,y1=20), Widget(x0=...,y0=...)]) 

I stop here, but you see the idea

share|improve this answer
    
It's not clear how your answer addresses the OP's concerns. –  GlenH7 Sep 22 '13 at 21:23
    
@GlenH7 I think he means that rather than giving in a bunch of parameters for the Window to construct the Widgets, the interface would become a lot cleaner if you would pass in a set of ready-made Widgets instead. Then those parameters end up in the Widget constructor, which is a better place for them. You might define different specialized kinds of Widgets with their own parameters, and refactor sets of parameters into grouping objects (x and y could constitute a Point) to clean the constructors even further. –  Thijs van Dien Oct 22 '13 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.