Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java inheritance, using builder pattern

I have 3 classes:

  1. Error
  2. ShellError
  3. WebError

where

ShellError extends Error 

and

WebError extends Error

In ShellError there are fields some of which are optional and others are required. I am building the object in the following manner:

shellError = new ShellError.Builder().setFile(filePattern)
.setHost(host).setPath(path).setSource(file.isSource())
.setJobName(p.getJobName()).build();

Since ShellError extends Error, I further:

shellError.setDescription(msg.toString());
shellError.setExceptionClass("MyEvilException");
shellError.setExceptionMessage("Some clever error message");
shellError.setStacktrace(stack);

So ... why bother with Builder? I like the fact that my build() amongst other things conveniently validates that all fields are set appropriately etc.

I would love it if I could .. build() ShellError and add to it the fields from the Error class.

What i did works.

  • The question is:

Is there a better way, or does it make sense what I did?

-- EDIT

I updated Builder() to accept some of the parameters which were in Error class before. Now I have

shellError = new ShellError.Builder(exception, "Some description").setFile(filePattern).setHost(host)
.setPath(path).setSource(file.isSource()).
setJobName(p.getJobName()).build();

What do you say? Better? Worse?

like image 452
James Raitsev Avatar asked Mar 18 '11 00:03

James Raitsev


People also ask

How do you inherit builders?

Builder Inheritance With Generic TypeA type parameter S is added to the Level0. Builder class which is defined as a subtype of Level0. Builder class. The method id() is also updated to return an S type instead of Level0.

What is builder pattern in Java with example?

Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.

What is difference between @builder and @SuperBuilder?

The @SuperBuilder annotation produces complex builder APIs for your classes. In contrast to @Builder , @SuperBuilder also works with fields from superclasses. However, it only works for types. Most importantly, it requires that all superclasses also have the @SuperBuilder annotation.

When should we use builder pattern in Java?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.


1 Answers

The builder pattern, popularized by Josh Bloch, has several benefits, but it doesn't work so elegantly on parent/subclasses, as explained in this discussion by our colleagues in the C# world. The best solution I have seen so far is this one (or a slight variant of it).

like image 162
Dan R. Avatar answered Oct 01 '22 20:10

Dan R.