473,574 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiating a read-only object from a factory method.

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
6 2657
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

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
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******** *************** ***********@mic rosoft.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
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
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******** *************** ***********@mic rosoft.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3906
by: Nischal Topno | last post by:
Hi, Is there any way to auto load/instantiate a new class of a servlet into the JVM (with shutting the web server) after replacing the old class with new one? For e.g., after a servlet is deployed and loaded in the web server, a change needs to made to this server. Without shutting down the web server, can this new class with changes be...
3
1461
by: SammySAm | last post by:
I have a set of XSD's that I have to instantiate at run time and pass some values in C++.Net. How can I acheive it? Please advice. Best, Sam
2
1314
by: HarishP | last post by:
Hi, How to avoid instantiating the class more than 10 times Harish.P Sr. Software Engineer Comat Technologies Pvt. Ltd., Bangalore Email: harish.p@comat.com
2
1238
by: active | last post by:
Because of an example I followed I've been instantiating Image objects. Now I notice that the documentation says the Image class is an abstract base class. Things seem to be working! Is the documentation wrong (it also says abstract classes cannot be instantiated)? Should I be using Bitmap instead? Is there a difference between...
4
4131
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in the arithmetic operation' when I attempt to instantiate my first form, as so: Dim frm As New frmMyForm() Based on some things I saw here, I...
1
2186
by: Nita Rai | last post by:
I am getting 'error: too few template-parameter-lists' (GCC -3.4.2 ) for instantiating static data members of template class. When I prefixed template <> for the instantiation GCC accepted it.But linker is throwing erros saying undefined reference to the sysmbols.(where ever static members are refered) Does anyone know how to fix it?? ...
1
3230
by: Bruce | last post by:
I am getting the following exception in the release build of my assembly. {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} System.Exception {System.AccessViolationException} I do not have any problems in debug. The problem occurs while instantiating an unmanaged class. Any idea...
3
2066
by: kj7ny | last post by:
Is there a good example on instantiating, calling, using, etc., an API from Python? I need to use Python to talk to another system through that system's API's on Windows XP. Can you point me to documentation and/or a good example of Python using 3rd party API's? (Preferably a good example rather than just documentation.)
18
1851
by: RB | last post by:
Hi guys (and gals!), I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits from "TypesafeConstant", and adds no new functionality. All "Color" does is to instantiate some class variables which are public instances of "Color". What "TypesafeConstant" does is to create a shared list, and add every declaration of itself to...
4
2156
by: Peter | last post by:
Hi in some example code I have seen delegates instantiated in the following fashion: public delegate string UserName(int id); UserHelper helper = new UserHelper();
0
7805
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8234
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7815
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.