public class Legacy { public void eg1() { stuff(Singleton.getInstance().call()); } public void eg2() { more_stuff(Singleton.getInstance().call()); } }
And you want to create a seam for the singleton access. The first step is to introduce two public fields:
public class Legacy { public int Singleton; public Singleton instance; public void eg1() { stuff(Singleton.getInstance().call()); } public void eg2() { more_stuff(Singleton.getInstance().call()); } }
Now Lean on the Compiler. All the calls to
Singleton.getInstance()
no longer compile because the Singleton class is now hidden by the int Singleton
just introduced.
Next refactor all occurences of Singleton.getInstance()
to instance
(the other identifier just introduced, you can pick any name for this one but it's type must be Singleton).
public class Legacy { public int Singleton; public Singleton instance; public void eg1() { stuff(instance.call()); } public void eg2() { more_stuff(instance.call()); } }
Then Lean on the Compiler again to make sure there's no typos. Finally refactor to this:
public class Legacy { public Singleton instance = Singleton.getInstance(); public void eg1() { stuff(instance.call()); } public void eg2() { more_stuff(instance.call()); } }
The name lookup rules for Java allow this to work but I don't think this idea will work in C++ (for example). I haven't got time to try it now as I'm heading off to the NDC conference in Oslo. I only thought of it yesterday. I haven't tried this on real legacy code. Caveat emptor!
Update. I have a C++ version aswell.
No comments:
Post a Comment