How to implements Lombok @Builder for Abstract class

This is possible with lombok 1.18.2 (and above) using the new (experimental) annotation @SuperBuilder. The only restriction is that every class in the hierarchy must have the @SuperBuilder annotation. There is no way around putting @SuperBuilder on all subclasses, because Lombok cannot know all subclasses at compile time. See the lombok documentation for details. Example: … Read more

Lombok builder to check non null and not empty

Maxim Kirilov’s answer is incomplete. It doesn’t check for blank/empty Strings. I’ve faced the same issue before, and I realized that in addition to using @NonNull and @Builder from Lombok, overload the constructor with a private access modifier, where you can perform the validations. Something like this: private Person(final String firstName, final String lastName) { … Read more

How do I initialize classes with lots of fields in an elegant way?

You can either use a constructor or a builder pattern or a variation of the builder pattern to fix the problem of having too many fields in your initialization step. I’m going to extend your example a bit to prove my point of why these options are useful. Understanding your example: Lets say an Offer … Read more

Abstract Factory, Factory Method, Builder

A builder helps you construct a complex object. An example is the StringBuilder class (Java, C#), which builds the final string piece by piece. A better example is the UriComponentsBuilder in Spring, which helps you build a URI. A factory method gives you a complete object in one shot (as opposed to the builder). A … Read more

Can I have an abstract builder class in java with method chaining without doing unsafe operations?

You want to declare T as extends AbstractBuilder<T> in AbstractBuilder. Use an abstract protected method to get this of type T. abstract class AbstractBuilder<T extends AbstractBuilder<T>> { protected abstract T getThis(); public T foo() { // set some property return getThis(); } } class TheBuilder extends AbstractBuilder<TheBuilder> { @Override protected TheBuilder getThis() { return this; … Read more

Java generics + Builder pattern

You were close: Foo.Builder.<Bar> start().setCount(1).setKey(bar).build(); Cheers! 🙂 P.S. If the compiler can’t infer the type parameter of the method on its own, you can force it by calling obj.<Type> method(…) . P.P.S you might want to use: public Foo<K2> build() { return new Foo<K2>(this); } Avoid using raw types.

Spring: Using builder pattern to create a bean

You may try to implement FactoryBean interface: public class HttpFactoryBean implements FactoryBean<HttpClient>{ private String host; private int port; public HttpClient getObject() throws Exception { return new StdHttpClient.Builder() .host(host) .port(port) .build(); } public Class<? extends HttpClient> getObjectType() { return StdHttpClient.class; } public boolean isSingleton() { return true; } public void setHost(String host) { this.host = host; … Read more

Builder Vs Decorator pattern [closed]

From wikipedia’s decorator pattern article: In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically. There’s no need to add toppings to a Pizza after it has been fully constructed. You don’t eat half a pizza and then add another topping to it. … Read more

tech