473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Confused - Static member object whose class design assumes multipleinstances.

I cant seem to get my head wrapped around this issue, I have myself so
twisted now there maybe no issue!

Ok so I designed a class X that has a few members, and for arguments
sake one of the members Y is the location of a file to be read. The
original design assumes that this class will be instantiated and each
instance will happily mange its own members. (ie One file location
per instance...no thread-safety).

Now another class A comes along and makes an instance of this class as
a static readonly member B. Do I now have "implied staticness" on the
original class design? Ok, I cant change the actual pointer to the
instance of Class X; however, through class methods I can change the
make-up of this object. In my case I can use methods in class X to
change the file to be read.

If all of this is true so far, I can now change the file to be read so
that all instances of A must read the same configuration file. Per
the original design of class X, the class invariant is ok. However,
with "implied staticness" I have a potential issue in its use.

So this is the Pandora's box I have been forced to use. I am forced
to create this object as static due to a static method
signature...still dont fully understand that architecture yet.

-Is "implied staticness" true?
-Does this force a refactoring of class X to account for static usage/
thread safety/static based invariants?
*Im implied to say no and these questions are directed towards my
design of class A
-Are there better patterns or best practices I should use to vet my
class design?
-Should I reconsider the design of class A for static usage/thread
safety/staticbased invariants?
*In this case I guess I would have to include additional overhead to
check certain settings before resetting.
*Could create a factory...hmmm thats probably really what should be
done anyways..huh?

Thanks in advance for reading my rumblings,

-Pete
Jun 27 '08 #1
5 1714
On Mon, 21 Apr 2008 20:46:25 -0700, <pg********@gmail.comwrote:
I cant seem to get my head wrapped around this issue, I have myself so
twisted now there maybe no issue!

Ok so I designed a class X that has a few members, and for arguments
sake one of the members Y is the location of a file to be read. The
original design assumes that this class will be instantiated and each
instance will happily mange its own members. (ie One file location
per instance...no thread-safety).

Now another class A comes along and makes an instance of this class as
a static readonly member B. Do I now have "implied staticness" on the
original class design?
No, you don't. You have a static member in class A that refers to one of
potentially many instances of class X.
Ok, I cant change the actual pointer to the
instance of Class X;
What pointer? Do you mean reference? Why can't you change it? Is A.B
readonly? And if it is, why does that matter with respect to other
instances of X?
however, through class methods I can change the
make-up of this object. In my case I can use methods in class X to
change the file to be read.
If you modify the instance of X referenced by A.B, then yes...that
instance is affected. No other instance of X would be affected though.
If all of this is true so far, I can now change the file to be read so
that all instances of A must read the same configuration file.
Absolutely not.
Per
the original design of class X, the class invariant is ok. However,
with "implied staticness" I have a potential issue in its use.
There's no "implied staticness".
So this is the Pandora's box I have been forced to use. I am forced
to create this object as static due to a static method
signature...still dont fully understand that architecture yet.
You cannot create an object "as static". By definition, an instance isn't
static. It might be referred to by something else that is static, but the
instance itself would not be static.
-Is "implied staticness" true?
No.
-Does this force a refactoring of class X to account for static usage/
thread safety/static based invariants?
Static and thread-safety are orthogonal. They have nothing to do with
each other, except inasmuch as a class that's not thread-safe can be used
safely by threads by having any given instance known and/or used by only
one thread at a time.

Regardless, since there's no "implied staticness", obviously there's no
forced "anything" as a consequence of "implied staticness".
*Im implied to say no and these questions are directed towards my
design of class A
-Are there better patterns or best practices I should use to vet my
class design?
Impossible to say, as we still know basically nothing about your class.
-Should I reconsider the design of class A for static usage/thread
safety/staticbased invariants?
Do you need your class to be thread-safe?
*In this case I guess I would have to include additional overhead to
check certain settings before resetting.
*Could create a factory...hmmm thats probably really what should be
done anyways..huh?
Why? I mean, I suppose a factory might be appropriate. But there's no
way to know for sure, since you didn't tell us anything about your classes.

