| Uniform Access Principle |
Article Index for Uniform |
Website Links For Uniform |
Information AboutUniform Access Principle |
| CATEGORIES ABOUT UNIFORM ACCESS PRINCIPLE | |
| articles with example python code | |
|
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 to Object-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 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 Foo.bar = 5 //Assigns 5 to the object variable "bar"then these operations should be the same :
When executed, should display :
This allows the object to still hide information as well as being easy to access. The same should be true for setting the data.
= Language Examples = RUBY Ruby Programming Language :
end end y = Foo.new(2) puts y.x puts y.x_times_5 This outputs:
Note how even though x is an attribute and x_times_5 is a parameterless method call, they're accessed the same way. PYTHON Python Programming Language class Foo(object): def __init__(self, x): self.setx(x) def getx(self): return self.__x def setx(self, x): self.__x = x def getx_times_5(self):
x = property(getx, doc="getter for x") x_times_5 = property(getx_times_5, doc="getter for x, times 5") y = Foo(2) print y.x print y.x_times_5 This outputs:
Python Properties can be used to achieve UAP. It should be noted that this method of using properties to map other functions to variable access matches Meyer's idea of UAP closely ( Eiffel uses a similar mechanism). SEE ALSO
|
|
|