Implementing new feature using SOLID principles in old code

136 views Asked by At

I just try to get more into SOLID principles but get stuck by implementing new structures in my old (not SOLID) code.

I have this Room.Class

public class Room {
   private String roomCode;
   private String roomDescription;
   // getter/setter
}

Now I need to have a translation for the roomDescription. I started to create an interface

public interface ITranslation {    
    String findTranslation();    
}

and an implementation

public class RoomDescriptionTranslation implements ITranslation {

@Override
public String findTranslation() {
    return "translated Room";
}

In the already existing code there is a service class which creates some Rooms with codes and descriptions. These Rooms are also used in the view (as jsp bean).

The new requirement is to have the translated description on the view.

So for me the question is where I should implement the logic of translation of the existing Rooms.

  • Should I implement it in the existing serivce class where the Rooms are created?
  • Or should RoomDescriptionTranslation be a field inside Room?
  • Or should I created a new service class where just the description gets translated?

Just need a pointer to go to the right direction.

3

There are 3 answers

0
Nazar Merza On BEST ANSWER

It could be first or third option, but not the second option in my opinion. I think one important question, in general for designing any class is this:

For a property p and class C, is p a property of C?

So, in your case the question becomes: is translation a property of Room? Semantically, it sounds that it is not.

Then, you can ask the same question on Room Service class. The answer to that depends on how you defined your service class. Again, another rule that helps to decide whether a property belongs to a class, is this:

What is one singe word or phrase that describes this class?

This goes to the very idea of what a class is in OOP and also to S in SOLID. Once, you ask this question and can describe one single purpose for your class, then you can go back and ask the first question, whether certain property belongs to this class or not.

Now, if your service class is such that, "Handle all room related actions" (not saying this is right, but if this is the case) then you can add one more action to it, namely translation. But, if it is not then you may create a new service, translation.

Considering all this, I lean more towards having a new translation service as it looks

  1. Something independent
  2. Will be easily extendible (compared to other option) like adding more languages
  3. Does not require changing existing code

Again, there might be other factors affecting the whole thing.

1
Marcin Szymczak On

If you want to translate text you should use internationalization solutions which already exist in java.

In your solution you'll create painful maintenance problems and every string which you'll return will be surrounded by if.

0
Dmitri Gudkov On

I would create a model TranslatedRoom extends Room to use only in view this L from SOLID and inside this new model would take care about translations.

Of course if it is possible to refactor service which creates model for views etc.

One more thing (maybe it is S from SOLID) this idea is good if we need to show translated room only in this/these views.