Pete
Jun 27 '08 #2
Right, I understand. Thats exactly why I made A.B readonly, for
arguments sake, so that the static reference will only refer to one
and only one instance of class X. All instances of class A will use B
(single instance of X). To make it easier lets throw some code out
here for examination:

public class X
{
private string _fileName = null;
public string FileName
{
get { return _fileName; }
set { if(String.IsNullOrEmpty(value) { throw new
Exception("FileName cannot be null or empty"); }
else { _fileName = value; }
}
}

public string ReadFile() { ...read content and return some piece
of content }
}

public class A
{
private readonly static X B = new X();

public void SetFile(string fileName) {
A.B.FileName = fileName;
}

public void ManageFile()
{
string temp = A.B.ReadFile();
//Perform some actions
}

}

class RunMe
{
static void Main(string[] args)
{
A instance1 = new A();
A instance2 = new A();

instance1.SetFile("C:\\temp1.txt");
instance2.SetFile("C:\\temp2.txt");
instance1.ManageFile(); //Being performed on C:
\temp2.txt not C:\temp1.txt
instance2.ManageFile(); //Being performed on C:
\temp2.txt as expected.
}
}

In this scenario there is a single instance of class X tied to static
member B in class A. Multiple instances of class A can only interact
with that one single instance of class X. In this scenario I could
make class X a static class and the behavior would be similiar.

Jun 27 '08 #3
n!
Multiple instances of class A can only interact
with that one single instance of class X. In this scenario I could
make class X a static class and the behavior would be similiar.
Well, if you made class X a static class then it would be accessible from
everywhere not just from class A. Meaning the security of X's state is not
secure and nor is the operation of class A.

It also means no other class could use X's interface without interfering
with class A (or vice-versa)

I would personally recommend that, 99% of the time, static classes not
contain any state, simply constants/readonly and functions.

ave
Jun 27 '08 #4
<pg********@gmail.comwrote:
Right, I understand. Thats exactly why I made A.B readonly, for
arguments sake, so that the static reference will only refer to one
and only one instance of class X. All instances of class A will use B
(single instance of X). To make it easier lets throw some code out
here for examination:
<snip>
In this scenario there is a single instance of class X tied to static
member B in class A. Multiple instances of class A can only interact
with that one single instance of class X. In this scenario I could
make class X a static class and the behavior would be similiar.
Only if A is the only consumer of X. In real life you'll often find
that you only need one instance of something for one particular case,
but other things need multiple instances.

As an example, I sometimes have a static variable of type
Dictionary<X,Yfor some useful types X and Y. Just because I have a
static variable doesn't mean that Dictionary should be a static class -
elsewhere in the code I'm likely to use Dictionary in completely
different ways.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #5
On Tue, 22 Apr 2008 06:05:51 -0700, <pg********@gmail.comwrote:
[...]
In this scenario there is a single instance of class X tied to static
member B in class A. Multiple instances of class A can only interact
with that one single instance of class X. In this scenario I could
make class X a static class and the behavior would be similiar.
Similar, but not identical. In particular, you haven't done anything here
that introduces "staticness" to class X. It's not even true that multiple
instances of class A "can only interact with that one single instance of
class X". It's true that if they use the B member, they will only ever
get that one instance. But any instance of A is free to create a new
instance of X and use that instead. And any other code, even outside
class A, can create new instances of class X.

I repeat: nothing about your example has introduced any sort of
"staticness" to class X.

Pete
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
0
by: ma740988 | last post by:
I've provided a stripped down version - as best I could - to get us by. That said, I'm in a quandry with respect to a design here. Consider: // stream.h #ifndef STREAM_H #define STREAM_H #...
5
by: ma740988 | last post by:
Select parameters in a vendors API file is as follows: #ifdef __cplusplus typedef void (*VOIDFUNCPTR)(...); /* ptr to function returning void */ #else typedef void (*VOIDFUNCPTR)(); ...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Gary Wessle | last post by:
Hi I am calling a class method on many objects of that class alternately. the class needs to make available "remember" values of variables of said method for each object separetly when the...
2
by: Markus Dehmann | last post by:
I need a simple object serialization, where loading an object from file looks like this: Foo* foo1 = FooFactory::create("./saved/foo1.a321f23d"); Foo* foo2 =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.