Task 6: Refactor to DRY the Support Classes

Review the code in the Employer, Location, CoreCompetency, and PositionType classes. What similarities do you see?

There is a fair amount of repetition between the classes. As a good coder, anytime you find yourself adding identical code in multiple locations you should consider how to streamline the process.

DRY = “Don’t Repeat Yourself”.

Create a Base Class

Let’s move all of the repeated code into a separate class. We will then have Employer, Location, CoreCompetency, and PositionType inherit this common code.

  1. Create a new class called JobField in the package org.launchcode.techjobs.oo.

  2. Consider the following questions to help you decide what code to put in the JobField class:

    1. What fields do ALL FOUR of the classes have in common?
    2. Which constructors are the same in ALL FOUR classes?
    3. What getters and setters do ALL of the classes share?
    4. Which custom methods are identical in ALL of the classes?
  3. In JobField, declare each of the common fields.

  4. Code the constructors.

  5. Use Generate to create the appropriate getters and setters.

  6. Add in the custom methods.

  7. Finally, to prevent the creation of a JobField object, make this class abstract.

Extend JobField into Employer

Now that you have the common code located in the JobField file, we can modify the other classes to reference this shared code. Let’s begin with Employer.

Modify line 5 to extend the JobField class into Employer.

public class Employer extends JobField {

   //Code not displayed.

  1. Next, remove any code in Employer that matches code from JobField (e.g. the id, value, and nextId fields are shared).

  2. Remove any of the getters and setters that are the same.

  3. Remove any of the custom methods that are identical.

  4. The empty constructor is shared, but not the second. Replace the two constructors with the following:

    public Employer(String value) {

    The extends and super keywords link the JobField and Employer classes.

  5. Rerun your unit tests to verify your refactored code.

Finish DRYing Your Code

Repeat the process above for the Location, CoreCompetency, and PositionType classes.

Rerun your unit tests to verify that your classes and methods still work.


You know you need to do this, but here is the reminder anyway. Save, commit, and push your work to GitHub.