I need to be able to conditionally execute a method. I prefer not having a bunch of IF statements throughout my code for several reasons, the most notable is that at a certain point in the future the method will no longer be used.
I am not sure of the best way to do this or what pattern(s) I should choose to accomplish this task.
The application I refer to is an application that is going to replace a legacy system. The legacy code will be turned off and no longer used at some point. Once that point of time comes, I don’t want to have to go back and change any code (if at all possible).
The following is a fictious conceptual example in psuedo of what I mean:
NewSystemEmployee.Save(Employee e)
if (Legacy System Is Running)
{
LegacySystemEmployee.Save(Employee e)
}
The method NewSystemEmployee.Save always needs to execute. I only want to execute LegacySystemEmployee.Save as long as the Legacy system is running. Once the Legacy system is shutdown, I no longer want to execute LegacySystemEmployee.Save
Once the legacy system goes away, I don’t know how I can accomplish what I want without:
- Creating an IF statement before I call
LegacySystemEmployee.SaveOR - Removing every call to
LegacySystemEmployee.Savemethod OR - Changing
LegacySystemEmployee.Savemethod so that it is a stub and nothing more
I also have a requirement that the NewSystemEmployee class does not refer in any way to the LegacySystemEmployee class.
Any suggestions?
Thanks so much
At first glance this calls for the Strategy pattern (or in fact Template Method), possibly with a Factory Method to control which strategy to use. Example (in pseudo-Java):
Since at some point in time you will no more need the legacy call, it makes sense to implement the default
LegacySaveas empty. Then you can trivially use the class like this:where
getSaver()is the factory method which silently decides whichSaverimplementation to present to its caller. Trivially it can get the class name from a config file.Question is, how do you need to change the behaviour: runtime, or by restarting the application? If you can restart the application, it is easy to change that one line in the configuration which controls the strategy to use. Changing behaviour on-the-fly is slightly more tricky, but doable.
A lot depends on what programming language you use. E.g. in Java / C++ / C# you can easily accomplish what you want with dependency injection; in some other languages it might not be that easy.