0
\$\begingroup\$

I have a hierarchy like next:

 AbstractRequestWs
   |- CreatorRequestWs
   |- CheckRequestWs
   |- GetRequestWs
   |- DeleteRequestWs

In this implementation I process ws calls with common code. So I've created an Abstract class which contains some common logic and structure.

So I've created a class like next:

public abstract class AbstractRequestWs {

    protected Entity itemMapped;
    protected final EntityToMap entityToMap;        

    public AbstractRequestWs(EntityToMap entityToMap) {
        this.entityToMap = entityToMap;
    }

    public Entity map() {
        doSometing();
        //Do this?? or instead override this method on childs
        if (this instanceof StatusUpdatable) {
            ((StatusUpdatable) this).calcualteStatus();
        }
        return mapToEntity();
    }

    protected abstract void doSometing();

    private Entity mapToEntity() {
       //common impl
    }

}

Then some childs need to update a status (which has a large calculation) and some others not, and I've got the idea to create an Interface StatusUpdatable whith a calcualteStatus method so if a child implements this interface the calculateStatus will be called in the Abstract parent class.

For example creator implements StatusUpdatable, so the method calculateStatus will be called.

 public CreatorRequestWs implements StatusUpdatable {
   //....
     public void calculateStatus() {
        //
     }
 }

Otherwise GetRequestWs doesn't implements StatusUpdatable, so the method calculateStatus won't be called.

public GetRequestWs {
   //....
}

So is this a good idea? Check if a child implements some interface and call a method of this interface, or otherwise override the public method and do it directly?

Otherwise I would have to override the map() method in the classes which is need to calculate status:

public CreatorRequestWs  {

   //....

   @Override
   public Entity map() {
       doSometing();
       calcualteStatus();
       return mapToEntity();
   }

   public void calculateStatus() {
      //...
   }
}

I see is more difficult to read than override the map because how the method calculateStatus is call is hide by the parent classes, but otherwise I don't repeat code.

What do you think?

\$\endgroup\$

closed as off-topic by 200_success May 26 '17 at 6:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

I think the classes that have a calculateStatus method should have it called in the end of their doSomeThing()

Or, if you wish to reuse more code, create a AbstractRequestStatusUpdatableWs, like this:

abstract class AbstractRequestStatusUpdatableWs implements StatusUpdatable
{
     abstract void calculateStatus();
     abstract void doSomeRealWork();

     public final void doSomeThing()
     { 
           doSomeRealWork();
           calculateStatus();
     }
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.