Uniform access principle

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 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 [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

Foo.bar = 5 //Assigns 5 to the object variable "bar"
then these operations should be the same :
//Assume print displays the variable passed to it, with or without parens//Assume Foo.bar = 5 for nowprint Foo.barprint Foo.bar()
When executed, should display :
55

This 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

class Foo attr_reader :x def initialize(x) @x = x end def x_times_5 return @x*5 endend

y = Foo.new(2)puts y.xputs y.x_times_5This outputs:

210

Note how even though x is an attribute and x_times_5 is a parameterless method call, they're accessed the same way.


= Python =

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): return self.x*5 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.xprint y.x_times_5This 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 =

class Foo{ private $x;

function __construct($x){ $this->x = $x; }

function x(){ return $this->x; }

function x_times_5(){ return $this->x*5;

$y = new Foo(2);echo $y->x();echo $y->x_times_5();This outputs:

210

Observation: There are many other ways to achieve the same functionality in PHP, for example making x public and accessing it directly or using magic methods function __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.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Uniform civil code — is a term which has originated from the concept of a Civil Law Code. It envisages administering the same set of secular civil laws to govern different people belonging to different religions and regions. This supersedes the right of citizens to… …   Wikipedia

  • Principle of relativity — In physics, the principle of relativity is the requirement that the equations describing the laws of physics have the same form in all admissible frames of reference. For example, in the framework of special relativity the Maxwell equations have… …   Wikipedia

  • Controlled-access highway — Freeway redirects here. For other meanings, see Freeway (disambiguation). A freeway with a wide grass median (Highway 402 in Canada) A controlled access highway is a highway designed exclusively for high speed vehicular traffic, with all traffic… …   Wikipedia

  • Direct memory access — (DMA) is a feature of modern computers that allows certain hardware subsystems within the computer to access system memory independently of the central processing unit (CPU). Without DMA, the CPU using programmed input/output is typically fully… …   Wikipedia

  • Prinzipien Objektorientierten Designs — sind Prinzipien welche zu gutem objektorientierten Design führen sollen. Sie wurden neben anderen von Robert C. Martin, Bertrand Meyer und Barbara Liskov publiziert und propagiert. Viele Techniken der Objektorientierung wie Entwurfsmuster, Domain …   Deutsch Wikipedia

  • Eiffel (programming language) — Infobox programming language name = Eiffel paradigm = object oriented year = 1986 designer = Bertrand Meyer developer = Bertrand Meyer Eiffel Software latest release version = 4.2 latest release date = Feb 6, 1998 typing = static typing, strong… …   Wikipedia

  • Property (programming) — In some object oriented programming languages, a property is a special sort of class member, intermediate between a field (or data member) and a method. You read and write a property just as you read and write a field, but this is (usually)… …   Wikipedia

  • Technical features new to Windows Vista — This article is part of a series on Windows Vista New features Overview Technical and core system Security and safety Networking technologies I/O technologies Management and administration Removed features …   Wikipedia

  • education — /ej oo kay sheuhn/, n. 1. the act or process of imparting or acquiring general knowledge, developing the powers of reasoning and judgment, and generally of preparing oneself or others intellectually for mature life. 2. the act or process of… …   Universalium

  • china — /chuy neuh/, n. 1. a translucent ceramic material, biscuit fired at a high temperature, its glaze fired at a low temperature. 2. any porcelain ware. 3. plates, cups, saucers, etc., collectively. 4. figurines made of porcelain or ceramic material …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”