I'm trying to design a program that uses a third party API. The third party API describes an input with 296 fields, and an output with 179 fields. Obviously I want classes to represent the input and output. Are there any tricks to designing a class with so many fields? Should I have a normal getter and setter for every field?
Note: Because you ask, the API takes a string with all the fields in fixed width format as input, and returns a string with the output also in the fixed width format. It's very hard to interpret a non-flat structure out of that.
Java Classes contains different fields and methods. Fields hold specific data while the methods contain codes that manipulate the data inside the fields. Fields hold data. Fields can be a Primitive Type or a Reference Type.
Get all declared fields from a class in Java. An array of field objects is returned by the method java.lang.Class.getDeclaredFields (). These field objects include the objects with the public, private, protected and default access but not the inherited fields.
Using Multiple Classes. You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.
Java Fields. Fields hold data. Fields can be a Primitive Type or a Reference Type. There are 8 primitive types in java, which are int, byte, long, short, double, float, boolean, and char. To define a field, we need the type of field, the name of the field, and the value of that field. For example:
Yikes.
One options is to simply use a Map or similar property holder.
Another option: use a series of nested classes, to add organization (e.g. Order.Person.ContactInfo.Address.ZipCode, rather then just Order.ZipCode). I'm not at all sure I like this one, as it means additional complexity, but without it, finding the particular getter/setter you want (say via autocompletion in an IDE) becomes a nightmare.
Yet another option: if you do create a single class with many properties, consider using the "expression builder" pattern, in which each "setter" returns the object itself, enabling you to chain setters together:
myObject.setPropertyA("Foo").setPropertyB("Bar").setPropertyC("Baz")...
which can create a quicker and more fluent interface then
myObject.setPropertyA("Foo");
myObject.setPropertyB("Bar");
myObject.setPropertyC("Baz");
...
How about code generation? Create a file with the input parameters and another with the output parameters. Then create whatever code templates you want: getters, setters, constants. Then multiply the two together.
If you need to re-architect it, just modify the templates and regenerate.
Perhaps wrap API methods having unwieldy number of parameters and apply Introduce Parameter Object refactoring repeatedly, grouping logically related parameters into parameter objects, especially if results map into existing model objects wherein adaptor pattern could be applied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With