2

Which one of the following a better way of defining a sling model and why?

@Model(adaptables=Resource.class)   
public interface MyModel {  

     @Inject   
     String getPropertyName();   
}  

OR

@Model(adaptables=Resource.class)  
public class MyModel {  

     @Inject  
     private String propertyName;   
}  

Can you tell me a defined use case for using an interface as a model when all the methods are to be overridden in all the implementation classes?

Guillaume S
  • 1,462
  • 2
  • 19
  • 31

2 Answers2

2

Use an interface when you access values of the ValueMap without any need to provide an additional view of the data. Class based models are used, when you need to apply transformations to the data or add additional data (via OSGI services etc).

Florian Salihovic
  • 3,921
  • 2
  • 19
  • 26
0

It strongly depends on the usage. In the case where you add the annotation to the getter you could also go with an interface instead of a class.

When you want to get a data attribute and manipulate it e.g. shorten a string or something, then it makes sense to inject it in a variable and then use a getter to return the shortened string.

Tim
  • 287
  • 1
  • 13