lundi 19 novembre 2018

How to organize my framework code to become cleaner and more flexible with Decorator DP?

I've started to work on a tiny framework whose purpose will be to speed up development of applications using Vert.x with Rx-java2.

I wrote a set of base classes (abstract) which add gradually specific features. The developer has to extend the class that (s)he needs according to the features (s)he wants to use. All the hierarchy extends Vert.x framework AbstractVerticle, so all the classes are verticles for Vert.x

These features are declared in interfaces and those base classes implements them.

Interfaces :

  • HttpVerticle : behavior of a verticle that provides HTTP serving capabilities.
  • WebVerticle : behavior of a verticle that provides HTTP request handling capabilities
  • WebSocketVerticle : behavior of a verticle that provides WebSocket messages exchange capabilities
  • JdbcVerticle : behavior of a verticle that provides asynchronous JDBC querying capabilities.

Base classes :

  • BaseVerticle : A base class that holds base features : logging, Observables auto-disposing, and configuration reading.
  • BaseHttpVerticle : A base class that wraps a Vert.x HttpServer and manage the configuration of Http listening port, SSL, etc.
  • BaseWebVerticle : A base class that wraps a Vert.x Router and manage request handling.
  • BaseWebSocketVerticle : A base inherited of BaseHttpVerticle and declare a webSocketHandler.

Here's a UML class diagram generated from the code :

a class diagram UML describing base classes hierarchy

Note that the 3 first classes belongs to the Vert.x framework, my class hierarchy begins with BaseVerticle.

The main problem : in order to cover all features combinations (Web + Jdbc, WS + Jdbc, etc.), I have to write for each of them a corresponding base class implementation (BaseJdbcWebVerticle for example). If the amount of available features increases, I will have to write a very large number of base implementations that potentially repeats code.

It seems inheritance is not flexible enough in this case, and I've heard about Decorator Pattern but I have difficulties understanding it and I don't know how to implement it in my case.

1) How to implement Decorator DP within my base classes hierarchy ?

However, here's an example of a base class usage in the code of a tiny Web controller :

@Configuration
public class AuthenticationVerticle extends BaseJdbcWebVerticle {

    @RequestHandling(path = "/users")
    @ResponseType(ResponseTypeEnum.JSON)
    public Single<Response> getUsers(RoutingContext rc) {
        return connect()
            .flatMap(query().single("SELECT * FROM users"))
            .map(Results::toList)
            .map(Response::ok)
            .onErrorResumeNext(this::defaultExceptionHandler);
    }

    @Override
    public JsonObject jdbcConfiguration() {
        return readConfig("db");
    }
}

You can see that for the moment I use inheritance to create a concrete implementation.

2) What will this code look like when replacing inheritance by decorator DP ?

Hope all that will be clear enough.

Regards,

Aucun commentaire:

Enregistrer un commentaire