java - Public fields in a Data Transfer Object -


in years of programming i've made classes group few variables setters , getters. i've seen these types of objects referred value objects, domain objects or model objects depending on context in used. fitting term generic usage seems data transfer object (dto). describes pojo contains accessors , mutators.

i've written 1 such object contains fifty fields used set theme parameters on chart. i'm wondering if instead of generating hundred getters , setters should declare these fields public. doing goes against programming instincts tell me yet can't deny increase code's legibility , reduce amount of boilerplate code in class.

the reason can see not use public fields if needed perform sort of validation on these fields. if assume type validation sufficient purposes, using public fields in scenario acceptable break object-oriented design? public dto perform better in large batch operations?

most programmers default private fields getters/setters without thinking it. cargo-cult thing, it's better make conscious decision.

the main reason using getter/setter combination instead of public field can change definition. so, if dto part of interface between components, best use getters. if change inner workings, can adapt getter mimic old behaviour , maintain compatibility.

another reason can make read-only fields. dtos, read-only , immutable choice.

a third reason dto needs javabean because intend use in tool requires it.

if none of these properties go you, there's no reason not use public fields.

don't expect of performance difference though :)


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -