3

I have a class that has some methods, which I use in code. Their aim is to generate an object and return it for further usage. I can implement it in two ways. The first way - is to make it static, like this:

public static class Builder
{
    public static MyObject BuildMyObject(Settings myEnumSetting, int someParam)
    {
        //Building object 
        return MyObject;
    }

    //Other methods          
}

Other way - is to make it instance like this:

public class Builder
{
    public MyObject BuildMyObject(Settings myEnumSetting, int someParam)
    {
        //Building object 
        return MyObject;
    }

    //Other methods          
}

In the first way can create my objects like this:

MyObject obj = Builder.BuildMyObject(Settings.Worker,20);

In the second case I can use it like this:

MyObject obj = new Builder().BuildMyObject(Settings.Worker,20);

Which of these approaches it more efficient for usage?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex
  • 8,827
  • 3
  • 42
  • 58
  • In general, I would advise to use a static method. It's a special case of a known pattern, see http://en.wikipedia.org/wiki/Factory_method_pattern – Lorenzo Dematté May 24 '13 at 07:23
  • Possible duplicate of http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp?rq=1 – Izzy May 24 '13 at 07:38

5 Answers5

5

Which of these approaches it more efficient for usage?

It depends on your requirement, if the sole responsibility of your class is to create an object and return it then the first option with static class is better.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks for the answer. When should I use the instance approach than? – Alex May 24 '13 at 07:23
  • @voo, When you are actually going to do something with the instance, for example maintaining instance members for each object etc. In your current example, your class instance *(in instance approach)* is not used anywhere. You are not even keeping the reference, so no point in having it in the first place. – Habib May 24 '13 at 07:25
  • Unless it is dependency injected – Fendy May 24 '13 at 07:28
  • @voo, see also: `Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.` from [Static Classes and Static Class Members (C# Programming Guide) - MSDN](http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx) – Habib May 24 '13 at 07:29
  • 1
    @voo you'd use the instance based approach in cases where you'd like to be able to configure the builder. E.g. if you need to be able to configure it either towards production of test. – Rune FS May 24 '13 at 09:16
  • @RuneFS, thats a valid point, but in the example, it didn't appear as the objects to be created using some kind of configuration maintained in instance member. That is why I said `static` is better option (in the two above codes). – Habib May 24 '13 at 09:29
4

Using a static method is more simple. An instance method opens up for an inheritance hierarchy of builders, where you use some kind of dependency inject to select what builder to use.

If the static method works - use it for now. You can always convert the static method to a facade that hides the details of an hierarchy of inherited builder classes later.

By the way: I'd use the name Factory instead of Builder as this is an example of a factory method pattern implementation. Builder is another (more complex) pattern.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
4

The concept you describe is called Factory Pattern. Typically a builder does not use any instance members therefore it should be static.

Instances should be used when there is a cohesion of members (http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29) e.g methods operating on the same set of instance variables (fields) belong together to an instance.

Samuel
  • 6,126
  • 35
  • 70
1

If this class just returns a new object then i'll think that the static way is the better one, because you are not storeing data in that class or something else.

So the static way is more efficient than the other. (I'll don't think that it would be useful to create 10 objects of this class for example)

Your class is a factory class / pattern

shadow
  • 131
  • 3
0

the advantage of a static (factory) class is, that you can keep track of the objects being created in some static property. Depending on your requirements, that might be very useful to have.

Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55