I have this design which I self-proclaim to be Composite Pattern, though I'm not entirely sure about that. So I'm aksing for your statement on this.
This is the interface which collectively describes all of them
public interface DomainResourceBuilder<T extends DomainResource> {
T buildInsertion(T persistence, EntityManager entityManager) throws Exception;
T buildUpdate(T model, T persistence, EntityManager entityManger);
<E extends T> DomainResourceBuilder<E> and(DomainResourceBuilder<E> next);
}
This one describes DomainResourceBuilder as a pair
public abstract class AbstractDomainResourceBuilder<D extends
DomainResource> implements DomainResourceBuilder<D> {
@Override
public <E extends D> DomainResourceBuilder<E>
and(DomainResourceBuilder<E> next) {
return new CompositeDomainResourceBuilder<>(this, next);
}
private class CompositeDomainResourceBuilder<T extends D>
extends AbstractDomainResourceBuilder<T> {
private final DomainResourceBuilder<D> parentBuilder;
private final DomainResourceBuilder<T> childBuilder;
public
CompositeDomainResourceBuilder(DomainResourceBuilder<D>
parentBuilder,
DomainResourceBuilder<T> childBuilder) {
super();
this.parentBuilder = parentBuilder;
this.childBuilder = childBuilder;
}
@SuppressWarnings("unchecked")
@Override
public T buildInsertion(T model, EntityManager
entityManager) throws Exception {
return childBuilder.buildInsertion((T)
parentBuilder.buildInsertion(model, entityManager),
entityManager);
}
@SuppressWarnings("unchecked")
@Override
public T buildUpdate(T model, T persistence,
EntityManager entityManger) {
return childBuilder.buildUpdate(model, (T)
parentBuilder.buildUpdate(model, persistence,
entityManger), entityManger);
}
}
}
Concrete class plays the Leaf role
public class CustomerBuilder extends AbstractDomainResourceBuilder<Customer> {
@Override
public
Customer buildInsertion(Customer persistence, EntityManager entityManager) throws Exception {
return persistence;
}
@Override
public
Customer buildUpdate(Customer model, Customer persistence, EntityManager entityManger) {
return persistence;
}
}
Can I call this a Composite Pattern?
This is how I use it. Assumming I have the following hierarchy.
AbstractEntity
|___User
|___Customer
Now I want to implement different logics on each class, so with the design I'll create specific logic for those class and then ultimately compose them into one, which is still one of their kind. Something like this.
// group of objects
DomainResourceBuilder<AbstractEntity> abstractEntityBuilder = new AbstractEntityBuilder<>();
DomainResourceBuilder<User> userBuilder = new UserBuilder<>();
DomainResourceBuilder<Customer> customerBuilder = new CustomerBuilder<>();
// treat them as a whole (unify them all)
DomainResourceBuilder<Customer> compositeCustomerBuilder =
abstractEntityBuilder
.and(userBuilder)
.and(customerBuilder);