Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

OO Design - Object Accessibility

Posted on 2004-07-30
5
Medium Priority
?
202 Views
Last Modified: 2010-04-15
This is less a c# question, and more of an object oriented design question in general, but since I'm using c#, I'm posting it in here.

I am a self taught programmer (books, tinkering, etc) and I seem to be having the same problem over and over again when I design more complex apps.  Here's an example:

Say that there are three classes: Class A, B, and C.

Class A contains an instance of Class B, and Class B contains an instance of Class C.

However, both Class B and Class C need to access an object that's a member of Class A.

The only way i can think of to accomplish this, is to add an instance of the object to Class B and C, and pass the required object from Class A by reference.

A. Is there a better way to do this?
B. Is this problem a result of bad design?  And if so, how could the design be changed to avoid it?
0
Comment
Question by:jdraper3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:wolflad
ID: 11677112
A.

You are referring to classes and not instances of the classes so my suggestion would be inheritance:

Class A is the topclass. Class B inherits class A and class C inherits class B.

That way class C will have access to all methods & objects of class A & B, while B will have access to all methods & objects of class A.

B.

If A. is correct then it might be caused by bad design.
If class B is just class A with extra objects & method it should inherit A. Same goes for C to B.

If however class B is a huge class that utilizes a lot of other classes and A is just one of them (and a minor part), I would go for the way you described: Class B just uses class A.


-

If you are talking about instances of the classes however you might want to look into Singleton'ing some of the classes.

If you follow your own design, class C would be able to access class A, through class B if the instance of class A is public in class B.

Just my opinion.

-Wolf5
0
 
LVL 1

Author Comment

by:jdraper3
ID: 11680910
It's laid out so that Class A contains an array of instances of class B, which contain instances of Class C.

Class B and C both need to access objects that are part of Class A.

0
 
LVL 1

Accepted Solution

by:
wolflad earned 2000 total points
ID: 11681125
Let A and B keep its arrays of B and C. In the constructor of B and C make sure you take in a reference to class A.

Here is a working example of a way to do it:

      public class A{
            public string theObject = "HOHOHO";
            B[] arrb = null;

            public A(){
                  arrb = new B[10];
                  for(int x=0;x<10;x++){
                        arrb[x] = new B(this);
                  }
            }
      }

      public class B{
            private A myRefToA = null;

            C[] arrb = null;

            public B(A inst){
                  myRefToA = inst;
                  System.Diagnostics.Debug.WriteLine("The A object from B: " + myRefToA.theObject);
                  arrb = new C[10];
                  for(int x=0;x<10;x++){
                        arrb[x] = new C(inst);
                  }
            }
      }

      public class C{
            private A myRefToA = null;
            public C(A inst){
                  myRefToA = inst;
                  System.Diagnostics.Debug.WriteLine("The A object from C: " + myRefToA.theObject);
            }
      }

Of course if you made A as a Singleton class (only 1 instance) or static class, you'd have no reason to pass the reference to class B and C.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question