Board index » delphi » observer pattern?

observer pattern?


2004-09-26 11:08:30 AM
delphi165
Hi,
I'm still new to this whole OO way of doing things...
If I want to save an object to disk, I call: TheObject.Save;
TheObject then passes it is properties to TheObjectStorage.Save; for the
actual saving
My question is how does the original call get notified if it was correctly
saved or not? And if not, it needs to know why it wasn't. The reason why
should come from TheObjectStorage. Is this an observer pattern, or chain of
responsibility? A small example would be very helpful.
Thanks in advance.
--
Rob Decker
news AT rdcomp DOT net
 
 

Re:observer pattern?

Quote
I'm still new to this whole OO way of doing things...

If I want to save an object to disk, I call: TheObject.Save;
TheObject then passes it is properties to TheObjectStorage.Save; for the
actual saving

My question is how does the original call get notified if it was correctly
saved or not? And if not, it needs to know why it wasn't. The reason why
should come from TheObjectStorage. Is this an observer pattern, or chain
of
responsibility? A small example would be very helpful.

It's usual to raise an exception if a low-level operation fails & trap it
higher-up where all the appropriate exceptions can be handled in an
appropriate manner, eg. by aborting the whole operation & logging the
exception.message.
try
myObject.save;
saveOK:=true;
except
on e:exception do
begin
saveOK:=false;
myLogger.writeString
('Object '+myObject.name+' save failed: '+e.message);
end;
end;
Any errors from the fileSystem will then appear in the log.
Rgds,
Martin
 

Re:observer pattern?

Quote
It's usual to raise an exception if a low-level operation fails & trap it
higher-up where all the appropriate exceptions can be handled in an
appropriate manner, eg. by aborting the whole operation & logging the
exception.message.
I guess I should have been more specific and used a better example. Instead
of TheObject.Save, let's look at TheObject.Load
The UI has a list of names that correspond to objects saved on disk. I
don't want to store all the objects in memory, but would fetch them one at a
time as necessary. When the user selects one of the names, then calls
TheObject.Load(TheName), the object with TheName should be retrieved from
disk (via TheObjectStorage) and loaded. But it is possible for the UI's
list of names to be out dated and the object may no longer be on disk. The
UI certainly shouldn't have to check the disk first. That should be the job
of TheObjectStorage. So if the object is no longer on disk, how does
TheObjectStorage let the UI know that it is no longer on disk?
--
Rob Decker
news AT rdcomp DOT net
 

Re:observer pattern?

Quote

I guess I should have been more specific and used a better example.
Instead
of TheObject.Save, let's look at TheObject.Load

The UI has a list of names that correspond to objects saved on disk.
OK
Quote
I
don't want to store all the objects in memory, but would fetch them one at
a
time as necessary.
OK
When the user selects one of the names, then calls
Quote
TheObject.Load(TheName), the object with TheName should be retrieved from
disk (via TheObjectStorage) and loaded.
OK
Quote
But it is possible for the UI's
list of names to be out dated and the object may no longer be on disk.
OK
Quote
The
UI certainly shouldn't have to check the disk first. That should be the
job
of TheObjectStorage. So if the object is no longer on disk, how does
TheObjectStorage let the UI know that it is no longer on disk?
I still do not see the problem with using exceptions for this, eg:
function MyUIform.loadAnObject(objectName:string):TloadableObject;
begin
try
result:=TloadableObject.load(objectName);
statusBar.simpleText:='Load of '+objectName'+' OK';
except
result:=nil;
on e:exception do
statusBar.simpleText:=
'Load of '+objectName'+' failed: '+e.message;
end;
end;
I'm sorry if I am missing something.
Rgds,
Martin
 

Re:observer pattern?

"Rob" <XXXX@XXXXX.COM>a écrit dans le message de news:
41563c12$XXXX@XXXXX.COM...
First let me agree with Martin about the general principle of raising an
exception if the Object Storage fails to complete the requested action.
Quote
I guess I should have been more specific and used a better example.
Instead
of TheObject.Save, let's look at TheObject.Load

The UI has a list of names that correspond to objects saved on disk. I
don't want to store all the objects in memory, but would fetch them one at
a
time as necessary. When the user selects one of the names, then calls
TheObject.Load(TheName), the object with TheName should be retrieved from
disk (via TheObjectStorage) and loaded. But it is possible for the UI's
list of names to be out dated and the object may no longer be on disk.
The
UI certainly shouldn't have to check the disk first. That should be the
job
of TheObjectStorage. So if the object is no longer on disk, how does
TheObjectStorage let the UI know that it is no longer on disk?
There are two scenarios to contemplate here:
1. You treat each retrieved list as a snapshot of the object storage at the
time of retrieval and work accordingly, raising an exception on any attempt
to store a non-existent/changed object.
2. You have an Object Broker that tracks all objects issued to users. The
Broker is an Observer to all objects issued and reacts to changes in any of
those objects by notifying any users that are registered as having access to
those same objects.
There are disadvantages to the latter approach in that users will be
subjected to data changing before their very eyes; not always a good thing.
You have to consider the particular use case and tailor your handling of
changes to each one. Sometimes 1 is better, other times 2 is better. :-)
Joanna
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker