Dual methods

From Wikipedia, the free encyclopedia

A concept in the concept-oriented programming (CoP) provides two definitions for one method (for a method with the same signature) in its reference class and object class which are referred to as dual methods. If in the source code a method is applied to a reference then the definition provided by the reference class has precedence over that provided by the object class. We say that any reference intercepts accesses to the object it represents.

In the following example concept Account defines method getBalance in both the reference class and the object class:

   concept Account 
       reference {
           String accountNumber; // Identifier 
           double getBalance() { 
               print("> Account: access from outside");
               double b = 0; 
               if(accessPermitted()) b = .getBalance(); 
               print("< Account: access from outside");
               return b;
           } 
       }
       object {
           double balance; 
           double getBalance() { 
               print("> Account: access from inside");
               return balance; 
               print("< Account: access from inside");
           } 
       }

The reference method accepts requests from outside. If access is permitted then it simply delegates this request further to this object dual method. Thus if we apply this method to an account reference then the following output will be produced:

   Account account = getAccount(); 
   Account.getBalance(); 
   
   $ > Account: access from outside 
   $ > Account: access from inside 
   $ < Account: access from inside 
   $ < Account: access from outside 

However, if this method will be called from a child concept such as SavingsAccount in Account (from inside) then the reference method will not be executed. So reference methods are executed when entering this scope while object methods are executed when the process has already intersected the border and is inside the scope.

Dual methods have an important application in life-cycle management. In particular, the creation and deletion methods have two versions: one for initializing a reference and the other for initializing an object. The latter is an analogue of the conventional constructor while the former is a specific feature of CoP. In the following example the reference creation method initializes this reference and the object creation method initializes this object:

   concept Account 
       reference {
           String accountNumber; // Identifier 
           void create() { // Initialize reference 
               accountNumber = getUniqueAccountNumber(); 
               Object o.create(); 
               map.add(accountNumber, o); 
           } 
       }
       object {
           double balance; 
           void create() { // Initialize object 
               balance = 0; 
           } 
       }

The reference creation method generates a unique account number. Then it allocates real (native) resources for this object and gets its primitive reference. Finally it stores somewhere the association between this account number and this primitive reference so that the continuation method can resolve it during access.

In the case of concept inclusion hierarchy the creation and deletion methods should provide a possibility to their children to contribute to this process.

[edit] See also

[edit] External links

In other languages