user1653400 user1653400 - 3 months ago 27
C# Question

Factory Pattern - with optional parameters

I have a factory:

public static class AdapterFactory
{

public static IAdapter GetAdapter(AdapterType claimType, int mid, int tid, int siteId, string version)
{
}
}


My problem is that sometimes the mid, tid, siteId and version parameters are not needed to construct an "Adapter", they are specific to only some kinds of adapters.

What is the best approach when parameters are not always required like in this situation?

Thanks for your help.

Answer

Specalise and make more factories, for example:

public static class MidAdapterFactory
{
    public static IAdapter GetAdapter(AdapterType claimType, int mid)
    {
    }
}

public static class TidAdapterFactory
{
    public static IAdapter GetAdapter(AdapterType claimType, int tid)
    {
    }
}

Also consider the builder pattern.