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 5 of 5

Thread: Caller passing itself to child object?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Caller passing itself to child object?

    I ran into a code pattern today that I know isn't right and I know what to do about it. What I don't know is the best way to document for the original developer just why it needs to be changed. Obviously he thought it was a good design when he did it so I want to be prepared to respond with better than, "because I said" or "just because" when I discuss it.

    I have two classes, for example, ClassA and ClassB. ClassA has a ClassB. ClassA passes a reference to itself into ClassB and calls the configure() method in its ClassB instance.
    public class ClassA {
    ClassB instance = new ClassB(this);
    private String field1;
    private String field2;
    private String field3;
     
    public void main(String[] args) {
    instance.configure(classAData);
    }
     
    public void setField1(String value) {
    field1 = value;
    }
     
    public void setField2(String value) {
    field2 = value;
    }
     
    public void setField2(String value) {
    field2 = value;
    }
     
    }
     
    public class ClassB {
    public ClassB(ClassA classA) {
    this.classA = classA; 
    }
     
    ClassA classA;
     
    public void configure(String[] classAData) {
    classA.setField1(classAData[0]);
    classA.setField2(classAData[1]);
    classA.setField3(classAData[2]);
    }
    }
    I see no reason to even have ClassB. In my view, ClassB knowing about its caller, ClassA, is definitely a bad smell in code. Since ClassB.configure only operates on ClassA objects, it really wants to be in ClassA.

    One option is to move Field1, Field2, and Field3 into ClassB and let it be a data holder object for ClassA, if that fits the model. Another option is to eliminate ClassB all together. This is going to be my initial preference since the fields are used extensively in ClassA. As I refactor more I may find that I want a data object for ClassA but I don't see the point yet. In my real code, ClassA is a pretty extensive bit of code strewn with spaghetti and I'm trying to clean it up so I can make what is, as it is, a very difficult addition to the code. I am trying to refactor it to make it more object-oriented so that my change can be done in 30 minutes instead of a couple days - and then future changes will also be minutes instead of days.

    What rules am I forgetting or do I need to research about children knowing about parent? It's clearly not loosely-coupled but I'd like to find documented theories or practices around this if I can. Any suggestions?

    Thanks,

    Dale


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Caller passing itself to child object?

    Is this the actual code? There are many many problems..surely this code is not in use

    Assuming some of the problems are no more than typos/oversights in a quick example rather than the actual code in question, this type of design provides a layer of security between ClassA's members and ClassB's access to them. In the sample given ClassB can not get the values of the fields, just set them. Not that such a thing is necessarily useful, that would depend on the actual code and what is being done.
    Would need more info/clarification on the specifics, but it looks like it could be cleaned up or minimized to one class with what is given so far

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Caller passing itself to child object?

    Thanks for the reply.

    THis is just sample code; I would have to jump through a ton of hoops to get permission to post actual code. The issue here is not so much the question of security to the fields; or not a question of using the getter or setter.

    Once ClassB sets the values in ClassA, ClassB is never referred to again in code. It appears to be a very-well-intentioned attempt at keeping classes small and focused where someone extracted some small set of behavior into its own class. I just don't think this gives the results that might be intended (I will find out more about the intentions in a later discussion with the developer).

    I'm a big fan of extracting data classes and in ClassA there are long lists of related fields with which I can do that. There are also objects I will extract with inheritance to eliminate a bunch of case or if-else statements. Overall, I see a bunch of good intention in the code but perhaps falling short in execution.

    So this code should demonstrate the principle, I hope. The question is about the owned object knowing details about its owner. It certainly means it can only have one owner type, ClassA, or owners that also own or know about ClassA objects to pass in to ClassB.

    I hope this helps clear up what I'm asking about.

    Thanks,

    Dale

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Caller passing itself to child object?

    It sounds like you already have it figured out but are not confident with your findings.
    If there is no reason to have ClassB, then that is the answer. Nothing seems clearer except that you have a solution you are hesitating with.
    I know this post is not much more help than the last one, but there is not much to go on, except your word that ClassB is not needed, so get rid of it and see how it goes
    I know its hard to ask questions without getting thrown into the fire on something like this, good luck with it.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Caller passing itself to child object?

    Thanks. I'm pretty confident in my finding; I was just looking for documentation to support it.

    I don't mind being thrown into the fire; I'm a big boy . I know it's hard to answer without clear code sometimes.

    Thanks for the help.

    Dale

Similar Threads

  1. Passing a parameter of current object
    By popnfresh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2013, 05:11 PM
  2. Passing as a parameter, an object + another variable
    By goochasauras in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2012, 10:13 PM
  3. Problem in passing an object class through socket
    By aladin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2012, 07:34 PM
  4. passing object and storing
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 30th, 2011, 09:32 PM
  5. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM