Skip to main content
Corrected the phrase "naming" to "naming conventions" in order to prompt users (both amateur and professional) to share typical standards they use in naming, declaring variables, code structure, etc.
Source Link

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 PlainCars and return them as RegisteredCars. 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.

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 PlainCars and return them as RegisteredCars. 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 would also be appreciated because PlainCar sounds quite awkward to me.

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 PlainCars and return them as RegisteredCars. 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.

Tweeted twitter.com/#!/StackProgrammer/status/555726819429994496
Source Link
valenterry
  • 2.4k
  • 17
  • 21

How to use scala case classes when delegation is needed

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 PlainCars and return them as RegisteredCars. 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 would also be appreciated because PlainCar sounds quite awkward to me.