Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A design pattern for constructors

I have been challenged by a design issue which I will try to describe below.

Suppose that a class, call it A, has a constructor with a bunch of parameters. Since it is tiring and dirty to write all those parameters in each instantiation, I have written another class, call it StyleSheetA, which encapsulates all those parameters and is the only parameter to the constructor of A. In this way, I can prepare some default StyleSheetA templates to be used later, and if it is needed, I can modify them.

And at this point, I need to extend A. Suppose B extends A. B will have its own stylesheet, namely StyleSheetB. I think it will be appropriate that StyleSheetB extends StyleSheetA, so with one stylesheet parameter, constructor of B can also construct its super class A. But I am afraid of the possibility that this design may have flaws. For example what if I decide to add a getter/setter for the stylesheet? Is there a novel way to handle all these situations? Am I in the wrong way? For those who are confused, I attach some code here:


    class A
    {
        StyleSheetA ss;

        A(StyleSheetA ss)
        {
            this.ss = ss;
            // Do some stuff with ingredients of styleSheet
        }
    }
    class StyleSheetA
    {
        int n1;
        int n2;
        // :
        // :
        int n100;
    }

    class B extends A
    {
        B(StyleSheetB ss)
        {
            super(ss);
            // Do some stuff with ingredients of styleSheet
        }
    }
    class StyleSheetB extends StyleSheetA
    {
        int n101;
        int n102;
        // :
        // :
        int n200;
    }

Thank you for any help or suggestions, also any of your critics will be appreciated.

Edit: I am developing in java me so there is no generics support.

like image 291
ozan k Avatar asked Feb 09 '11 21:02

ozan k


1 Answers

It seems to me that you are only moving the problem of having too many parameters from class A to class StyleSheetA.

To illustrate my point, think of this question: How would you instantiate StyleSheetA? Probably using a constructor that accepts all these parameters, anyway. The only benefit this design may give you is if you have a same set of parameter values encapsulated by an object of StyleSheetA which you will reuse among multiple instances of A. If so, bear in mind that although you'd have different instances of A they would share the same parameters, so it isn't a good choice.

What I could recommend you is to try to refactor your class A itself. Try to break it up into smaller classes. If nesseccary, try to create subclasses to avoid conditional branches, etc.

Now, I don't know how your class A looks like, but maybe if you do so you'll have several classes, each with its own set of parameters. And if any of the parameters is a discriminator (meaning that it determines the class "type") you will be able to get rid of it, just by using subclasses, and relying on built in type system to do it instead.

like image 193
Goran Jovic Avatar answered Sep 28 '22 22:09

Goran Jovic