1
Answer

OOP Design Question

Ask a question
S Osh

S Osh

16y
2.2k
1
Hello,

I just would like to know the best practice for dealing with this situation in OOP, specifically C#.  This is probably a very easy question for seasoned OOP people, and I can think of a couple of ways I could possibly do it, but I'm not sure what the best way to go is. So:

We have a base class eg 'Vehicle'.

This base class defines certain common properties and methods such as:
Weight
Height
RegistrationNumber
CalculateAge()
etc.

There are a number of derived classes that deal with specific types of vehicle, such as
Truck
Pickup
Car
Bus
etc.

Subclasses obv have their own properties and methods unique to the specific type of vehicle.  Truck, for example might have:
MaxLoadWeight
IsArticulated


So my question is..

I want to define a method where I can provide the RegistrationNumber of a Vehicle of unknown type, and have an object returned to me which is of the correct sub class.  For example I want a Car object returned if the RegistrationNumber I provide belongs to a car, and a truck object returned if it belongs to a truck.  What is the best way to do this?

Perhaps sub-classing isn't event the best approach.  Maybe something interface based would be better?  Any opinions??

Many Thanks!

S

Answers (1)