Click here to Skip to main content
15,887,848 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All

I Have A Class And In This Class Have a Method ..

Method Work With 3 Way :
if number=1 Do job1
if number=2 Do job2
if number=3 Do job3

what statement is better :

1-write one method with if

C#
public int sum()
{
  if (number==1)
  do job1
  if (number==2)
  do job2
  if (number==3)
  do job3
}


2-write method with 3 constrauctor

if way 2 Is Better Please Take 1 Example

Thanks A Lot
Posted
Comments
Sergey Alexandrovich Kryukov 26-Jun-12 14:53pm    
Your method with three constructors is not clear...
--SA
[no name] 16-Jul-12 3:41am    
Isn't it? It sonds like he just (re-)invented the strategy pattern.

It sounds like you need some refactoring. However, to answer your specific question, I would have the Sum method contain a switch statement that would call three different private methods like so:

C#
public int Sum()
{
   switch(number)
   {
      case 1:
         Job1();
         break;
      case 2:
         Job2();
         break;
      case 3:
         Job3();
         break;
      default:
         Console.WriteLine("Error here - fix this");
         break;

   }
}

private int Job1()
{
}
private int Job2()
{
}
private int Job3()
{
}


I would not do the three different constructors option becuase of a couple reasons. First, constructors are for setting up your class instance, not for changing how it will work. Second, that will not be extensible for when you have a fourth job type. Third, you can only have a different constructor if it has a different signature (no arguments, one int argument, one bool argument, etc.) Having the same type of signature for three different constructors would not work. Finally, a class should be behave in an expected manner. If you are changing how the class behaves on instantiation based upon a passed-in value, you are going to cause problems in your code.

My overall advice would be to look at the SOLID principles (especially SRP) and see how you might refactor your application to better conform to these. I don't know your overall design, but I'm assuming you should probably have three different public methods. Then each could return the data as expected. However, if your use case defines that you have one method that does three different things this way, the way I outlined above is probably the best way of doing things.
 
Share this answer
 
v2
Comments
Abhinav S 26-Jun-12 9:23am    
Yes. I wanted to mention case as well.
5.
Sergey Alexandrovich Kryukov 26-Jun-12 14:58pm    
Not such a big difference. I developed a radical approach; please see my article referenced in my answer.
--SA
You should not be using constructors to write this kind of code.

Ideally though, you can even look at the factory design pattern / interfaces to achieve this kind of task.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jun-12 15:01pm    
Frankly, it's not quite clear to me how.
I've developed a radical approach to this problem, please see my article referenced in my answer. I think you knew that one.
--SA
Hello

I think you can use a behavioural design pattern for this. For example, State or Strategy
or Command
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 26-Jun-12 15:00pm    
To me, this is not quite clear, frankly.
I've developed a radical approach to this problem, please see my article referenced in my answer.
--SA
Shahin Khorshidnia 27-Jun-12 0:39am    
Ok Sa, but my name is not Frankly ;)
Sergey Alexandrovich Kryukov 27-Jun-12 11:05am    
:-)
Another solution is to use an array of delegates.
C#
private delegate int MyAction();
private static MyAction[] actions;

private int DoJob1() { Console.WriteLine("1"); return 1; }
private int DoJob2() { Console.WriteLine("2"); return 2; }
private int DoJob3() { Console.WriteLine("3"); return 3; }

public MyClass()
    {
    InitializeComponent();
    actions = new MyAction[] { new MyAction(DoJob1),
                               new MyAction(DoJob2),
                               new MyAction(DoJob3)};
    }
public int Sum(int n)
    {
    if (n >= 0 && n < actions.Length)
        {
        return actions[n]();
        }
    throw new ArgumentException("Invalid action number: " + n);
    }
 
Share this answer
 
Comments
Shahin Khorshidnia 26-Jun-12 8:51am    
Nice
OriginalGriff 26-Jun-12 9:13am    
Once you get your head around the idea of delegates, they can make code a lot cleaner and easier to maintain.
And a lot more efficient, too! :laugh:

Of course, there as good laces to use them, and bad places... :D
Shahin Khorshidnia 26-Jun-12 11:57am    
Yes, your solution set me thinking. That's because I said "Nice". And of course it's a good solution.(Voted 5)
Sergey Alexandrovich Kryukov 26-Jun-12 14:59pm    
Reasonable, my 5.
I've developed a radical approach to this problem, please see my article referenced in my answer.
--SA
This is my CodeProject article where I address a number of similar problems on the base of systematic approach:
Dynamic Method Dispatcher[^].

—SA
 
Share this answer
 
Comments
Shahin Khorshidnia 27-Jun-12 0:43am    
+5
Sergey Alexandrovich Kryukov 27-Jun-12 11:04am    
Thank you. Shahin. And thank you for looking at my article.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900