• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Can I pass the current class object into another class's method?

 
Ranch Hand
Posts: 278
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been stuck on an assignment for a while and decided to try and just tackle a suggestion made by Knute Snortum.  https://coderanch.com/t/711072/java/Suggestion-handling-expected-results-code on this post.

PersonMaintenance would be the class driving the process.  That is, it would have the start() method and that would call the printMenuoptions() from UserInterface (you will need an instance of the class unless you make the method static).  PersonMaintenance would have the enterMenuOption() method in it and so on.  So PersonMaintenance would have all the logic to "maintain" a Person (add a Person, search, etc.)  Then printing "not found" or "phone not found" would be calls to UserInterface.



I made the changes I think but currently my UserInterface has the method performCommand() which calls the PersonMaintenance to perform searches,  adds information and removes information.  

Previously
The way I had PRIOR to changing everything was UserInterface had the start() method , and that was called by the Main method.   I created a PersonMaintenance object in the UserInterface.  PersonMaintenance would create the Person object. .  From UserInterface I used the PersonMaintenance object to call the methods within PersonMaintenance.  I hope that makes sense.

Just to try and draw an explanation as to what I mean :  

UserInterface  {

      PersonMaintenance {
                                       Person {  }
                                   }
                      }



NOW

But with the suggestion to have PersonMaintenance drive , how can I get UserInterface to call a method on PersonMaintenance without creating a new PersonMaintenance object ?  Is there a way I can pass the current PersonMaintenance object over to UserInterface?










 
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lisa Austin wrote:
NOW
But with the suggestion to have PersonMaintenance drive , how can I get UserInterface to call a method on PersonMaintenance without creating a new PersonMaintenance object ?  Is there a way I can pass the current PersonMaintenance object over to UserInterface?


There is a way but I doubt that's a good idea. If PersonMaintenance knows about UserInterface, then it's not a good idea to have UserInterface know about PersonMaintenance. Rethink your idea about UserInterface accessing anything that has to do with PersonMaintenance. If you do that, you create a two-way dependency: PersonMaintenance is dependent on UserInterface and UserInterface is dependent on PersonMaintenance. That kind of design, one where you have circular dependencies, tends to create problems.
 
Lisa Austin
Ranch Hand
Posts: 278
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lisa Austin wrote:
NOW
But with the suggestion to have PersonMaintenance drive , how can I get UserInterface to call a method on PersonMaintenance without creating a new PersonMaintenance object ?  Is there a way I can pass the current PersonMaintenance object over to UserInterface?


There is a way but I doubt that's a good idea. If PersonMaintenance knows about UserInterface, then it's not a good idea to have UserInterface know about PersonMaintenance. Rethink your idea about UserInterface accessing anything that has to do with PersonMaintenance. If you do that, you create a two-way dependency: PersonMaintenance is dependent on UserInterface and UserInterface is dependent on PersonMaintenance. That kind of design, one where you have circular dependencies, tends to create problems.



Gotcha .  Okay.  From what Knute Snortum said, it does sound like the UserInterface was only supposed to be for things like response (i.e "address unknown" , "not found" ,"phone number not found" )   but the way the assignment has it's requirements , the response "phone number not found" is the only response that repeats so I am not sure it makes sense to just use it for responses.  

I'll keep thinking.  Thanks!
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lisa Austin wrote:I'll keep thinking.


That may not be enough though, unfortunately. What you're missing is context, so you can figure out what's a better way to go. I doubt you have enough experience to put the problems you have in the code into context though. This is the dilemma that beginners are caught in. Lucky for you, there are folks around here with plenty of experience in these sort of things.

Think about the following:

1. Code should be cohesive. That is, related ideas should be lumped together. It's hard to see connections between things when ideas are spread out and scattered throughout your program. Readability drops and maintenance cost goes up, as does the likelihood of introducing bugs.

Here's an example of a violation of this principle:

In PersonMaintenance:

In UserInterface:

If I were reading that line in PersonMaintenance, I might be wondering where "x" is specified (how do I know to check for "x" here rather than "Q" or "10", especially since the variable commandNumber suggests a numeric value rather than an alphabetic one?). More problematic is the fact that if I decided to change the menu option to "Q to quit", then I'd have to go to UserInterface to do that. However, I'd also need to go to PersonMaintenance and change the condition to check for "Q" instead of "x".  This should immediately trigger a warning bell in your head that you have a fragmented idea in your code that needs to be refactored so that it's more cohesive.

When you're trying to fix these kinds of issues, you need to look at higher levels of interaction, at the API boundaries of your objects, not at the nitty-gritty details. Look at how you have assigned responsibilities. Ask yourself whether "enter Menu option" is a concern that a driver class like PersonMaintenance should be responsible for. The behavior of the enterMenuOption() method is largely that of input/output (it displays a prompt and gets user input). Why is it not in UserInterface then?
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I've read through the thread that you cited, I see that it was Knute who suggested that enterMenuOption be in the PersonMaintenance class. That was an offhand suggestion though and Knute might have had something different in mind for that method from what you ended up writing. I would have questioned the thought though since the idea of "entering a menu option" is more closely related to a class named UserInterface than it is to one called PersonMaintenance.
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:More problematic is the fact that if I decided to change the menu option to "Q to quit", then I'd have to go to UserInterface to do that. However, I'd also need to go to PersonMaintenance and change the condition to check for "Q" instead of "x".


And in fact, in the other thread,

you wrote:The program is supposed to have 8 commands .  1 - 7 does different things, 8 is to quit.

 
Lisa Austin
Ranch Hand
Posts: 278
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Junilu Lacar wrote:More problematic is the fact that if I decided to change the menu option to "Q to quit", then I'd have to go to UserInterface to do that. However, I'd also need to go to PersonMaintenance and change the condition to check for "Q" instead of "x".


And in fact, in the other thread,

you wrote:The program is supposed to have 8 commands .  1 - 7 does different things, 8 is to quit.



Correct.  Originally I had both the printMenuOptions() method and the enterMenuOption() method on the UserInterface class.  The assignment wants the user to enter the value X to quit.  I apologize, I can see saying "8 is to quit" implies something different.  
I think I'm just going to keep the UserInterface the way it was prior to making the changes I was trying to do here .  I understand what you mean by Code should be cohesive and Thank You.

Would some of the issues you pointed out , where the printMenuOptions() method being on UserInterface and enterMenuOption() being on PersonMaintenance , been resolved by comments?   If I had added a comment on the UserInterface that the requirement is to use "X" and that the printMenuOption() method was on PersonMaintenance?

If I completely missed your point, please let me know.  

Thank You
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comments are not the solution to a structural/organization problem. That's like putting a sticky note on your refrigerator saying "Make sure to set the thermostat to 75 to make the microwave oven work."

In fact, comments like those you are suggesting would be considered a "code smell."
 
Lisa Austin
Ranch Hand
Posts: 278
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Comments are not the solution to a structural/organization problem. That's like putting a sticky note on your refrigerator saying "Make sure to set the thermostat to 75 to make the microwave oven work."

In fact, comments like those you are suggesting would be considered a "code smell."



Got it.  And , honestly I figured as much.  

Thank You for your time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!