Access function

from Wikipedia, the free encyclopedia

An access function , also called access method , accessor or getter and setter in programmer jargon , is a special method in object-oriented programming that queries or changes a single property ( attribute ) of an object. Access functions are part of the public interface of an object and hide implementation details of this object. This allows the implementation of an object to be changed without changing its public interface.

Query methods (getter)

An access method that queries a property of an object is also called a query method or getter (from English to get - to get something). The property can either be taken directly from an object attribute , which then normally has less visibility , or it can be calculated at the moment it is called. This cannot be seen by the caller; this ensures encapsulation .

In some programming languages , it is common practice to start the names of all query methods with get. So could z. B. called a method that asks for a person's name getName. Alternatively, methods that query Boolean values ​​can begin with is(e.g. isMarried) or has(e.g. hasChild).

In other programming languages ​​this is unusual and the method would be called simple name, since the function call itself already involves a fetch .

Modification methods (setter)

An access method that changes an object attribute is also called a change method or setter (from English to set - to set something, to set). One advantage of the change method is that it can check the validity of the value before the change. For example, in an object that represents a person, the function for setting the name could ensure that no empty text is passed as the name. If an invalid value is transferred, a possible reaction is the triggering of an exception routine , which interrupts the normal program sequence and signals an error.

Another advantage of the change method is that it can inform any observers (in the sense of the design pattern of the same name ) about the change in the state of the object. This is for example in model-view-controller used architectures to a view ( View , z. B. a screen form) to update when the contents of the underlying model ( model z. B. a record in a database table) has been changed .

In some programming languages, it is common practice to start the names of all modification methods with set. So could z. B. called a method that changes a person's name setName. In other programming languages ​​this prefix is ​​unusual and the method would be called simple name, since the parameterized function / procedure call as such implies a setting method.

JavaBeans

JavaBeans , the components of the Java programming language , recognize access functions at runtime by reflection based on their prefixes getand set. The specially provided class java.beans.Introspectorthen automatically derives the name of the requested or changed property, for example saldofrom getSaldoor geburtstagfrom setGeburtstag.

criticism

In his description of the anti- pattern anemic object model , Martin Fowler points out that object models whose objects practically only consist of access functions and do not offer any further behavior have no advantages over classic data models . Such object models are therefore not necessarily useful.

Individual evidence

  1. Graham Hamilton (Ed.): Java Beans API specification . Sun Microsystems , Inc., Aug. 8, 1997, p. 54 ( online - version 1.01).
  2. Martin Fowler: AnemicDomainModel. In: MF Bliki. November 25, 2003, accessed March 25, 2009 .