mardi 7 novembre 2017

Decorating a static class C#

I've got a design question.

I've got a static class used in some old code that calls a static method to run some operation. If a certain condition is met, I want to call another method right after it.

I wanted to use the decorator pattern but I can't exactly return an instance of the static class if the condition is not met.

This is what's happening now.

var result = StaticClass.DoSomething(some parameters);

What I want is to write to a database right after that DoSomething is called if another variable is true and I didn't want to just pile on to the old code with conditionals so I'd rather delegate that to some other class. This is what I really want to do.

var result = StaticClassFactory(condition).DoSomething(some parameters);

Class1
void DoSomething(parameters) {
StaticClass.DoSomething()
}

Class2
void DoSomething(parameters) {
StaticClass.DoSomething();
DoSomethignElse();
}

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire