Sling Model Delegation Pattern with Lombok
/apps/aem-demo/components/button
. This structure is required for the delegation pattern and keeps the existing behavior intact.button / .content.xml
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Component"
jcr:title="Link & Button"
sling:resourceSuperType="core/wcm/components/button/v2/button"
componentGroup="AEM Demo - Content"/>
ButtonLink.java
public class ButtonLink implements Button {
private String textColor;
private String backgroundColor;
private Button coreButton;
public String getText() {
return coreButton.getText();
}
public Link getButtonLink() {
return coreButton.getButtonLink();
}
// ... rest of the methods implementation ...
public String getTextColor() {
return textColor;
}
public String getBackgroundColor() {
return backgroundColor;
}
}
@Delegate
annotation is used to delegate the methods to the core component. It automatically generates implementations for ALL public methods from the Button
interface and eliminates the need to manually write boilerplate delegation code.ButtonLink.java
public class ButtonLink implements Button {
private String textColor;
private String backgroundColor;
private Button coreButton;
public String getTextColor() {
return textColor;
}
public String getBackgroundColor() {
return backgroundColor;
}
}
getButtonLink
method, you can use the @Delegate(excludes = DelegationExclusion.class)
annotation. Then specify the method signatures in the DelegationExclusion interface and provide your own implementation for those methods.ButtonLink.java
public class ButtonLink implements Button {
private String textColor;
private String backgroundColor;
private Button coreButton;
public Link getButtonLink() {
// ... custom implementation ...
return coreButton.getButtonLink();
}
public String getTextColor() {
return textColor;
}
public String getBackgroundColor() {
return backgroundColor;
}
private interface DelegationExclusion {
String getButtonLink();
}
}
@Delegate
, you can eliminate boilerplate code and focus on your custom functionality while preserving the existing behavior. This approach maintains Adobe's best practices and ensures your customizations remain clean and maintainable for enterprise AEM implementations.