OOP Address Book

An Example on OOP




Welcome

Hi! Now, I'm going to write some more example on building application using OOP methodology. I hope that you have followed chapter 1 to 4 earlier. This chapter uses the linked list component that we've built last chapter. You'd probably want to read it to refresh your memory. I understand that OOP, if it is not explained in depth, will confuse a lot of people. Numerous examples are needed to clarify the point. I will be more elaborate this time.

For OOP learning purpose, I decided to make a simple but usable application. It is simple enough so that you can follow, but not overly complex. The choice fall into building a simple address book. Remember the ones that you store in your pocket? Yes, that's it. We just want to have the address book to store only names, addresses, and phone numbers. Of course, you can extend it later if you want to.

 

Data Structure

OK, the objective is set: To build an address book. First, we would think what fields need to be stored inside an address book. Well, I've quoted that earlier: a name, an address, and a phone number. So, our data structure looks like this:

type
   PData = ^TData
   TData = record
              name    : string[25];
              address : string[40];
              phone   : string[15];
           end;

Hmm... That's pretty straight forward. Now, the next step is: What would you like to do with this data structure? You can think of several actions that can be done to it:

Hmm... That's pretty much like the linked list operation, isn't it? Well, of course, save and load have to be added. Wait a minute... Why don't we program those actions into methods directly by converting TData into an object? You can do it. Since we have already build a linked list in the previous chapter, we'd use it rather than rebuilding from scratch. Moreover, you will need a linked list anyway to store your data inside the memory.

Now, let's drag up our TMyLinkedList built last chapter. Yes, I didn't give you the complete implementation last time. I assume that you yourself completed it. It's pretty straight forward anyway. So, the whole gang looks like this:

type
   PMyLinkedList = ^TMyLinkedList;
   TMyLinkedList = object(TMyObject)
                      private
                         data : TMyObject;
                         next : PMyLinkedList;
                      public
                         constructor init; virtual;
                         procedure Add(theData: TMyObject);
                         function  Find(theData: TMyData): PMyLinkedList;
                         procedure Insert(theData: PMyLinkedList);
                         procedure Delete(theData: TMyData);
                         destructor Done;
                         { Accessor }
                         procedure SetNext(n: PMyLinkedList);
                         function  GetNext: PMyLinkedList;
                         procedure SetData(d: TMyObject);
                         function  GetData: TMyObject;
                         function equals(o : TMyObject): boolean;
                   end;

Of course don't forget to copy the TMyObject too. OK, what's next then?

Since we're trying to build an application using OOP methodology, why don't we build a class for our main application?

type
   TMyApplication = object
                    end;

Aha. It's pretty simple.

 

Closing

OK, I think that's all for now. See you next time.

 


Where to go

Chapter 7
News Page
Pascal Lesson 3 index
Contacting Me


Roby Joehanes © 2001