Sometimes when I use a class (let's call it MyClass) I need to change its behavior locally and make sure the default behavior is restored afterwards.
I'm thinking something along the lines of creating another class (e.g. MyClassBehaviorSwitcher) implementing IDisposable. In the constructor it will set some static property and unset it in the Dispose() method. MyClass will then take the value of the static property into account. Usage example:
using (new MyClassBehaviorSwitcher()) {
// Work with MyClass, which will behave differently
// until the end of the block.
}
This way, I ensure the default behavior is restored afterwards. Even if the client code doesn't use using, the object will get disposed at some point.
My question: is this a pattern? Is there naming convention for such classes? Or maybe I am overlooking something and there is a better way of implementing what I want?