Instantiating a read-only object from a factory method.

G

Guest

I have an object that has a fairly complex construction sequence, so I have
written a dedicated "factory" class that invokes the constructor of my object
class (which does nothing but instantiate the object and set default
blank/null values), and then does all the Db access and number crunching to
populate the new object. The factory returns the fully populated object to
the caller. All the fields in the object are private, but have public
properties with both Get and Set methods. If the Set methods weren't public,
the factory couldn't populate them.

Now I would like my factory class to create either a Read-Only or a
Read/Write version of my object (they're all Read/Write today). The object
is remotable and I want to be able to control whether the object is
updatable.

Any ideas?

I thought of this...

1) Make a version of the class where all the public properties have only
"Get" code associated with them (i.e. all properties of created objects can
only be read). Make all the properties in this class "virtual".
2) Subclass the "read only" class and overrride the properties to contain
both Get and Set code. The Get methods just reference base.Get so there
isn't any code duplication (haven't tried this don't know if this is the
right syntax). The Set code updates the appropriate private field data.
3) Write a "CopyToReadOnly" method in the subclass that a) instantiates a
"read only" object (i.e. call the default constructor on the superclass), b)
copies all the values from the instance fields in the read/write object
directly to the same fields in the new "read-only" object c) returns the
"read only" version of the original "read/write" object. So to get a
"read-only" object I a) tell the factory to make a read/write object, b)
tell that object to make a "read-only" copy of itself, and c) return the
"read-only" version to the caller.

This seems much too complex, and I don't like the performance hit of having
to instantiate the object twice, once as read/write and again as read-only.

Somebody must know how to do this better.

Thanks.

BBM
 
M

Mike Newton

Why don't you just put all of that stuff in the factory class in a
constructor? You can declare a few constructors, one of which would
instantiate blankness, but you can make a static constructor that would
work like your factory class, but giving you access to all child
variables within.

It seems that you have a good idea for declaring a virtual class, but
I'd do it the other way. Make the parent read/write, and the child
classes read-only, just don't expose the set{} constructs.

Lastly, you should be able to cast between the two without having to
clone the object (if you've used inheritance).
 
G

Guest

Now you know why Delphi uses 2 seperate functions for read & write
properties... ;)

--Richard
 
E

Etienne Boucher

If you really want to have a seperate factory object, maybe it could be a
nested class. Also, the construction could be done by member static methods.

Are you making the object readonly by using a flag, or through inheritance?
Using a factory model makes easier to do it with inherientance so I guess
that's why you constructing your object that way.

Etienne Boucher
 
G

Guest

Mike:

Thanks for your response.

1) The constructor is quite complex, and deals with access to multiple
databases. Also, because of the layering of the system, it's likely to be in
a different component than the main object.

2) I like your idea about just casting the object to the read only type.
Certainly simpler from a code standpoint.

3) I'll think about switching the main / subclass setup. What advantage do
you think that making the read/write class the parent class has?

Thanks again for your help.

BBM
 
G

Guest

Etienne:

Thanks for your response:

1) I think I need the code for the constructor separate from the main
object code. The app is client server, and the factory object is likely to
be in a different component.

2) I thought of using a flag to secure the object, but ran into the same
dilemma. For the factory to set it, it has to be public. Once it's public,
its not as "safe".

Thanks again.
 
G

Guest

Thanks to all the responders. I'm pretty new to C# and was afraid that I was
really off base with my whole approach to this problem.

BBM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top