Professional Documents
Culture Documents
Chng 1. Mu
Class: i tng Lp hc
Subject: i tng Mn hc
Tha k
nh danh i tng (Inheritance)
(Object Identity)
ng gi a hnh
(Encapsulation) (Polymorphism)
/p trnh Hng
i tng
Ton vn /u tr
(Integrity) (Archive)
& s d liu Hng i tng
An ton Khi phc
(Security) & s d liu (Recover)
Giao tc ng b
(Transaction) (Concurrency)
Phin bn Truy vn
(Versioning) 7n ti lu (Query)
(Persistence)
char Gender;
char Address;
public:
// Khai bo cc hnh vi i tng:
void setStudentID (char SID);
void setName (char Name);
float getAverageMark ();
}
nh danh i tng
Tn i tng
quan hi tng. T kho interface c s dng thay cho cc t kho type v
class trong m hnh hng i tng ca ngn ng lp trnh, c s dng cho vic
miu t giao din ca cc lp i tng. Interface khng phi l mt th hin, khng
thc thi c, hay khng c i tng no c to ra cho interface, nhng c s
Gng phc v cho vic nh ngha cc thao tc ca i tng. Cc thao tc ny c
thc tha k bi cc i tng do ngi dng nh ngha trong cc ng dng c
th.
V d sau y minh ho vic nh ngha mt s giao din ca cc lp i
Wng v mt s cc hnh vi i tng:
interface Object{
boolean same_as (Object otherObject);
Object copy();
void delete();
2.3.3. Tp Cha
Cc kiu tp cha i tng trong ODMG c th l: Array (mng), Bag (ti),
Collection (tp), List (danh sch), Set (tp hp), Dictionary (t Ln). Trong ,
Collection l tp c s v chung nht, cc tp cn li c tha k t Collection. Tnh
cht ca cc tp cha c xc nh nh trong Hnh 2, v d: Array l mt tp cha c
th t, cho php phn t c gi tr Null (khng xc nh); Set l mt tp khng lu tr
th t, khng cho php lp cc phn t
9i mi tp cha, c cc phng thc cho vic duyt, truy vn v cp nht tp
cha. V d mt s phng thc c bn ca tp cha Collection l:
Collection::insert_element (e) // chn thm mt phn t e
Collection::remove_element (e) // loi b phn t e
Collection
/u gi C mt Kho i
th t Khng cng mi phn t
C
Khng
Cho php
C Cho php
Null
/p
C Khng
C Khng
float getAverageMark();
}
class Class
{
attribute char Name;
attribute Teacher HeadTeacher
attribute Student Monitor
attribute Student Secretary
class Class
(extent all_Classes
key Name)
^}
Class: Student
(Class)
Interface: Person
(Interface)
Relationships: 1:1
1:N
N:N
hasClasses hasTeachers
Department
memberOfDep Name
Person
belongToDep Address
Birthday
Class is_a is_a
hasStudents
Student Teacher
belongToClass
teachSubject
hasSubjects hasMarks
ofStudent hasTeachers
Mark Subject
ofMark hasMarks
teachedInClass
class Person
(extent all_Person)
{
attribute NameObj Name;
attribute BirthdayObj Birthday;
attribute char Gender;
attribute AddressObj Address;
attribute char Phone;
attribute char Email;
char getInfo();
}
float getAverageMark();
}
class Department
(extent all_Departments
key Name)
{
attribute char Name;
relationship Set<Class> hasClasses inverse Class::belongToDep
relationship Set<Teacher> hasTeachers
inverse Teacher::memberOfDep
}
class Class
(extent all_Classes
key Name)
{
attribute char Name;
attribute Teacher HeadTeacher
attribute StudentMonitor
attribute StudentSecretary
relationship Department belongToDep inverse Department::hasClasses
relationship Set<Student> hasStudents inverse Student::belongToClass
relationship Set<Subject> hasSubject inverse Subject::teachInClass
}
class Subject
(extent all_Subjects
key Name)
{
attribute NameObj Name;
attribute char Description;
attribute int Unit;
relationship Set<Mark> hasMark inverse Mark::ofSubject
relationship Set<Class> teachedInClass inverse Class::hasSubject
relationship Set<Teacher> hasTeachers inverse Teacher::teachSubject
}
class Mark
(extent all_Marks)
{
attribute int Mark1;
attribute int Mark2;
relationship Set<Mark> hasMark inverse Mark::ofSubject
relationship Set<Class> teachedInClass inverse Class::hasSubject
relationship Set<Teacher> hasTeachers inverse Teacher::teachSubject
}
VD: hnh trn minh ho qu trnh to lp Student, v thuc tnh kho chnh
StudentID vi kiu l char* (con trn kiu char).
0t thnh vin d liu c th c kho truy cp l Public, Private hay Protected
cho php cng khai, b mt hay c bo v i vi cc i tng khc. Kiu ca
thuc tnh c th l kiu nguyn thy (nh: kiu k t, kiu s, ), hoc c th l cc
kiu m rng ca ObjectStore s dng cho CSDL nh: os_database (kiu CSDL),
os_segment (kiu phn Rn), os_Array (mng), os_Bag (ti), os_Collection (tp),
os_List (danh sch), os_Set (tp hp), os_Dictionary (tLn), hoc cng c th l cc
kiu do ngi dng nh ngha (cc kiu d liu phc hp, cc kiu d liu i
Wng). Kiu ca mt thuc tnh cng c thc thit lp l con tr - Pointer, hoc
l mt Vector.
belongToClass;
os_relationship_m_1(Student,hasMarks,Mark,ofStudent,os_Set<Mark*>) hasMarks;
os_Set<Mark*> * get_hasMarks(){return &hasMarks.getvalue();};
};
// ObjectStore Relation
public:
os_relationship_m_m(Teacher,memberOfDep,Department,hasTeachers,
os_Set<Department*>) memberOfDep;
os_Set<Department*> * get_memberOfDep(){return &memberOfDep.getvalue();};
os_relationship_1_m(Teacher,teachSubject,Subject,hasTeachers,Subject*)
teachSubject;
void set_teachSubject(Subject* value){teachSubject = value;};
};
/p Person (Ngi):
NameObj Name;
BirthdayObj Birthday;
char * Gender;
char * Phone;
AddressObj Address;
char * Email;
// Operations
public:
char * getInfo();
};
// ObjectStore Relation
public:
os_relationship_m_1(Department,hasClasses,Class,belongToDep,
os_Set<Class*>) hasClasses;
os_Set<Class*> * get_hasClasses(){return &hasClasses.getvalue();};
os_relationship_m_m(Department,hasTeachers,Teacher,memberOfDep,
os_Set<Teacher*>) hasTeachers;
os_Set<Teacher*> * get_hasTeachers(){return &hasTeachers.getvalue();};
};
// extents
static void UpdateExtents(Class* pClass, int add, os_database* pdb = NULL);
static os_Set<Class*> *GetExtents(os_database* pdb);
static const char* GetObjectStoreClassName();
// Attributes
public:
char * Name;
Teacher * HeadTeacher;
Student * Monitor;
Student * Secretary;
// Operations
// ObjectStore Relation
public:
os_relationship_1_m(Class,belongToDep,Department,hasClasses,Department*)
belongToDep;
void set_belongToDep(Department* value){belongToDep = value;};
os_relationship_m_1(Class,hasStudents,Student,belongToClass,os_Set<Student*>)
hasStudents;
os_Set<Student*> * get_hasStudents(){return &hasStudents.getvalue();};
os_relationship_m_m(Class,hasSubjects,Subject,teachedInClass,os_Set<Subject*>)
hasSubjects;
os_Set<Subject*> * get_hasSubjects(){return &hasSubjects.getvalue();};
};
// ObjectStore Relation
public:
os_relationship_m_1(Subject,hasMarks,Mark,ofSubject,os_Set<Mark*>) hasMarks;
os_Set<Mark*> * get_hasMarks(){return &hasMarks.getvalue();};
os_relationship_m_m(Subject,teachedInClass,Class,hasSubjects,os_Set<Class*>)
teachedInClass;
os_Set<Class*> * get_teachedInClass(){return &teachedInClass.getvalue();};
os_relationship_m_1(Subject,hasTeachers,Teacher,teachSubject,os_Set<Teacher*>)
hasTeachers;
os_Set<Teacher*> * get_hasTeachers(){return &hasTeachers.getvalue();};
};
/p Mark (Lm):
// ObjectStore Relation
public:
os_relationship_1_m(Mark,ofStudent,Student,hasMarks,Student*) ofStudent;
void set_ofStudent(Student* value){ofStudent = value;};
os_relationship_1_m(Mark,ofSubject,Subject,hasMarks,Subject*) ofSubject;
void set_ofSubject(Subject* value){ofSubject = value;};
};
#include "Class.h"
#include "Mark.h"
os_rel_1_m_body(Student,belongToClass,Class,hasStudents)
os_rel_m_1_body(Student,hasMarks,Mark,ofStudent)
void Student::UpdateExtents
(Student* pStudent, int add, os_database* pdb /*= NULL*/)
{
Person::UpdateExtents(pStudent, add, pdb);
if (!pStudent && !pdb)
return;
try{
os_database *db;
if (pStudent)
db = os_database::of(pStudent);
else
db = pdb;
os_Set<Student*> *db_root_c_Student = GetExtents(db);
if(!db_root_c_Student)
return;
if (pStudent)
if (add)
db_root_c_Student->insert( pStudent);
else
db_root_c_Student->remove(pStudent);
}
catch (os_err& err){
puts(err.get_report());
}
}
Student::~Student()
{
if(StudentID){
delete StudentID;
}
UpdateExtents(this, FALSE);
}
char * Student::getInfo()
{
char * ret;
//TODO: Add your code here
return ret;
}
float Student::getAverageMark()
{
float AverageMark;
//TODO: Add your code here
return AverageMark;
}
Mark * m;
float sum=0.0;
int count =0;
// con tr c duyt trong tp hp
for (m=c.first(); c.more(); m = c.next()){
sum = sum + m->Mark1; // tnh tng cc Lm
4.1.3. To v m mt CSDL
m hay kt ni n mt CSDL, ObjectStore cung cp phng thc
OpenDatabase vi ba tham s chnh l: Name, ReadOnly, CreateMode. Gi tr tr v
Fa phng thc ny l mt CSDL v c thc gn cho mt bin CSDL. V d:
Set ObjectDB = ObjectStore.OpenDatabase(App.Path &
"\LearningManager.db", 0, 1)
Tham s CreateMode c dng to mi CSDL nu n khng tn ti, hay cng c
th to mi mt CSDL bng phng thc CreateDatabase vi cc tham s Name,
CreateMode, Overwrite:
Set ObjectDB = ObjectStore.CreateDatabase(App.Path &
"\LearningManager.db", 0, 1)
ObjectStore.BeginTrans
.Name.FirstName = OsStr(txtFirstName)
.Name.MidName = OsStr(txtMidName)
.Name.LastName = OsStr(txtLastName)
.Birthday.Day = OsNumber(txtDay)
.Birthday.Month = OsNumber(txtMonth)
.Birthday.Year = OsNumber(txtYear)
.Gender = OsStr(cboGender.Text)
.Address.DoorNo = OsStr(txtNo)
.Address.Street = OsStr(txtStreet)
.Address.City = OsStr(txtCity)
.Address.Province = OsStr(txtProvince)
.Address.Phone = OsStr(txtPhone)
.Address.Email = OsStr(txtEmail)
End With
ObjectStore.CommitTrans
Exit Sub
RB:
MsgBox Err.Description
ObjectStore.Rollback
End Sub
ObjectStore.BeginTrans
For Each aClass In CClass.GetExtents(ObjectDB)
If aClass.Name = Name Then
Set getClassHasName = aClass
Exit For
End If
Next
ObjectStore.CommitTrans
End Function
aStudent.getAverageMark()
aStudent.Birthday.getAge()
1u mt hnh vi khng cn cc tham s, ch cn gi tn hnh vi , nu hnh vi yu
Fu cc tham s, th cc tham s cn c truyn vo tng t nh cc li gi hm
trong ngn ng lp trnh.
Teacher2.memberOfDep.Add Dep2
Dep2.hasTeachers.Add Teacher1
4.2.4. Thit lp quan hi tng v kim tra tnh ton vn tham
chiu
V d, thit lp quan h hai chiu gia mt i tng Student v mt i
Wng Class, trong khung Relationship, nhp tn Name ca i tng Class vo
Belong To Class. Khi nu mt i tng Student c to mi, hu b, hoc cp
nht, th quan h cng s c thit lp tng ng. ng thi, chiu ngc li ca
quan h, Has Students Fa i tng Class cng c tng cp nht.
Hnh 29: Thit lp quan h hai chiu, kim tra chiu ngc li
0t v d khc kim tra tnh ton vn tham chiu trong CSDL khi thay i
gi tr kho chnh. Xt mt i tng Class, i tng ny c cc thnh vin d liu l
Head Teacher tham chiu n mt i tng Teacher v Monitor tham chiu n mt
i tng Student. Nu i TeacherID ca i tng Teacher, v StudentID ca i
Wng Student th cc gi tr ID tng ng ca Head Teacher v Monitor ca i tng
Class cng s tng c thay i tng ng, hay cc quan h vn c duy tr gia
cc i tng v khng ph thuc vo gi tr kho chnh.
Chng 5. Tng kt
Ph lc A
1. Form Student:
2. Form Teacher:
3. Form Class:
4. Form Subject:
5. Form Mark:
6. Form Department:
Ph lc B
9. Form Student:
If CheckExistedID(txtStudentID) Then
MsgBox "The Student ID has existed, Please use anothe ID"
Exit Sub
End If
ObjectStore.BeginTrans
.Name.FirstName = OsStr(txtFirstName)
.Name.MidName = OsStr(txtMidName)
.Name.LastName = OsStr(txtLastName)
.Birthday.Day = OsNumber(txtDay)
.Birthday.Month = OsNumber(txtMonth)
.Birthday.Year = OsNumber(txtYear)
.Gender = OsStr(cboGender.Text)
.Address.DoorNo = OsStr(txtNo)
.Address.Street = OsStr(txtStreet)
.Address.City = OsStr(txtCity)
.Address.Province = OsStr(txtProvince)
.Phone = OsStr(txtPhone)
.Email = OsStr(txtEmail)
End With
ObjectStore.CommitTrans
lstStudentRefresh
setSelectedList (aStudent.StudentID)
Exit Sub
RB:
MsgBox "Error: " & Err.Description
ObjectStore.Rollback
End Sub
ObjectStore.BeginTrans
'aStudent.belongToClass = Nothing
CStudent.GetExtents(ObjectDB).Remove aStudent
ObjectStore.CommitTrans
lstStudentRefresh
Exit Sub
RB:
MsgBox "Error: " & Err.Description
ObjectStore.Rollback
End Sub
End Sub
aForm.lblTitle = aStudent.StudentID
aForm.TreeRefresh aStudent.hasMarks
End Sub
On Error GoTo RB
ObjectStore.BeginTrans
With aStudent
.StudentID = OsStr(txtStudentID)
.Name.FirstName = OsStr(txtFirstName)
.Name.MidName = OsStr(txtMidName)
.Name.LastName = OsStr(txtLastName)
.Birthday.Day = OsNumber(txtDay)
.Birthday.Month = OsNumber(txtMonth)
.Birthday.Year = OsNumber(txtYear)
.Gender = OsStr(cboGender.Text)
.Address.DoorNo = OsStr(txtNo)
.Address.Street = OsStr(txtStreet)
.Address.City = OsStr(txtCity)
.Address.Province = OsStr(txtProvince)
.Phone = OsStr(txtPhone)
.Email = OsStr(txtEmail)
End With
ObjectStore.CommitTrans
lstStudentRefresh
setSelectedList (aStudent.StudentID)
Exit Sub
RB:
MsgBox "Error: " & Err.Description
ObjectStore.Rollback
End Sub
lstStudentRefresh
ObjectDB.SetUndoMarker
End Sub
End Sub
lstStudent.Clear
ObjectStore.BeginTrans
Dim aStudent As IStudent
For Each aStudent In CStudent.GetExtents(ObjectDB)
lstStudent.AddItem (aStudent.StudentID & ": " &
aStudent.Name.FirstName & " " & aStudent.Name.MidName & " " &
aStudent.Name.LastName)
Next
ObjectStore.Rollback
If lstStudent.ListCount = 0 Then
DisableBtn
Else
EnableBtn
lstStudent.ListIndex = 0
End If
loadStudentInfo getCurrentStudent
Exit Function
er:
MsgBox Err.Description
ObjectStore.Rollback
End Function
With aStudent
txtStudentID.Text = .StudentID
txtFirstName = .Name.FirstName
txtMidName = .Name.MidName
txtLastName = .Name.LastName
txtDay = .Birthday.Day
txtMonth = .Birthday.Month
txtYear = .Birthday.Year
cboGender.Text = .Gender
txtNo = .Address.DoorNo
txtStreet = .Address.Street
txtCity = .Address.City
txtProvince = .Address.Province
txtPhone = .Phone
txtEmail = .Email
cboClass.Text = ""
If .belongToClass Is Nothing Then
cboClass.Text = ""
Else
cboClass.Text = aStudent.belongToClass.Name
End If
End With
EX:
End Function
lstStudent.Selected(i) = True
Exit For
End If
Next
End Function
ObjectStore.BeginTrans
Dim aStudent As IStudent
End Function