By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Instantiating a read-only object from a factory method.

P: n/a
BBM
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

Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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).

BBM wrote:
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

Nov 16 '05 #2

P: n/a

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

--Richard

"BBM" wrote:
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

Nov 16 '05 #3

P: n/a
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
"BBM" <bb*@bbmcompany.com> a écrit dans le message de
news:90**********************************@microsof t.com...
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

Nov 16 '05 #4

P: n/a
BBM
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
"Mike Newton" wrote:
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).

BBM wrote:
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

Nov 16 '05 #5

P: n/a
BBM
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.

"Etienne Boucher" wrote:
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
"BBM" <bb*@bbmcompany.com> a écrit dans le message de
news:90**********************************@microsof t.com...
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


Nov 16 '05 #6

P: n/a
BBM
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

"BBM" wrote:
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

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.