Home > Software engineering >  Right way to group initialization code in Java
Right way to group initialization code in Java

Time:11-17

I have some stuff with similar properties, methods and initialization code. Like

public class LoadElement {

    LoadingElementBinding binding;

    public LoadElement(ViewGroup parent) {
        binding = LoadingElementBinding.inflate(
                       LayoutInflater.from(parent.getContext()),
                       parent,
                       false));
        binding.setLifecycleOwner(ViewTreeLifecycleOwner.get(parent));
    }

    public void doDomething() {
        //somehing do with binding
    }
}

public class ErrorElement {

    ErrorElementBinding binding;

    public ErrorElement(ViewGroup parent) {
        binding = ErrorElementBinding.inflate(
                       LayoutInflater.from(parent.getContext()),
                       parent,
                       false));
        binding.setLifecycleOwner(ViewTreeLifecycleOwner.get(parent));
    }

    public void doDomething() {
        //somehing do with binding
    }
}

I wanna group this code and write some like

public abstract class BindingElement <T extends ViewDataBinding>{

    T binding;

    public BindingElement (ViewGroup parent) {
        binding = createBinding(LayoutInflater.from(parent.getContext()), parent);
        binding.setLifecycleOwner(ViewTreeLifecycleOwner.get(parent));
    }

    abstract T createBinding(ViewGroup parent, LayoutInflater inflater);

    public void doDomething() {
        //somehing do with binding
    }
}

public class LoadElement extends BindingElement<LoadingElementBinding>{

    public LoadElement(ViewGroup parent) {super(parent)}

    @Override LoadingElementBinding createBinding(ViewGroup parent, LayoutInflater inflater){
       return LoadingElementBinding.inflate(
                       LayoutInflater.from(parent.getContext()),
                       parent,
                       false));
    }
}

public class ErrorElement extends BindingElement<ErrorElementBinding>{

    public ErrorElement(ViewGroup parent) {super(parent)}

    @Override ErrorElementBinding createBinding(ViewGroup parent, LayoutInflater inflater){
       return ErrorElementBinding.inflate(
                       LayoutInflater.from(parent.getContext()),
                       parent,
                       false));
    }
}

It is grouped and i will never forget determine my property (binding) when i add new similar class. But this code is wrong cause i use non final or abstract function from the constructor.

But how can i group my code correctly in java oop style?

CodePudding user response:

You could use a lambda expression/method reference:

@FunctionalInterface
public interface BindingCreator<T extends ViewDataBinding>{
    T createBinding(LayoutInflater inflator, ViewGroup parent, boolean addToParent);
}


public abstract class BindingElement <T extends ViewDataBinding>{
    T binding;//maybe make it protected
    public BindingElement(ViewGroup parent, BindingCreator<T> bindingCreator){
        binding = bindingCreator.createBinding(
                       LayoutInflater.from(parent.getContext()),
                       parent,
                       false));
        binding.setLifecycleOwner(ViewTreeLifecycleOwner.get(parent));
    }

    public void doDomething() {
        //somehing do with binding
    }
}

public class LoadElement extends BindingElement<LoadingElementBinding>{
    public LoadElement(ViewGroup parent) {
        super(parent, LoadingElementBinding::inflate);
    }

}
public class ErrorElement extends BindingElement<ErrorElementBinding>{

    public LoadElement(ViewGroup parent) {
        super(parent, ErrorElementBinding::inflate);
    }

}

BindingCreator contains a "single abstract method" createBinding and the expression (method reference) LoadingElementBinding::inflate creates a BindingCreator where createBinding is implemented by calling LoadingElementBinding.inflate with the same arguments. Instances like that are just passed to the super constructor.

  • Related