Skip to content
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

Create a builder pattern for the objects that have longer parameter lists #3

Open
goneall opened this issue Dec 27, 2019 · 4 comments

Comments

@goneall
Copy link
Member

goneall commented Dec 27, 2019

e.g. SPDX Listed License

@goneall
Copy link
Member Author

goneall commented Mar 25, 2020

See SpdxFile for an example of the builder pattern. This pattern was introduced late in the development of the library. It would be nice to add this to some of the license classes that were implemented previously.

@amCap1712
Copy link
Contributor

amCap1712 commented Jan 9, 2021

Hi @goneall, I have begun some work on this here. I'll add documentation and tests for this and then proceed to find other possible use cases for builders. Do you have more such cases for using builders in mind ?

Also, I felt a little awkward while adding the builder. The large number of possible arguments make sense for adding the Builder but having multiple public constructors in addition to it make the API of the class a bit confusing. It would be interesting to explore if the public constructors can be replaced with static factory methods for common use cases.

@goneall
Copy link
Member Author

goneall commented Jan 11, 2021

Hi @amCap1712 Thanks for taking this on.

One suggestion - In the other classes that use builders, I used a slightly different pattern where the builder is passed in as a parameter to a constructor for the class rather than having a method which returns the instantiated class from the builder itself (see

protected SpdxFile(SpdxFileBuilder spdxFileBuilder) throws InvalidSPDXAnalysisException {
)

Either (or both) approach is fine, but for consistency I would like to pass the builder in as a constructor.

Also, I felt a little awkward while adding the builder. The large number of possible arguments make sense for adding the Builder but having multiple public constructors in addition to it make the API of the class a bit confusing. It would be interesting to explore if the public constructors can be replaced with static factory methods for common use cases.

There may be some compatibility considerations for code which is already using these constructors. That being said, simplifying the code would definitely be a good thing and I would be interested in your proposal and thoughts.

Note that there are a lot of helper methods in ModelObject to create the model objects - I think this will be the dominant use case with the other constructors just being around for compatibility.
See

public SpdxFile.SpdxFileBuilder createSpdxFile(String id, String name, AnyLicenseInfo concludedLicense,
for an example.

@amCap1712
Copy link
Contributor

I agree with you regarding the compatibility restraints. I noticed the library is still 0.6. I am not aware of how widely it is used and the status section in the README says that it is under development. If the library is mostly used in other SPDX projects, it might be easier to make an incompatible change.

For now, I'll just add the builder and be consistent with the other parts of the code. Later, once I am more familiar with the project, I'll try to come back to this and see if we have some room for improvement here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants