Static Class Factory?

G

GroupReader

In my app, I have two very similar static classes. After long
thought, I've decided *yes - keep them static*.

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

- The interfaces (are interfaces allowed on static classes?) are
almost identical.

What's the best way in code to create some sort of "factory" that will
return a reference to Static Class A *OR* Static Class B, depending
on a switch.

(With instance classes, this is fairly straightforward, but I don't
know the best way to do it with static classes)

Are Interfaces allowed? How do you create a "factory" that returns a
reference to a static class, and not an instance of an object?

Thanks for the input.
 
N

Nicholas Paldino [.NET/C# MVP]

Unfortunately, you can't do this. You will have to make your static
classes work on an instance level, and then have a factory which will return
instances of these classes (which implement the same interface or derive
from the same base).

You could make those instances singletons, if you want, which will give
you similar semantics to static methods on a class (but not completely).

Hope this helps.
 
A

Alberto Poblacion

It doesn't make much sense. A "reference" always refers to an instance, so
it doesn't apply to a static class that will have no instances.

You could have your "factory" return a System.Type referring to the
appropriate static class, and then use Reflection on that Type to invoke the
static methods of the class, but this is not going to be very efficient. You
may wish to rethink the adequacy of the classes being static in this
scenario.
 
M

MBR

You could use reflection to access one static class or the other (and I
suppose generate IL to clone them), but ick...
Why not make them instance-friendly?

If you must keep them static (like if you don't own the code), you could
also make an adapter that delgates the instace calls to the static classes,
and use a normal factory pattern to create the adaptors.

Or wait for MetaClass support from C# 4.7... :)

thanks,
m
 
G

GroupReader

Thanks guys, I think all of you were correct...

I ended up getting it to work using a slightly modified version of the
"classic singleton pattern."

I have a static class that returns either a Singleton Instance A or
Singleton Instance B.
 
B

Ben Voigt

MBR said:
You could use reflection to access one static class or the other (and I
suppose generate IL to clone them), but ick...
Why not make them instance-friendly?

If you must keep them static (like if you don't own the code), you could
also make an adapter that delgates the instace calls to the static
classes,

Or create a struct with a bunch of delegate member variables (equivalent of
function pointers) which are a typesafe way to select a static method at
runtime. This is actually very similar to how virtual functions work
anyway, except that virtual function pass an extra "this" pointer that you
don't need or want.
 
G

Guest

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

You may consider a generic static class:

public static class Foo<Unspecified>
{
...
}
- The interfaces (are interfaces allowed on static classes?)

No. A static class can inherit only from System.Object.

public static class Bar : System.Object // this will compile
{
...
}

public interface IFunkyInterface
{
...
}

public static class Foo : IFunkyInterface // this won't compile
{
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top