spdx / Spdx-Java-Library

Java library which implements the Java object model for SPDX and provides useful helper functions
Apache License 2.0
32 stars 33 forks source link

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

Open goneall opened 4 years ago

goneall commented 4 years ago

e.g. SPDX Listed License

goneall commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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 https://github.com/spdx/Spdx-Java-Library/blob/7e3223ec6847989d7d6fb3964b9987c6cb896b85/src/main/java/org/spdx/library/model/SpdxFile.java#L85)

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 https://github.com/spdx/Spdx-Java-Library/blob/7e3223ec6847989d7d6fb3964b9987c6cb896b85/src/main/java/org/spdx/library/model/ModelObject.java#L1145 for an example.

amCap1712 commented 3 years ago

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.