-
Notifications
You must be signed in to change notification settings - Fork 166
Closed
Labels
enhancementNew feature or requestNew feature or request
Milestone
Description
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:
- Program to an interface; not to an implementation
- Open for extension; closed for modification
- 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);——
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request