Skip to content

Remove Clients' Dependency on Concrete Implementations of EventFormat #537

@skepticoitusInteruptus

Description

@skepticoitusInteruptus

Context

The intent of Java™ SE's Service Provider Interface (SPI) mechanism is to facilitate OO best practice. The value the SPI facility is meant to add to the design of a Java™ library, is to decouple clients from concrete implementations of services.

I ❤️ well-designed libraries like Java SDK for Cloud Events that leverage SPI to follow OO best practice:

  1. Program to an interface; not to an implementation
  2. Open for extension; closed for modification
  3. etc.

The Issue

In the usage example [1], the import io.cloudevents.jackson.JsonFormat of the concrete implementation of EventFormat effectively defeats the purpose of using the SPI service abstraction facility.

Current Design

import io.cloudevents.CloudEvent;
import io.cloudevents.core.format.EventFormatProvider;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.jackson.JsonFormat; // Tightly couples this client to a concrete implementation

CloudEvent event = CloudEventBuilder.v1()
    .withId("hello")
    .withType("example.vertx")
    .withSource(URI.create("http://localhost"))
    .build();

byte[]serialized = EventFormatProvider
    .getInstance()                          // Since clients need the concrete impl in spite of SPI, why not do this: 
    .resolveFormat(JsonFormat.CONTENT_TYPE) // var json = new JsonFormat(); 
    .serialize(event);                      // var serialized = json.serialize(event);

Proposed Enhancement

import io.cloudevents.CloudEvent;
import io.cloudevents.core.format.EventFormatProvider;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.format.ContentTypes; // Introduce constant to decouple clients from implementations

CloudEvent event = CloudEventBuilder.v1()
    .withId("hello")
    .withType("example.vertx")
    .withSource(URI.create("http://localhost"))
    .build();

byte[] serialized = EventFormatProvider
    .getInstance()
    .resolveFormat(ContentTypes.JSON;) // No longer coupled to concrete io.cloudevents.jackson.JsonFormat
    .serialize(event);

——

[1] https://bit.ly/CEJsonUsgEg

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions