-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Builder should work with inheritance #78
Comments
|
I definitely need this. I have a superclass in a dependency that I need for the subclass builder |
Why not have the builder incorporate the fields from the super class if the super class is also annotated with @builder ? |
Hello @peichhorn, I may have a suggestion for the implementation of the builder pattern, but I think there is still one obstacle to overcome. The way you implemented it is to have a private inner class plus a public method, which doesn't support overriding. Instead, I'm suggesting we do a public static class, and the children's builders would extend their parent's Here is how it looks like: //File A.java public class A {
protected int myAttr;
public static class Builder {
protected A toBuild;
public Builder() {
toBuild = new A();
}
public Builder myAttr(int myAttr) {
toBuild.myAttr = myAttr;
return this;
}
public A build() {
return toBuild;
}
}
} //File B.java public class B extends A {
protected int myOtherAttr;
public static class Builder extends A.Builder {
protected B toBuild;
public Builder() {
super();
super.toBuild = new B();
this.toBuild = (B) super.toBuild;
}
public Builder myOtherAttr(int myOtherAttr) {
this.toBuild.myOtherAttr = myOtherAttr;
return this;
}
@Override
public B build() {
return this.toBuild;
}
}
} Let me know if this is of any help, and great job for everything |
I vote on this issue too. Somehow inheritance need to be handled. |
+1 to the issue! |
+1 for this issue ! Any idea when a solution would be implemented ? I think the solution described here is a good one and I've already used it in my code with success. |
+1 to the issue! |
+1 |
4 similar comments
+1 |
+1 |
+1 |
+1 |
+1 as well |
+1 |
👍 |
1 similar comment
+1 |
You're probably better off raising this with the core Lombok project as this repo has had no changes since March 2013. |
Yeah, noticed that also, raised the issue in the official repo already :) |
I can't find the issue in the official repo anymore. It might help if you can provide the link :-) |
Actually I started in the group (my bad on the ref) https://groups.google.com/forum/#!topic/project-lombok/-6b9dPH8qAw and I just saw that it will not be available in the short term :( |
+1 |
4 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
6 similar comments
+1 |
+1 |
👍 |
+1 |
+1 |
+1 |
...5 years later... |
+1 |
4 similar comments
+1 |
+1 |
+1 |
+1 |
+1 comments are not speeding up this fix, but only spam inboxes of subscribed users. Please use reaction button instead. Thanks! |
👍 |
Uhm can we please discuss why lombok should support inheritance on a builder? Maybe i do not understand the builder pattern properly. What i'd expect is that every class must have it's own builder. The inheritance you may want would rather suite a factory pattern. So you want to different things but made in a pretty similar way. Though inheritance may not by the best example for the pattern it would work. Adding inheritance handling to a builder feels like abusing the builder pattern. I do not say that it is not possible or that it can have a positive effect for java oop but it is not behavior i'd expect when using a builder. Which means that when lombok would support such a mechanic a few users in this world are using patterns wrong and create misleading apis and that is where i see no benefit. Please correct me if i think in the wrong direction but there might be a good reason why this issue is still open after five years. |
Will try to explain what I need. Let see example what I have. Maybe there is a pattern for above example (factory pattern doesn't look like solve this issue or I am missing something). I will be grateful if you show me it or suggest other way of the issue solving. |
Why don't you add the class variables you need on B to class A? If you need to represent the object in a form that validates to A you'd then serve the default values which may be NULL or something else. In the case that you want your object to represent state B you use the corresponding methods on the builder. It is also still valid to say that you want to use oop concepts like inheritance but first think about what makes B different from A. The builder pattern is only a creational pattern so it should only affect the way of instantiation. You may circumvent your problem with this solution. You will lose the builder on the parent class (A) but keep it on B. This is okay because it still fits my solution mentioned first. If you want to represent A you may use the builder on a representation of B which suits A. Consider: The builder pattern does exactly what you want: Slim constructors because the class is very complex and very customizable. Its goal is not to reduce your boilerplate code. See Wikipedia for examples on that matter and for an explanation of the builder pattern. Maybe we can find a use-case where it would be absolutely helpful to incorporate that feature but i cannot think of any right now. It should be more practical and concrete than class A and B. This is only my opinion on the matter just consider it. |
To make something clear: We do not necessarily need inheritance on the builders. It's just the only solution we came up so far to solve another problem: We want the builder of our class to be able to set fields from superclasses.
This may be a solution in this example; however, there are problems where instances of A simply do not have these fields, for instance because there are other subclasses deriving from A.
The factory pattern is used when you want to create objects without having to specify the exact class of that object (e.g., when you have an API that allows creating instances of an interface, but you want to make the concrete implementation an implementation detail and keep it interchangeable). |
This is exactly what I meant. I need ability to set superclass' fields in subclass builder. Superclass can even not to have own Builder. It will not break Builder pattern (as far as I understand) because if I am creating Builder in vanilla java it is possible to set superclass' fields. |
The question is, why sould a builder not support class hierarchies? The builder pattern never prohibited classes in a hierarchy to be built. |
The valid argument for me is that the builder is not properly inherited. I was focused that the Pattern itself would be the troublesome point but what janrieke and niks- explained was that the inheritance of class variables is broken which must be supported. I really didn't wanted to flame this topic but find the detailed problem and a possible solution. I'll be quite from now on. |
We didn't mean to silence you. :) |
@getJack I'm sorry if my words made you think so. I didn't mean that this is a bug and must be fixed immediately. I meant that it's nice to have such feature in Lombok Bulder if creators agree to it. I tried to explain why I want to it and how it will be convenient for me and just wrote down my opinion and my arguments. But decision is up to creators and maintainers. I'm not pushing. I will use Builder and entire Lombok anyway because it's cool. |
+1 |
As |
i can't use @SuperBuilder in parent (it is library class) |
Would be nice to also have a way to specify extra attributes in the builder to be used in the superclass constructor call.
Or if the superclass has a builder it would be nice use this builder when creating the builder of the subclass.
The text was updated successfully, but these errors were encountered: