lundi 27 juillet 2020

Properly use adapter pattern in Spring Boot

I am trying to implement an adapter to connect with multiple vendors. The issue is the spring dependencies are becoming unwieldy. I find myself having to edit the adapter class, each adapter sub class, and each controller class that uses the adapter methods. This is how I am using the adapter:

public interface IAdapterBuilder {
  public IAdapterBuilder withMyString(String myString);
  public IAdapter build();
}
public abstract class BaseAdapter {

  final String myString;

 public static IAdapterBuilder getBuilder(){
    return SubAdapter.getBuilder();
}

void myAdapterMethod(){
System.out.println("baseAdapter running myAdapterMethod")
}

void setMyString(String myString) {
    this.myString = myString;
  }
}

public class SubAdapter extends BaseAdapter implements IAdapter {

protected SubAdapter(UUID SubAdapterId) {
    super(SubAdapterId);
  }

public static Builder builder() {
    return new Builder();
  }

public static final class Builder implements IAdapterBuilder {

String myString = null;

    @Override
    public IAdapterBuilder withMyString(String myString) {
      this.myString = myString;
      return this;
    }

public SubAdapter build() {
      if (SubAdapterObject == null) {
        throw new IllegalStateException(
            "Unable to create an adapter without a Synchronization record.");
      }


      SubAdapter adapter = new SubAdapter(SubAdapterObject.getId());
      if (myString != null) {
        adapter.setMyString(myString);
      }
   }
}

@RestController
public class MyController {

private final myString;

@Autowired
  public MyController(@Value("${io.org.service.myString}") String myString){
    this.myString = myString;
}

@GetMapping
public void test(){
IAdapterBuilder adapterBuilder =
        BaseAdapter.getBuilder(provider).withMyString(myString);
    }
}

Basically anytime I want to add a dependency to the SubAdapter I need to modify all of these files. There has to be a better way to do this. I would appreciate any help.

Aucun commentaire:

Enregistrer un commentaire