vendredi 19 juillet 2019

How to avoid null implementation of interface methods

There may be some problem in my design too. Here is my problem:

I have AbstractCustomAuthHandler which first;

  • gets IUser (gets users with implementing logic)
  • than calls IUser's IRole object's function (gets roles with the implementing logic)

So in the beginnig of the design; * every IUser implementation has some IRole logic * those are seperated because they are separate rest calls in seperate microservices * but I related them with IUser has a IRole relation

But now there is some implementations that some IUser's implementation should not have IRole object. So for now I'm returning null for this implementations and I didn't like it. I thought about splitting the intefaces but couldn't find the solution which satisfies by AbstractCustomAuthHandler too. Here is a diagram and the code:

Here is the design for the auth

Here is the some part of AbstractCustomAuthHandler

IUser userAuth= this.getUserAuth();
final Response userResponse= userAuth.findUser();
// ...
Map<String, Object> attributes= userAuth.getMappedAttributes();
// ...
IRole roleAuth= userAuth.getRoleAuth();
if (roleAuth!= null)
{
    final Response rolesResponse = roleAuth.findRolesBy();
}
// ....

Here is AuthMethodWithoutRole that I have problem about returning null

public class AuthMethodWithoutRole implements IUser
{
    @Override public Response findUserBy( )
    {
        // some logic
    }

    @Override public IRole getRoleAuth( )
    {
        return null;
    }
}

Here is IUser interface

public interface IUser extends IAuth
{
    Response findUserBy();

    IRole getRoleAuth();
}

Here is IRole interface

public interface IRole
{
    Response findRolesBy( );
}

Aucun commentaire:

Enregistrer un commentaire