Model classes vs Interfaces

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

01-08-2018

Hello,

I am slightly new to the world of sling models. More used to JSP and custom taglibs (and implementing my own "idea" of models by using reflections) than models. Whatever i have done with models is limited to simple injections, references and self injections.

Recently i was fooling around with some examples from the sling portal and came across this:

@Model(adaptables=Resource.class) public interface MyModel { @Inject String getPropertyName(); }

Then i started wondering why and when you would ever use interfaces? I tried simple parent child relations and it worked well.  But when would i ever write model interfaces and in which scenarios would i ever use them and how does it work?

Pardon me if the question is too basic.. but just could not get this...

thanks,

Yadhu

View Entire Topic

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

02-08-2018

Hey thanks all for the responses.

smacdonald2008​ is this the paragraph you wanted me to see? I think this is probably the most direct answer to my question.... I need to read up more on adapters i think.... am i right? Apart from that and of course what Jitendra mentioned about the general use of interfaces, i dont see any reason why i would use a interface instead of a concrete class... please let me know what you think

The @Model annotation registers CardImpl as a Sling Model when it is deployed to AEM. The adaptables parameter specifies that this model can be adapted by the request. The adapters parameter allows the implementation class to be registered under the Card interface. This allows the HTL script to call the Sling Model via the interface (instead of the impl directly). More details about adapters can be found here. The resourceType points to the Card Component resource type (created earlier) and helps to resolve the correct model if there are multiple implementations. More details about associating a model class with a resource type can be found here.