+ 3

Refactoring existing methods to their own classes

Heya, back with more java questions. No example code needed, I need advice honestly. For you to understand, let me briefly explain what I'm assigned to do. If you wanna just get down to my question, skip to the bottom: My assignment asks me to refactor an existing set of code of a Hospital system. The new system is to have the tasks be their own class (commands) extended by a super that checks the status of them, and also handles their exceptions. Currently, the Hospital class used to handle all input and output calls, including storage of objects such as doctors and patients. I've since restructured it so that all I/O is assigned to an consoleIO class. However ... the issue I have is when giving each method its own class under the command package, calling them more than once is a problem, especially when one method depends upon another to acquire information. The class in question assigns a patient to a doctor, but that calls upon the patientData class. The IDE gives me a warning that the compiler will expect a method call, not a class call. So I'm unsure what method of approach I should use when I'm required to make these tasks a class of their own, and be called more than once. Maybe the best way I can ask this question is: When converting a method to its own class, how should I organize it? Should or shouldn't the constructor do everything? How should I approach calling and recalling the class? If I'm glossing over the obvious, please do point it out. Or, if I'm still too vague, let me know. Thanks in advance!

12th Nov 2017, 12:13 AM
Sapphire
7 odpowiedzi
+ 1
I have a feeling you are close to resolving this and it's a simple issue that is being overlooked. However, some of the details in your description are a bit difficult to follow. Is it possible to review your code in CodePlayground - even if it doesn't run. Being able to review the code would be very helpful. In the meantime, please respond to my questions below and confirm if I'm following you correctly. 1. Is the purpose of this refactoring exercise to extract common functionality found in multiple classes to a separate class? In your case, it sounds like you are extracting I/O functionality from different classes and placing them into a new class called ConsoleIO. 2. Are you creating a separate class for each method? Without seeing your code, I'm not sure this structure makes sense. I have about 5 different scenarios playing out in my head as to why you might be doing this. However, it would be much easier to see the code and go from there. 3. What do you mean when you say calling the new methods more than once is a problem? Does the method work as expected on your first call? What problem occurs when you call the method a second time? 4. Did the methods you refactored to a new class have access to private or protected members of the original class? If so, how did you refactor the methods to access those values? [UPDATE: New Question] 5. Are the refactored methods being moved to a new package outside of the original package? If so, were the refactored methods being moved from a single package or from multiple packages? Apologies for answer your question with more questions. I just want to make sure I respond with an answer that relates to your specific issue.
12th Nov 2017, 1:26 AM
David Carroll
David Carroll - avatar
+ 1
unfortunately it would be innaproperiate for me to post my CMPT assignment code online. I can post bits and pieces and talk about each one. I'll answer your questions, and try to clarify a bit: 1. Yes and no. The assignment has a bit of a few things. We're taking tasks that were originally methods of hospital class, and making them into command subclasses of a command status in the commands package. Since the methods heavily relied on the hospital class for the private containers that held the list of patients and doctors who are at the hospital, the commands must somehow retrieve that information from the hospital class. The hint we're given is to somehow use 'singletons'. 2. Taking each 'task' method that does something in the hospital, and converting them into classes. So basically: when you start the program, the hospital class creates a ward, and has zero patients/doctors. The user is prompted with a list of possible commands on the console. When they enter a command, it let's say adds a patient to the system. 3. Well the commands are classes, which create a new object class when called to do something. But some of those same commands need the help of others, and the IDE gives a warning when trying to call another class within the constructor that itself must somehow get the patient container. 4. My first approach was to pass the container as arguments to the command. The command makes the additions, and no returns are needed. I'm sure there's another way, but for the life of me I cannot think of the answer. 5. There are multiple packages: commands, user interface, startup, containers, and entities. The tasks were given a class and placed into the commands package. hospital is located in startup.
12th Nov 2017, 2:38 AM
Sapphire
+ 1
@David Carrol: That approach is common. You use it to track individual orders,make them reversible etc. See Command Pattern. @Saphhire: You have .execute() on the commands? You can make it .execute(Hospital hospital) You can put the commands in a subpackage of where the hospital is and make the hospital's method package, ensuring commands are used.
12th Nov 2017, 3:12 AM
1of3
1of3 - avatar
+ 1
Hi Sapphire - I've attempted to sketch a UML class diagram from your descriptions and am struggling with interpreting the context of some of your details. Unfortunately, without looking at the code structure, this type of diagnostics requires me to make a lot of assumptions and speculations on how you are structuring the code. I would be very interested in helping you with this. However, this may not be the best forum to provide that help. One option to consider is for you to join the Slack Team called SoloLearn Dev Connection (created by Shane Overby). We would then complete this review in a more real time chat with better code snippet support and the ability to add screenshots, etc. If you are interested, let me know when you would like to meet up and I can spend 30 minutes or so to help get you through this issue. To join that Slack Team, just follow the instructions in this link on SoloLearn: - https://www.sololearn.com/Discuss/754401/let-s-chat Otherwise, I do wish you the best of luck.
12th Nov 2017, 6:00 PM
David Carroll
David Carroll - avatar
+ 1
@David Hi, just an update and to lessen the confusion, I've created a python code on the playground that lists the java source and its classes. I figured it'd be easier than typing and imagining it in your head. I would also quickly write up their dependencies or draw a UML, but I'm extremely short on time as I'm stilling modifying the code. I've (as you've said) made progress over night. Basically what I've done is turn IOHandle, all the containers, and all the commands into singleton's. I then added a method "run()", which executes the class internally. IOHandle acts as an abstract class for both dialog and console. Depending on which environment the user selected, IOHandle calls the appropriate class to complete the Input/Output. This way, all classes in the program all point to IOHandle, while IOHandle decides on which method to use. I also discovered over night that I must have a getInstance() method in every singleton. This might be obvious to others, but I'm new to the concept. I figured once it was called, all its static methods could be used. I still don't quite understand singletons that well, but so far I've made the angry red text go away. heh. https://code.sololearn.com/c3f7igC53VmN
12th Nov 2017, 7:02 PM
Sapphire
+ 1
Thanks for the update. It looks like you've made a lot of progress indeed. Also... great job on really pushing through on your assignment. I'm really impressed with your work ethic. There are quite a bit of moving parts when building applications with design patterns. I've included a link you might find interesting to review. For me personally, I've experienced challenges with Command Patterns when introducing singleton Command Objects to create global state. That may not be what you are doing. However, in case it is, I thought I'd share that CQRS is another really good architecture (not a GoF design pattern) that has proven to be much better for larger applications. - http://danielwhittaker.me/2015/05/25/is-a-cqrs-command-gof-command/ In any case, best of luck on the rest of your assignment.
12th Nov 2017, 8:40 PM
David Carroll
David Carroll - avatar
0
@1of3 - Yep... after reviewing Sapphires' follow up response, I agree this is classic Command Pattern. Since I'm not used to seeing abstract questions at this level in SoloLearn, I simply assumed it was something more basic in nature. 🙃 @Sapphire - Now that I have the proper context, your questions make a lot more sense and I believe I can provide some suggestions based on the issues you described. However, I may not be able to respond until morning my time. It's 11pm (EST / New York Time) here and I've been up since 5am. So, basically, I need to get some sleep. If this hasn't been answered by the morning, I'll post a response.
12th Nov 2017, 4:05 AM
David Carroll
David Carroll - avatar