Description
Problem
There is a fair amount of direct object creation ( via new
) happening in functions and unit tests in order to work with EventEnvelope
and PostOffice.
If we needed to modify functionality of either class through polymorphism our users would need to modify how they instantiate these objects to take advantage of it. It will also make forward-compatibility a bit more challenging as we continue to evolve the framework without it
Proposal
- Introduce a set-few helper methods for use cases that are frequently used
class EventEnvelope{
...
public static of(){
return new EventEnvelope();
}
public static to(String to){
return of().setTo(to);
}
...
}
class PostOffice{
...
public static withRoute(String myRoute, String myTraceId, String myTracePath){
return new PostOffice(myRoute, myTraceId, myTracePath);
}
public static withHeaders(Map<String, String> headers, int instance){
return new PostOffice(headers, instance);
}
...
}
- Utilize the builder pattern
EventEnvelope is mostly using the builder pattern already by returning this
in most setters.
Create a static Builder
class in EventEnvelope and PostOffice that can convert to an instance and utilizes .with
prefixes over .set
Recommendation
Option 1 (Helper methods) would be preferable for EventEnvelope
as it has the highest ROI, but PostOffice
may benefit more from Option 2 (Builder Pattern) for forward-compatibility - as there are a number of constructors parameters already