c# - Is it OK to subclass solely to provide default state/configuration? -


say have gui framework supports nesting gui components inside 1 another. let's call base class gui component can contain other gui components container.

is ok subclass container soley provide default configuration, or should subclassing provide addition/override/implemented behavior? example, wanted make buttonbar container filled 100% of screen width, 50 pixel in height, , laid out components horizontally. configure container that, either of following 2 examples:

container container = new container(); container.percentwidth = 100; container.height = 50; container.layout = layout.horizontal;  // use container 

or, (and question), ok this:

public class buttonbar : container {     public buttonbar()     {         percentwidth = 100;         height = 50;         layout = layout.horizontal;     } }  buttonbar buttonbar = new buttonbar(); // use buttonbar 

buttonbar has no additional functionality on container, , overrides no container methods. serves ease configuring container buttonbar.

edit

i have concluded best use factory returns container, such widgetfactory.createbuttonbar(); way, end using abstract type (container), , encapsulate 'set-up' of type in factory, factories do.

public class widgetfactory {     public container createbuttonbar()     {         container container = new container();         container.percentwidth = 100;         container.height = 50;         container.layout = layout.horizontal;             return container;     } } 

your approach correct if created subclass override method. 1 of main reasons of oop simple modular design of programs. when think philosophical stand point button bar different kind of object container difference may seem trivial in small program in larger 1 make difference of program being comprehensible.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -