Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!apollo!vinoski From: vinoski@apollo.HP.COM (Stephen Vinoski) Newsgroups: comp.object Subject: Pre-computing objects Message-ID: <4671be53.20b6d@apollo.HP.COM> Date: 25 Oct 89 20:23:00 GMT Sender: root@apollo.HP.COM Organization: Hewlett-Packard Apollo Division - Chelmsford, MA Lines: 38 Some time ago a discussion arose in comp.lang.c++ about reading objects in from, say, a disk file. There were several answers, but none that seemed all that great, so I hope to start a discussion of the subject here. The subject line of this article comes from Bertrand Meyer's "Object-oriented Software Construction" book, page 290. The problem he is solving on that page is how to create the appropriate object to fulfill a command within an editor program; the user enters the command, and the program must construct a command object to carry out the desired operation. He advocates associating an integer identifier with each command, pre-computing an array of command elements, and initializing the nth array element with a command object having integer identifier n. When the user enters an editor command, the integer identifier associated with that command is used to index into the array to obtain a command object which is copied and used to carry out the command. This problem seems pervasive; other examples are reading objects from disk storage and receiving objects from another process. Basically, how does one know what type of object to construct to receive the object unless the type of the blob coming in from the external source is already known? This appears to be one area of OOP that requires something like a switch or case statement to work properly. Doesn't Meyer's method trade the maintenance woes of a switch or case statement for having to maintain the initialization function for the command object array? It's a good tradeoff, but isn't there anything better? It also seems that his method is appropriate only for collections of objects which are somehow related. Does anyone out there know of any good solutions for this problem? Pointers to literature would be appreciated (I'm working in C++). -steve | Steve Vinoski | Hewlett-Packard Apollo Div. | ARPA: vinoski@apollo.com | | (508)256-6600 x5904 | Chelmsford, MA 01824 | UUCP: ...!apollo!vinoski | | "Yikes, not another OOP book using the tired old 'stack' class example!!!" |