- Uniform access principle
The Uniform Access Principle was put forth by
Bertrand Meyer . It states "All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation." This principle applies generally toobject-oriented programming languages . In simpler form, it states that there should be no difference between working with an attribute, precomputed property, or method/query .While most examples focus on the "read" aspect of the principle, Meyer shows that the "write" implications of the principle are harder to deal with in [http://www.eiffel.com/general/monthly_column/2005/Sept_October.html his monthly column] on the Eiffel programming language official website.
Many languages have various degrees of support for UAP, where some of the implementations violate the spirit of UAP.
= UAP Example =If a language allows access to a variable via dot-notation and assignment
then these operations should be the same :Foo.bar = 5 //Assigns 5 to the object variable "bar"
When executed, should display ://Assume print displays the variable passed to it, with or without parens//Assume Foo.bar = 5 for nowprint Foo.barprint Foo.bar()55This allows the object to still hide information as well as being easy to access.
The same should be true for setting the data.
Foo.bar = 5Foo.bar(5)//These should achieve the same goal= Language Examples =
Ruby
This outputs:
210Note how even though
x
is an attribute andx_times_5
is a parameterless method call, they're accessed the same way.
= Python =This outputs:
210
Python properties can be used to achieve UAP. This method of using properties to map other functions to variable access matches Meyer's idea of UAP closely (Eiffel uses a similar mechanism).
= PHP =This outputs:
210Observation: There are many other ways to achieve the same functionality in PHP, for example making
x
public
and accessing it directly or using magic methodsfunction __get($variable)
ee also
* [http://c2.com/cgi/wiki?UniformAccessPrinciple The UniformAccessPrinciple on the c2 wiki]
* http://www.eiffel.com/general/monthly_column/2005/Sept_October.html
Wikimedia Foundation. 2010.