Solved

OO Design - Object Accessibility

Posted on 2004-07-30
5
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

734 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