views:

59

answers:

1

I need to create a set of static classes and all of them need to implement the same methods. I want to find a way to force them so.

I understand that static classes cannot derive anything other than System.Object. Should I use non-static methods for this? It could be, but none of the methods of this class will use instance properties...

My best shot is that I should use a singleton. I'll use an instance then, but at least I am not forced to instantiate the class every time I need to use a method.

What is the alternative that you suggest?

EDIT:

I will not implement those methods. I need to force other developers to implement specific methods with specific signatures. All signatures are the same for every class. That's it.

Imagine you have a static class that gets the records from a database in your office. You have several methods to do it. Since none of these methods share variables they are marked as static and so is the class.

Now, you and other developers in your team have to do the same for other databases or even APIs outside your control, it doesn't matter. You want to force your colleagues to implement all of those classes with the exact same methods with the same signatures.

Your comments are always welcome, but now I just want to find the closest way to do this.

I could use an abstract class from which all others would inherit. But since these methods don't share anything, I would prefer not to instantiate the class every time I need them.

That's why I mentioned using the singleton pattern. Shouldn't I do it?

A: 

If I'm reading your question correctly, the singleton wouldn't work unless you had a whole lot of overloads on your methods.

While we talk about Interfaces, and Base Classes, and such, if you're in control of the code, is there a reason you can't just make a set of classes which all implement the required methods? You'd need to document what you did and why, of course, but other than sticking with "convention" do you have a reason for not doing that?

If you're using Static Classes, which don't inherit anything beyond System.Object, and implement no interfaces, you can't call them interchangably anyway.

EDIT Based on your edit, it looks like you're trying to enforce what should be a coding policy through code itself. Every development shop has its own Policies, Procedures, and Standards they follow, everything from documentation, to project layout, to method signatures. My current client is very strict about method signatures because we pass a lot of things to and from BizTalk and its a pain to change a BizTalk solution just because someone didn't want to follow the pattern the rest of the team is using.

If it is members on your team who are writing these static classes and static methods, I don't see a reason (beyond possible office politics) why you couldn't do this by implementing a policy.

If you must do this through code, I think you're stuck with an interface and instance methods.

If you wanted, once those instance methods were created, you could write static helper classes as wrappers (so you, personally, don't have to instanciate the objects every time), but I don't know that that would get you anything.

AllenG
@AllenG, the reason why I want to do this is in the fact that I will not implement all of the classes in question.I wouldn't need overloads for anything, because the methods accept a variable that is an instance of a class which properties are the options allowed.What do you mean with your last paragraph? I can't call the methods in the static classes interchangably? Call 1, 2, then 3 and then 2? I searched google translator for that word, but not sure about what you mean. Read the edit section of the question. Thanks!
Fabio Milheiro
@Fabio Milherio: If I have `static class Foo` with method `DoStuff()` and `static class Bar` with method `DoStuff` I still have to specify which class I need: `Foo.DoStuff()` or `Bar.DoStuff()`
AllenG