Re: Managing multiple instances
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 11:24:22 +0200
On 28 Apr 2006 14:00:59 -0700, Benjamin M. Stocks wrote:
I'm working on an application where a number of what I will call core
objects are maintained within a single data structure. Each class that
must persist for the lifetime of the application inherits from the same
base class and then adds itself to a data structure of that base class
type.
When other parts of the application need to access a particular
instance of a specific object they request the object by a type
enumeration
= factory
and provide the instance number of that object that is
requested.
= identity
Multiple instances of the same object may be kept in the
data structure.
= bag (of class-wide objects)
I did not design this and don't believe in "all the
core application classes should inherit from X and they all should be
stored in the SAME data structure" but I have to work with it. So an
object is stored and retrieved according to a class identifier and the
instance number of objects with that class identifer.
= persistence
(no problem so far)
So here's the rub that I'm working on now: the instance numbers of the
different objects were assigned based on the order in which the objects
were added to the data structure. So if I add three instances of class
A the first instance added would get instance identifier 1, the next
would get 2 and the last would get 3. Much of the client code within
the same application was written to request a hard coded instance
number of a class when they needed access to that instance.
Identity should be a private type. It is in general a bad idea to assume
that it is a number.
As I'm sure you guessed by now changing the initialization order of the
product caused many of the instance numbers stored in the data
structure to change breaking the hard coded clients. As a first step to
improving this design I'm trying to replace the implicit assigning of
instance numbers with explicit instance numbers so changing the
initialization order will no longer break the clients.
Implementing persistency you should take care on dependencies between
objects. The base type of a persistent object should have a method: "get me
all objects you need." The objects from this set (not a bag) should be
stored / restored before the object. The dependencies form an order
relation, but to map it on object's IDs would be a bad idea.
And finally the issue I ran into: I have a class whose instances are
stored in this "one data structure to hold them all" (call it class A),
however this class is created within another class (call it class B) of
which multiple instances are created from within another class (class
C). IOW class C contains 2 instances of B each of which contain an
instance of A. So when creating instances of class A I cannot provide a
simple enumeration of the instance number when adding to the data
structure or the instance numbers will not be unique.
Yes, you should decouple IDs from dependencies. Note also that you probably
would need also external IDs, like DB keys. To manage all three as if they
were one is a nightmare, especially if the DB layer is decoupled (as it
should) and can be switched on the fly.
I'm thinking this data structure design is falling apart rapidly but
its so deeply integrated into the product redesigning it may not be
feasible right now. But I'm really stuck on how to create unique
instance numbers (that can then be referenced from within client code)
when the objects to be added to the data structure are created from
within objects that may also have multiple instances.
If you can Ada 95 you could take a look at persistence implementation at my
site:
http://www.dmitry-kazakov.de/ada/components.htm#persistent_objects
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- References:
- Managing multiple instances
- From: Benjamin M. Stocks
- Managing multiple instances
- Prev by Date: Re: Managing multiple instances
- Next by Date: Re: Question on LSP
- Previous by thread: Re: Managing multiple instances
- Next by thread: Re: Managing multiple instances
- Index(es):
Relevant Pages
|