1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power

    Good design for multiple optional parameters

    I want to build a class which takes multiple parameters. To be more precise - It can take four parameters say 'a', 'b', 'c' and 'd' but only one of them is allowed at one time.

    So it is something like:-
    obj = Class(a = 1)
    obj = Class(b=2)
    but not
    obj = Class(a=1, b=2) ... Only one of them is allowed.

    What kind of design show I follow to build such a class? I'm working in python.
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    A Land Down Under
    Rep Power
    You could build one object, and in it's constructor do whatever processing you need to do for the object type that's passed in.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Rep Power
    Can you tell us more about the parameters and how they're being used please? In your example, you have a and b as integers (and presumably c and d are the same). Is it really this simple, or are things more complicated than that?

    Generally, I'd be inclined to create a new type for the parameter. That way, your constructor would have a single parameter (of that new type) and you could restrict the allowed values for the type as necessary.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    Piece of cake. Make your constructor accept a dictionary as an argument. Then check the length of the dictionary argument and abort if there is more than one value in it. Otherwise compare the dictionary value against expected values and set the members accordingly. In python, this would be:
    #!/usr/bin/env python
    class MyClass(object):
        def __init__(self, kwargs = {}):
            if len(kwargs) != 1:
                raise RuntimeError("Incorrect # of arguments in the constructor")
            elif kwargs.has_key('a'):
                self.a = kwargs['a']
            elif kwargs.has_key('b'):
                self.b = kwargs['b']
            elif kwargs.has_key('c'):
                self.c = kwargs['c']
            elif kwargs.has_key('d'):
                self.c = kwargs['d']
                raise RuntimeError("Option not recognized" )
    if __name__ == "__main__":
        foo = MyClass({ "a" : 1 })
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2014
    Rep Power
    You will need to use the builder pattern so that the user can explicitly set the parameter.

IMN logo majestic logo threadwatch logo seochat tools logo