Accessor Method Pattern

The values of different instance variables of an object, at a given point of time, constitute its state. The state of an object can be grouped into two categories, public and private. The public state of an object is available to different client objects to access, whereas the private state of an object is meant to be used internally by the object itself and not to be accessed by other objects.

When we have variables with public state, Accessor Method Pattern suggests that all such instance variables being declared as private and provide public methods known as accessor methods to access the public state of an object. This prevents external client objects from accessing object instance variables directly.

  • For a non boolean variable "name" we should define getName and setName.
  • For boolean variable "active" we might use isActive or getActive and setActive.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License