Solved

OO Design - Object Accessibility

Posted on 2004-07-30
5
191 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now