User Tools

Site Tools


at:tutorial:objects

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
at:tutorial:objects [2007/07/09 21:03] – * tvcutsemat:tutorial:objects [2007/07/09 21:25] – explained tvcutsem
Line 45: Line 45:
  
 ===== Cloning and instantiation ===== ===== Cloning and instantiation =====
-As said before in this section, AmbientTalk objects are created [[objects#Objects,_fields_and_methods|ex-nihilo]] or by cloning and adapting an existing object. The code below shows the instatiation of a new ''point'' object by using the cloning semantics.+As noted above, AmbientTalk objects are created [[#Objects,_fields_and_methods|ex-nihilo]] or by cloning and adapting an existing object. The code below shows the instatiation of a new point object by //instantiating// the ''Point'' prototype:
  
 <code> <code>
-def anotherPoint := point.new(2,3)+def anotherPoint := Point.new(2,3)
 </code> </code>
  
-Every object understands the message ''new'', which creates a clone (a shallow copy) of the receiver object and initializes the clone by invoking its ''init'' method with the arguments that were passed to new (''aX'' and ''aY'' in the example of the ''point'' object). Hence, the ''init'' method plays the role of constructor” for AmbientTalk objects. AmbientTalk’s object instantiation protocol closely corresponds to class instantiation in class-based languagesexcept that the new object is a clone of an existing objectrather than an empty object allocated from a class. +Every object understands the message ''new'', which creates a clone (a shallow copy) of the receiver object and initializes the clone by invoking its ''init'' method with the arguments that were passed to new (''aX'' and ''aY'' in the example code). Hence, the ''init'' method plays the role of "constructorfor AmbientTalk objects. Hencein the code above, ''anotherPoint'' shares its methods with its ''Point'' prototype, but it has its own set of fields:
- +
-AmbientTalk also provides a ''clone'' language contsruct which only creates a clone of the receiver object without calling the ''init'' method (as a matter of fact the ''new'' message desribed above does nothing more but invoking this construct and the ''init'' method subsequently).+
  
 <code> <code>
-def clonedPoint := clone: point+anotherPoint.x 
 +>> 2 
 +> Point.x 
 +>> 0 
 +> anotherPoint.x := 
 +>> nil 
 +> Point.x 
 +>> 0
 </code> </code>
  
-===== Delegation and cloning ===== +<note> 
-AmbientTalk features object inheritance or delegationBy means of delegation, an object can reuse and extend the defintion of another establishing parent-child relationship. We identify two kinds of delegation relationships: **IS-A** and **SHARE-A**These relationships define two different semantics for clonning child objects. Whereas clonning a **IS-A** child also clones its parent**SHARE-A** child shares the parent of the cloned object (see the figure below).+AmbientTalk'object instantiation protocol closely corresponds to class instantiation in class-based languagesThe major difference lies in the evaluation context of the ''init'' method: in a class-based languagethe constructor is ran in the context of an empty object, freshly allocated from the class blueprint. In AmbientTalk, the ''init'' method is ran in the context of a shallow copy of an original objectHencein the ''init'' method, fields do not necessarily contain ''nil'' values: they have the value of the clonee. This can sometimes be useful to express the state of a clone as a delta w.r.t. the state of its clonee. 
 +</note>
  
 +AmbientTalk also provides a ''clone:'' construct which only creates a clone of the receiver object without calling the ''init'' method ((As a matter of fact the ''new'' message desribed above does nothing more but invoking this construct and the ''init'' method subsequently.)).
  
-{{:at:tutorial:isaversussharea.png|:at:tutorial:isaversussharea.png}}+<code> 
 +def clonedPoint := clonePoint 
 +> clonedPoint.
 +>> 0 
 +> clonedPoint.x := 2 
 +>> nil 
 +> Point.
 +>> 0 
 +</code>
  
 +===== Delegation and Cloning =====
  
-The following code shows how to extend objects with a **IS-A** relationship. It uses the ''extend: with:'' language construct.+In order to support code reuse and modular extensions between objects, AmbientTalk features //delegation// (also known as object-based inheritance). By means of delegation, an object can reuse and extend the fields and methods of another object by establishing so-called "parent-child" or "delegate-delegator" relationship. 
 + 
 +Delegation implies that, if a message is sent to an object, but that object has no definition for the message's selector, then the message is //delegated// to a designated object (often called the //parent// or //delegate//). What is important to note here is that "delegating" a message is not the same as simply "forwarding" the message to the other object: delegating a message leaves the ''self'' pseudovariable unchanged to the //original// receiver of the message. 
 + 
 +AmbientTalk distinguishes between **two kinds** of delegation relationships,**IS-A** and **SHARES-A**, each denoting a different kind of object extension. An **IS-A** delegation relationship between two objects signifies that the child object "is-a" kind of parent object, with the implicit assumption that such a child object cannot exist without its parentAs an example, consider the following code (don't worry about the meaning of ''^'' yet):
  
 <code> <code>
-def point3D := extend: point with: { +def Point3D := extend: Point with: { 
-    def z := 0; +  def z := 0; 
-    def sumOfSquares() { +  def sumOfSquares() { 
-      super^sumOfSquares() + z*z +    super^sumOfSquares() + z*z
-    }+
   }   }
 +}
 </code> </code>
 +
 +In this example, ''Point3D'' delegates
 +
 +These relationships define two different semantics for clonning child objects. Whereas clonning a **IS-A** child also clones its parent, **SHARE-A** child shares the parent of the cloned object (see the figure below).
 +
 +
 +{{:at:tutorial:isaversussharea.png|:at:tutorial:isaversussharea.png}}
 +
 +
 +The following code shows how to extend objects with a **IS-A** relationship. It uses the ''extend: with:'' language construct.
 +
 +
  
 The following code shows how to extend objects with a **SHARE-A** relationship. It uses the ''share: with:'' language construct. The following code shows how to extend objects with a **SHARE-A** relationship. It uses the ''share: with:'' language construct.
at/tutorial/objects.txt · Last modified: 2013/05/17 20:23 by tvcutsem