I currently have a class Foo, i've decided I need a second type of Foo where I want to test out a significantly different implementation. It will no doubt share some functionality with Foo so I'll need to create a Abstract Class parent for both of them.
At the end of the process I want to have:
AbstractFoo: an abstract classFooBoo: functionaly identical to the originalFooFooBar: the new subclass of AbstractFoo I've created.
There are two ways I could go about this:
Copy Paste, then Refactor
- Create a copy of
Foo, and name itFooBarand renameFootoFooBoo - Edit
FooBarwith the new implementation - Inspect,
FooBarandFooBoo, move methods that are the same between them into a abstract classAbstractFoo, which both inherit from
Refactor, then Implement
- Rename
Foo,FooBoo - Consider what methods are not related to the part of the implementation that
FooBooandFooBazare going to be different in. Move these toAbstractFoo - Create
FooBazand reimplement all the methods that remain withinFooBoo
Which is to be preferred? *Is there another method?*
Last time I had to do this, it was because the I decided that Foo had 2 different modes, that was determined by a boolean passed into the constructor.
This time (on a different class), it is because I want to try out using a different engine underneath that might be faster. I want to keep FooBoo around because it has a subclass that I think will be much harder to implement for FooBaz, and I also want to be able to check that in the end they behave the same.