"Hi, Amigo!"
"Hello, Captain Squirrels, sir!"
"Naaamoy mo ba yan, sundalo?"
"Hindi, ayoko."
"Malamang sa terminal mo nanggaling ang amoy. Tingnan natin kung ano ang ginagawa mo diyan."
"Huh? Paano mabaho ang code?"
"Totoo, siguradong hindi mabaho... pero madalas may amoy."
"Captain, gumagawa ako ng bagong personnel management system para sa barko. Tingnan mo, malapit na ang project."
"Well, oras na para ma-achieve mo si Zen sa refactoring. Doon mo lang magagawang linisin ang code mo. Makipag-ugnayan kay Agent IntelliJ IDEA para sa assignment mo. Ibibigay niya sa iyo ang lahat ng instructions."
"Yes, sir! Isinasagawa ang iyong order, sir."
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 1)
It's time for a little refactoring. Wikipedia says, "Refactoring
or reorganizing code is the process of changing the internal structure of a program, without
affecting its external behavior and with an aim to making it easier to understand.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 2)
2.1. Extract a subclass.
2.1.1. Add the Soldier class to the human package.
2.1.2. Get rid of the isSoldier field.
2.1.3. Move the necessary methods from Human to Soldier.
2.1.4. Update the Human constructor's signature.
2.2. Pull up the body of the constructor.
7
Gawain
Java Multithreading, antas, aralin
Refactoring (part 3)
3.1. Push down a field. Push down the course field to the appropriate class. Make it private.
3.2. Push down a method. Push down the getter for the course field to the appropriate class.
3.3. Extract an interface.
3.3.1. Create an Alive interface in the human package.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 4)
4.1. Replace inheritance with delegation.
4.1.1. The University class must not inherit Student.
4.1.2. The University class must have a students list. Don't forget to initialize it.
4.1.3. Add a setter and getter for students.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 5)
5.1. Create a template method.
5.1.1. Add a String getPosition() method to the Human class. It should return the string "Person".
5.1.2. Override this method in the Student and Teacher classes. The method should return "Student" and "Teacher", respectively.
28
Gawain
Java Multithreading, antas, aralin
Refactoring (part 6)
6.1. Replace a parameter with a set of specialized methods. Replace the Student class's setValue() method
with specialized setCourse() and setAverageGrade() methods.
6.2. Add a parameter. Add a double parameter to the getStudentWithAverageGrade() method so the average grade of the required student.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 7)
7.1. Parameterize a method. Replace the incAverageGradeBy01() and incAverageGradeBy02() methods
with an incAverageGrade(double delta) method.
7.2. Pass an entire object. Rewrite the addStudentInfo() method so that it
takes a Student object as an argument.
7
Gawain
Java Multithreading, antas, aralin
Refactoring (part 8)
8.1. Remove a setter. Remove the setId() method. The id field should be set only when
the object is created.
8.2. Hide a method (field). Change the visibility of the nextId field to match
the area where it is used.
28
Gawain
Java Multithreading, antas, aralin
Refactoring (part 9)
9.1. Self-encapsulate a field. Rewrite the incAverageGrade() method, using a setter and getter to access averageGrade.
9.2. Replace an array field with an object. Replace the int[] size array with an instance of a new Size type,
containing the following public fields: int height and int weight.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 10)
Understand the code in the car package.
10.1. Replace a constructor with a factory method.
10.1.1. Declare Truck, Sedan, and Cabriolet classes that inherit Car.
10.1.2. Add constructors that have an int numberOfPassengers constructors.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 11)
11.1. Replace an error code with an exception. Rewrite the fill(double numberOfGallons)
method to throw an Exception if there is an error.
11.2. Decompose a conditional expression.
11.2.1. Add and implement a method in the Car class that determines whether the passed date belongs to
summer.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 12)
12.1. Combine conditional expressions.
12.1.1. In the Car class, add a private method that indicates whether passengers can be carried:
boolean canPassengersBeCarried(). The method should return true if
the driver is available (isDriverAvailable) and there is fuel.
28
Gawain
Java Multithreading, antas, aralin
Refactoring (part 13)
Understand the code in the user package.
13.1. Extract a method. Add a printInfo() method that displays the first and last name
on the console, formatted as follows -
First name: Jason;
Last name: Slater.
Replace the duplicate code in printUsers() with a method call.
14
Gawain
Java Multithreading, antas, aralin
Refactoring (part 14)
14.1. Move a field. Replace the isAnnaMale and isRomanMale fields with a male field in the appropriate class.
Add a setter and getter for the new field (when choosing method names, consider the field type).
14.2. Extract a class.
14.2.1. Add an Address class to the user package.
7
Gawain
Java Multithreading, antas, aralin
Refactoring (part 15)
Great. You've mastered basic refactoring techniques: Pull up a field, pull up a method, Encapsulate a collection, extract a subclass, pull up the body of a constructor, push down a method, push down a field, extract an interface, collapse a hierarchy, replace inheritance with delegation.