Object-Oriented Views
4 Object-Oriented Views 59
- : Instance ... : Inheritance
Figure 4.3: Class hierarchies in the data model of C++
IN ADA provides the following expression in order to define views.
view ViewSet on BaseSet{
C41 for ah;
C42 for ah;
Cl.ik for aik;
where condition
};
where C41 ( l
==
1, 2, · · · , k) stands for an attribute of views and a11 ( l==
1, 2, · · · , k) means an attribute of base objects. The keyword view reveals that this is a view definition like as the keyword class in C++ and that the following description defines a class. C41 for aj1 ( l==
1, 2, · · · , k) means "the view has the attribute Cl-ip which corresponds to the attribute a11 in a base object." Both ai1 and aj1 ( l==
1, 2, · · · , k) can be 1ne1n ber variables and n1ethods in a base object, but aj1(l==
1, 2, · · ·, k) 1nust be public.This section discusses views like those which are defined with selection and projection in relational databases (hereafter, the two types of views are referred to as selection views and projection views, respectively). For in1ple1nen ting the selection and projection views, you have to take the following steps.
4 Object-Oriented Views 60 i) For projection views, you define first the C++ class View, which has attributes ~1 ,
~2 , • • · , ~k. Instance objects of this class are elen1ents of instance objects of the
class ViewSet defined in ii), and exist virtually. Instance objects of ViewSet construct a view of instance objects of BaseSet class, which is supposed to be defined already elsewhere.
ii) You define class ViewSet in INADA. The class is like that the type of its instance objects is Base in the case of selection views, or is View in the case of projection views.
Base class is supposed to be defined already elsewhere. In a method of ViewSet class the corresponding methods to those of BaseSet class are evaluated by means of the syntax 'DID as Type.' An interface in ViewSet as a selection view includes selection predicate(s) which appear in the condition part.
iii) You create an object of ViewSet class as an object of another class (or type) of a base object of BaseSet class. In other words, you create a multiple type object by adding type ViewSet to an object of BaseSet type. This multiple type object is a view of the base set object.
iv) A 'for all' statement which accesses the view is translated in to a 'for all' statement accessing the base set object. This can be done automatically, since both BaseSet and ViewSet have the same interfaces which set classes rnust have in IN ADA.
What users have to do with this approach to views is to create a set object as a view of a base set and to delete it when it is no longer needed after manipulating it. Although the class of elen1ents of a view set is defined in i), any object for this class is not actually created; therefore, there is no overhead regarding it. Let us consider the following example.
Example 1 Suppose that you must model the employees in a company. You model each employee using Employee class which has attributes of name, age, department number, and pay. And suppose that the set object, which is referred to by eset pointer variable, holds many objects of the class Employee.
Figure 4.4 shows a san1 ple code for this exan1 ple (for the sake of simplicity, detailed codes of methods and so forth are omitted). Set is irnplemented as a ternplate class. In the
1L tl·l
*- *
I*
$ ffi ¥fl. I*
~ ~4 Object-Oriented Views 61
constructor of Employee default values are set in all member variables, i.e., the default values are used if users do not put the value( s) when creating an object. Change _Dept () method is an update method to change an employee's department. In Figure 4.4 five persistent objects of Employee are created as elements of a set object of Set<Employee>. The persistent heap referred to by pho is mapped on EmpFile file. Figure 4.5 shows this conceptually.
The rest of this section presents son1e concrete views on the set in Exan1ple 1.
View 1 A view which allows users to get names, ages, and departments, and cannot retrieve pays from a set of employees. (A projection view)
For implementing this view, users can define Proview, for example, as follows:
view Proview on Set<Employee>{
char* Name() for Name();
int AGE() for Age();
int DeptNo() for DeptNo();
};
where char* Name() for Name() means "the view has the n1ethod named Name(), which returns a value whose type is char*, and the method corresponds to the Name() in its base set." It is interesting to note that na1nes of methods in a view need not to have the san1e names as the methods in its base class ( int AGE () for Age () in the example is an example).
Figure 4.6 shows the code translated fron1 the view definition. As the figure shows, the data of pays cannot be accessed from the class Proview_Employee. The class Pro view whose elements' type is the Proview_Employee is used to implen1ent the view.
View 2 A view which has only employees whose pays are equal to or more than two thousand.
(A selection view)
This view Sel view can, for exan1ple, be expressed as follows:
view Selview on Set<Employee>{
char* Name() for Name();
int Age() for Age();
};
int DeptNo() for DeptNo();
int Pay() for Pay();
where Pay() >= 2000;
4 Object-Oriented Views class Employee{
public:
char name[40];
int age;
int deptno;
private:
int pay;
public:
};
Employee(char* n="No-Name",int a=O,int d=-1,int p=-1) { strcpy(name, n);
age=a;
deptno=d;
pay= p;}
II
Constructor -Employee();II
Destructor char* Name(){ return name; } int Age(){ return age; }int DeptNo(){ return deptno; } int Pay(){ return pay; }
Change_Dept(int d){ deptno=d; }
II
Update template <class T>class Set{
public:
Set();
II
ConstructorT* GetElement(iterator<T>* i);
iterator<T>* Next(iterator<T>* i);
iterator<T>* OpenScan(iterator<T>* i);
void CloseScan(iterator<T>* i);
};
main(){
}
PHFT* phft =new PHFT();
PHD* pho =new PHO(phft, "ErnpFile");
persistent Employee *emp;
persistent Set<Employee>* eset=new(pho)Set<Employee>();
emp=new (eset) Employee ( "Ari 11,26, 4, 1000);
emp=new(eset)Ernployee("Kyu",50,2,3000);
emp=new(eset)Employee("Csc",34,4,1800);
emp=new(eset)Employee("Dat",28,1,1500);
emp=new(eset)Ernployee("Kum",55,3,3800);
Figure 4.4: Class definitions of example 1 and creation of instance objects
62
In this view definition, you can see that all n1em bers of the base class are specified. This means that an element of the view has the same members as those of the base class. In this case, the interpreted class in C++ is the sa1ne as the base class, i.e., the interpreted class is unnecessary. To avoid this, the following definition can be used: