Let's assume in our application we want to model cars. We also want to model a car repository where we store some registered cars. How should that be modeled in scala?
Here comes my approach: First, I create a case class PlainCar
. This is just a car how it exists in real world, with nothing special in it. Next I create a CarRepository
. I also create a RegisteredCar
. The CarRepository
can now store PlainCar
s and return them as RegisteredCar
s. Both PlainCar
and RegisteredCar
extend the trait Car
which provides all the comon methods like drive
. RegisteredCar
however is special - it owns an instance of PlainCar
but also adds a method registrationNumber
which returns the registration number; all other methods are just delegated to the PlainCar
instance.
I feel however, that there are some flaws of this design. One is that I have to change RegisteredCar
when I add a method/property to PlainCar
which I think should not have to be done.
My question is therefore, can this be modeled better and if so, how? Are there other drawbacks that I am missing?
Some advice on naming conventions would also be appreciated because PlainCar
sounds quite awkward to me.