1. Modelling references in CoP
In the concept-oriented programming (CoP) concepts are used instead of classes. A concept is a pair consisting of one reference class and one object class. Instances of the reference class are passed-by-value and are intended to represent instances of the object class. Thus objects and references always exist in pairs reflecting two sides of one and the same thing (identity and entity). For example, to describe bank accounts we could define concept Account:concept Account
reference {
String accNo;
...
}
object {
double balance;
...
} Now, if we define a variable of this concept, it will contain an instance of this concept reference class (account number). In contrast, in OOP it would contain a direct primitive reference to an object in memory.
References can be viewed as intermediate elements or proxies. They intercept any access to the object. For example, if we apply some method to a variable, say, account.getBalance(), then it will be executed in the reference and only after that its execution can be continued in the object. Thus CoP and concepts allow the programmer to effectively model custom references which provide a level of indirection but simulate normal references.
2. Modelling references by smart pointersThe same result could be reached using an old C++ pattern called smart pointers. The main goal of this technique consists in describing custom references by normal classes so that their instances then are used instead of direct references provided by the compiler. In C++ class instances can be passed-by-value and hence reference structure could be described by normal classes. (Notice that in Java it is not possible.) For example, account identifiers could be described as follows:
public class AccountRef {
String accNo;
...
} In order to create a new instance of account object, it is actually necessary to create two instances: one reference and one object. And hence it is necessary to provide two class names. However, to work properly, a reference class has to know about the object class. To pass this information, smart pointers use the mechanism of templates. Specifically, a reference class is defined as a template parameterized by the name of the objects class:
templateThus smart pointer class does not know its concrete object class. Rather, it has a parameter that may take any value. In other words, a smart pointer class can be used to represent any object and one object can be represented by many reference classes. This pairing is performed when a new variable is declared, i.e., for each new variable we provide two concrete class names: one for the reference class and one for the object class:public class AccountRef { String accNo; T& operator->() const { ... } // Access operator ... }
AccountRefThis variable will store an instance of AccountRef parameterized for Account. Any operation is then applied to this smart pointer which overloads access operator (dot or arrow). Using this smart pointer we can transparently call methods of the represented object, say, account.getBalance(). Here we see that both CoP and smart pointers allow the programmer to define custom references with arbitrary structure and behaviour which can be used as intermediate object representatives.account(new Account);
3. Comparison
Although these two approaches look very similar, they are actually quite different.- Smart pointers is a technique or pattern that can be used only if templates are supported and the language permits pass-by-value semantics for objects. On the other hand, CoP is a general purpose approach rather than a reference modelling technique.
- Concepts in CoP are intended to describe hierarchical virtual address spaces where objects will live while smart pointers play the role of interceptors for objects.
- Smart pointers still use normal classes for modelling references by adapting them for certain purpose. So it is an implementation of some logic of behaviour. In CoP, a new programming construct is used instead of classes which intrinsically supports the necessary functions rather than implements them by user-defined methods.
- Reference class and object class in smart pointers are defined separately and can be then paired almost arbitrarily for specifying what object class has to be represented by what reference class. In CoP, reference class and object class do not exist separately because they are parts of one construct. In this sense a reference class and object class are designed together from the very beginning and they cannot be freely paired.
- Smart pointers assume that reference class and object class are paired many times when each new variable is declared (the problem of duplicated code). In CoP, they are paired only once when the concept is defined while variables are declared using one concept name.
- It is difficult to implement layered (hierarchical) references using smart pointers. In CoP it is very easy and, moreover, CoP is designed to describe hierarchies using inclusion relation between concepts.
- In CoP, references can be reused by their child concepts. In other words, we can develop a concept which implements in its reference class some logic of indirect representation and then use it as a base concept for other concepts.
- Smart smart pointers need special operators in language to distinguish by-value and by-reference semantics (like star and ampersand in C++) because classes to not have this role. In CoP, the role of reference and object is built into concept.

<< Home