lundi 20 février 2017

Best approach to design 8 overloaded methods with combination of 4 parameters

Let's assume I have a following class

public static class ClassA
    {
        public static Type1 A { get; set; }
        public static Type2 B { get; set; }
        public static Type3 C { get; set; }
        public static Type4 D { get; set; }

        public static string Method(Type1 a, Type2 b, Type3 c, Type4 d)
        {
            //do smth
        }

        public static string Method(Type1 a, Type2 b, Type3 c)
        {
            Type4 d = D ?? defaultValue4;
            return Method(a, b, c, d);
        }

        public static string Method(Type1 a, Type2 b, Type4 d)
        {
            Type3 c = C ?? defaultValue3;
            return Method(a, b, c, d);
        }

        public static string Method(Type1 a, Type3 c, Type4 d)
        {
            Type2 b = B ?? defaultValue2;
            return Method(a, b, c, d);
        }


        public static string Method(Type1 a, Type2 b)
        {
            Type3 c = C ?? defaultValue3;
            Type4 d = D ?? defaultValue4;
            return Method(a, b, c, d);
        }

        public static string Method(Type1 a, Type4 d)
        {
            Type2 b = B ?? defaultValue2;
            Type3 c = C ?? defaultValue3;
            return Method(a, b, c, d);
        }

        public static string Method(Type1 a, Type3 c)
        {
            Type2 b = B ?? defaultValue2;
            Type4 d = D ?? defaultValue4;
            return Method(a, b, c, d);
        }

        public static string Method(Type1 a)
        {
            Type2 b = B ?? defaultValue2;
            Type3 c = C ?? defaultValue3;
            Type4 d = D ?? defaultValue4;
            return Method(a, b, c, d);
        }


}

In which I have to work with a combination of 4 parameters where A is always necessary and others are taken in order of priority from parameter, else from property and if no property is set or no parameter passed from its default value. I need to redesign this without dublicate code, so these lines

Type2 b = B ?? defaultValue2;
Type3 c = C ?? defaultValue3; 
Type4 d = D ?? defaultValue4;

a only written once?

Aucun commentaire:

Enregistrer un commentaire