Skip to content

Reduce Explicit Instance Creation #44

Open
@danrods

Description

@danrods

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

  1. 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);
 }

...
}

  1. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions