I need to abstract the following structure to avoid implementing logic that is executed in x twice in y. Below is the code structure.
A bit more specific: The first code part is used by a JSF framework. Therefore, the class of the SpecificHandler is also annotated with @ViewScoped. The SpecificOnCreate class is called from a REST API and is therefore stateless. Logic that is executed in SpecificHandler should now also be executed in SpecificOnCreate and must therefore be abstracted. Does anyone have any ideas on this?
public interface Handler {
Container createComponents(ar1, arg2, arg3);
}
public class DefaultHandler implements Handler {
@Override
public Container createComponent(arg1, arg2, arg3) {
// logic here
}
}
@ViewScoped
public class SpecificHandler extends DefaultHandler {
@Override
public Container createComponent(arg1, arg2, arg3) {
// logic here
}
}
public interface OnCreate {
Dto onCreate(ar1);
}
public abstract class OnCreateBase implements OnCreate {
@Override
public Dto onCreate(arg1) {
// logic here
}
}
public class SpecificOnCreate extends OnCreateBase {
@Override
public Dto onCreate(arg1) {
// logic here
}
}
I need to abstract the following structure to avoid implementing logic that is executed in x twice in y. Below is the code structure.
A bit more specific: The first code part is used by a JSF framework. Therefore, the class of the SpecificHandler is also annotated with @ViewScoped. The SpecificOnCreate class is called from a REST API and is therefore stateless. Logic that is executed in SpecificHandler should now also be executed in SpecificOnCreate and must therefore be abstracted. Does anyone have any ideas on this?
public interface Handler {
Container createComponents(ar1, arg2, arg3);
}
public class DefaultHandler implements Handler {
@Override
public Container createComponent(arg1, arg2, arg3) {
// logic here
}
}
@ViewScoped
public class SpecificHandler extends DefaultHandler {
@Override
public Container createComponent(arg1, arg2, arg3) {
// logic here
}
}
public interface OnCreate {
Dto onCreate(ar1);
}
public abstract class OnCreateBase implements OnCreate {
@Override
public Dto onCreate(arg1) {
// logic here
}
}
public class SpecificOnCreate extends OnCreateBase {
@Override
public Dto onCreate(arg1) {
// logic here
}
}
You can implement the logic inside the interface as default implementation. Thus, avoid duplicating it. Refer to https://www.baeldung.com/java-static-default-methods#why-interfaces-need-default-methods for more details.
e.g.
public interface Handler {
default Container createComponents(ar1, arg2, arg3) {
// logic here
}
}
public class DefaultHandler implements Handler {
}
@ViewScoped
public class SpecificHandler extends DefaultHandler {
}