Mutator method

An access function also access method or accessor in programming jargon getter and setter called, is the object-oriented programming, a special method that retrieves or modifies a single property of an object. They are part of the public interface of an object and hide implementation details of the object. This allows the implementation of an object to be changed without changing its public interface.

Query methods ( getters )

An access method that queries a property of an object is also called query method or getter ( from english to get - pick something ). The property can either directly from a field ( an instance variable ) of the object taken, which then usually has a lower visibility, or are calculated at the moment of the call. For the caller that is not recognizable by the encapsulation is assured.

In some programming languages ​​, it is common to have begin the names of all query methods with get. This could mean, for example, a method that retrieves the name of a person, getName. Alternatively, methods which will prompt Boolean properties that is ( eg isMarried ) or Has start (eg hasChild ).

In other programming languages, this is not common and the method would be to simply name.

Mutators ( setter)

An access method that alters a property of an object is also called change method or setter (from english to set - set something set ). One advantage of the change method is that it can check the value for validity before the change. Thus, for example, in an object that represents a person, ensure the function to set a name for the name is not an empty text is passed. If an invalid value is passed, there is a possible reaction in the throwing of an exception routine, whereby the normal program flow is interrupted and an error is signaled.

Another advantage of the change method is that it can inform (in the sense of the same design pattern ) on the modification of the object's state any observer. This is used for example in the Model View Controller architecture to update a view ( View, such as a screen form) when the contents of the underlying model ( Model, for example, a record in a database table ) was changed.

In some programming languages ​​, it is common to have to start changing the names of all methods with set. This could mean, for example, a method that changes a person's name, setName. In other programming languages ​​this prefix is ​​unusual, and the method would be to simply name.

JavaBeans

JavaBeans components of the Java programming language, detect access functions at runtime via reflection based on their prefixes "get" and "set". The specially -provided class " java.beans.Introspector " derives then automatically the name of the queried or changed property from, so for example, " balance " from " getSaldo " or " birthday " from " setGeburtstag ".

Criticism

Martin Fowler points out in his description of the anti-pattern anemic object model suggests that object models whose objects consist virtually only access functions and offer no going beyond behavior, have no advantages over traditional data models. Such object models are therefore not necessarily make sense.

Pictures of Mutator method

24053
de