Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Advice on how to structure co-dependent objects

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Advice on how to structure co-dependent objects

    Hi,

    I'm currently practicing my java by creating a basic android app using libgdx. Each screen has its own class. I wanted to improve code legibility, so I created additional classes which are called as objects to hold different functions. For example:

    public class Screen {
     
     ScreenA a;
     ScreenB b;
     ScreenC c;
     
     public Screen(){
      a = new ScreenA();
      b = new ScreenB();
      c = new ScreenC();
     }
     
     public void doStuff(){
      a.method();
      b.method();
      c.method();
     }
    }

    However, the methods in the additional classes usually depend on objects or methods stored in the other classes. To get this to work I had to add a reference to the other objects in each object's constructor. For example:

    public class Screen {
     
     ScreenA a;
     ScreenB b;
     ScreenC c;
     
     public Screen(){
      a = new ScreenA();
      b = new ScreenB(a);
      c = new ScreenC(a,b);
     }
     
     public void doStuff(){
      a.method();
      b.method();
      c.method();
     }
    }
     
    public class ScreenB {
     
     ScreenA a;
     
     public ScreenB(ScreenA a) {
      this.a = a;
     }
    }

    Although I'm not sure if this is a good way to go about it. e.g. what if ScreenA needs to reference ScreenB? Another way I could do it would be to have the methods reference the other objects themselves. For example:

    public class Screen {
     
     ScreenA a;
     ScreenB b;
     ScreenC c;
     
     public Screen(){
      a = new ScreenA();
      b = new ScreenB();
      c = new ScreenC();
     }
     
     public void doStuff(){
      a.method();
      b.method(a);
      c.method(a,b);
     }
    }

    I'm not sure what is best in terms of best practice. Is there a better way to go about doing this? Also how this would impact performance compared to having all the code within the class. I would love to hear what you guys think.

    Thanks in advance!

  2. #2
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Advice on how to structure co-dependent objects

    I'm on an android. The java concepts works the same on any device. Write once, run anywhere. Cross platform, as long as the network you're on uses a JVM and compiles files to read as .class files. This looks like a fine program.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Advice on how to structure co-dependent objects

    @planetHollywood

    The question asked was about objects where each object's behaviour might depend on the state of the other objects. And the OP asked whether it was better to pass the other objects as arguments in the constructor, or as arguments in the method call.

    How do either of your comments address this?

    @OP: I have just skimmed it, but there is a discussion of this at CodeRanch. As well as the constructor vs method question they consider a "configuration" class. Your Screen class might play this role.

Similar Threads

  1. Loop Structure
    By jasonkwp in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 23rd, 2017, 11:06 AM
  2. Structure
    By Gerardgrundy in forum Object Oriented Programming
    Replies: 8
    Last Post: November 3rd, 2012, 02:25 AM
  3. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  4. Data Structure
    By Faha in forum Object Oriented Programming
    Replies: 9
    Last Post: November 10th, 2011, 01:35 AM
  5. HI can some one tell me the size of the below structure.
    By sucheth13 in forum Java Native Interface
    Replies: 1
    Last Post: March 11th, 2011, 03:08 AM