tags:

views:

176

answers:

3

(Disclaimer: This question is not specific to ASP.NET)

I have a control which may be templated, similar to the login controls:

public abstract class TemplatedControl : CompositeControl
{
    public ITemplate Template { get; set; }

    protected override void CreateChildControls()
    {
        var template = this.Template ?? CreateDefaultTemplate();

        // ...
    }

    protected virtual ITemplate CreateDefaultTemplate()
    {
        return null;
    }
}

A templated control would look like:

public class FooControl : TemplatedControl
{
    public override ITemplate CreateDefaultTemplate()
    {
        return new FooTemplate();
    }
}

My question is: would a Singleton be appropriate here instead?

public override ITemplate CreateDefaultTemplate()
{
    return FooTemplate.Instance;
}

Singletons are associated with global variables; in this case, there is no state.

Singletons are also associated with hard-coded dependencies. In this case, knowledge of the specific type is warranted.

A: 

Do you really want to use the exact same instance for the template control? We may need some more info about what you are trying to accomplish. How many places does TemplatedControl get used in the same application?

Andrew Hare
The idea is that I am encapsulating a particular interface implementation. The object relies on no state, and the same concrete implementation is used in all cases. Is instantiating an object every time overkill if nothing differs from instance to instance?
Bryan Watts
Instantiating an object is never an overkill. It is indeed a blazing fast operation. What might hurt performance is what you will do with the object and not the "new" operator.
Darin Dimitrov
+2  A: 

In this case I would say not. In the pattern you are proposing, there would only ever be one FooTemplate, which would be shared across multiple controls, pages and threads. You would have to be very careful that the template did not contain any request or user specific information, and also synchronize any method calls. It is much easier, and just a bit less performant to instantiate it each time.

The only reason I see doing it that was is that it takes a long time to instantiate the control. In that case, I would go with a factory pattern, where any initialization is done once, but all the data copied into a new instance every time.

Robert Wagner
There is no state in the template. I asked this question specifically because nothing will differ from instance to instance; it is only a class because I have to implement an interface.
Bryan Watts
As long as there are no member variables, I see no harm. You could use a static member variable in your page as well.
Robert Wagner
The template is private to the control which uses it; the template type exposes a static .Instance property.
Bryan Watts
+1  A: 

If you only want the template created once for the control, you could use lazy initialization instead and achieve nearly the same effect.

private ITemplate defaultTemplate;
public override ITemplate CreateDefaultTemplate()
{
     if (defaultTemplate == null)
     {
         defaultTemplate = new FooTemplate();
     }
     return defaultTemplate;
}

You should only use a Singleton implementation if you are sure that you only want one instance of any particular object ever in your application.

tvanfosson
See my comments on other answers. I am fairly certain one instance is desirable in this case.
Bryan Watts
Singleton objects are difficult to test. I would still avoid them unless absolutely required.
tvanfosson
Because it has no constructor parameters or state, it is easily tested. My goal is to determine whether I've identified a legitimate Singleton usage, with full awareness of their drawbacks (none of which seem to apply here).
Bryan Watts