w0051977
w0051977

Reputation: 15807

Organising refactored code

Please have a look at the code below:

public class Vehicle
  'Not very well designed.  Contains properties and functions/subs for cars, buses, tractors, planes, drivers etc.
end class

I am wanting to refactor the code so that there is a superclass (vehicle) and many subclasses. I want to do this as I go along whilst working on much higher priorities.

If I create the new classes then there will be two Vehicle classes i.e. the refactored vehicle and the old vehicle. I believe the best approach is to create a new namespace for the refactored code e.g. company.app.businesslogiclayer.automobiles.refactoredcode, company.app.datalogiclayer.automobiles.refactoredcode. Is this the correct approach?

Upvotes: 1

Views: 95

Answers (2)

Jeroen De Dauw
Jeroen De Dauw

Reputation: 10908

Be careful to not overuse inheritance. "Driver" strikes me as something that you really want to use composition for. A vehicle has a driver. Similarly other things such as the might be better handled using composition. For instance you could have a car that can go 200km/h, and have one that can go 300km/h. Really do not want to have different classes for that. You could have a simple int value or a EngineBehaviour class if you have something more complex. (Keyword: strategy pattern) Also be sure to not instantiate such composite objects in your object but rather inject them (keyword: dependency injection).

Upvotes: 1

Robert Greiner
Robert Greiner

Reputation: 29722

I think you could treat your existing clas as a subclass since it already has some class-specific functionality in it and then look at the Extract Superclass refactoring. Here you would create your new super class and then move common features from the sub class to the super class.

enter image description here

Refactoring for Visual Basic has a nice section on Extract Super Class that you might find interesting.

Upvotes: 1

Related Questions