[Tutor] Question about 'Predicate Methods'
alan.gauld@bt.com
alan.gauld@bt.com
Tue Mar 25 06:58:02 2003
> 'predicate' methods is introduced with reference to access
> control methods protecting object's attributes from being
> 'gotten' at and rendered inconsistent by client code.
That's slightly confusing IMHO.
Access control is not directly related to predicate methods.
Predicate methods(or even predicate functions!) are really methods
which answer a question, they usually return a boolean result.
Examples in a graphics library would include methods like:
isVisible()
overlaps(aShape)
contains(aShape)
canMove(adistance,aDirection)
isShaded()
isTransparent()
etc...
Some of these will be the same as accessor methods in that they
return an attribute value, others (more typically) will return
the logical combination of several values.
> It is described as a 'read-only' access method
> that test the validity of a condition.
That is correct as the examples above illustrate.
They test the state of the object but don't change the state in any way.
> someone point me in the direction of documentation for this
> method (what is and how it works),
Its just a category of method there is no mechanism per se. Its
just one way of describing a particular type of method - in the
same way that various authors use terms like 'accessor', 'modifier',
'constructor', 'persistor' etc... to categorize the various methods
within a class.
Personally I prefer to work on the principle that you determine the
methods that your clsass needs to have to fulfill its responsibilities
and implement them. Don't worry too much about what kind of method it
is, just build whats needed. Concentrating on categories can lead you
down the road of building in unnecessary methods just because you don't,
for example, have any persistence methods defined yet....
Thois is also why I don't like to see a class fuill of getXXX/setXXX
methods. If the class's clients don't need to get/set the attributes then
don't provide the method - thats what inheritance is for!
HTH,
Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/