Skip to content

Instantly share code, notes, and snippets.

@girish3
Last active August 16, 2018 06:00
Show Gist options
  • Save girish3/f2be222cc0d482083b6f3539bb309b5f to your computer and use it in GitHub Desktop.
Save girish3/f2be222cc0d482083b6f3539bb309b5f to your computer and use it in GitHub Desktop.
[Building an object model without setters] #design_pattern

Some pointers

  • Having setters violates open/close principle, prevents information hiding (breaks encapsulation)
  • Discussing getter/setters vs public fields often obscures bigger problems with objects manipulating each others' internal state in an intimate manner and hence being too closely coupled. The idea is to make methods do what your business logic wants it to do, rather than have setters which change things at a field level.
  • One way to avoid writing setters is a task based approach to the model. Think of every task that is performed in an application and add a method that changes all the affected fields at once, to perform this task.

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment