java - Abstract logic to avoid duplicate implementations - Stack Overflow

admin2025-04-26  3

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
 }
}
Share Improve this question edited Jan 15 at 10:23 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Jan 15 at 6:23 Jan KückJan Kück 1331 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 {
}
转载请注明原文地址:http://anycun.com/QandA/1745597251a90968.html