Software Engineering

Written Assignment 3: Refactoring

Due on November 12th at 11:59 PM.

This is an individual assignment. You are to complete this assignment on your own, although you may discuss the lab concepts with your classmates. Remember the Academic Integrity Policy: do not show your work to anyone outside of the course staff and do not look at anyone else’s work for this lab. If you need help, please post on the Piazza forum or contact the instructor. Note that no exception is made for your group members; collaboration with your group members is a violation of the Academic Integrity Policy as much as collaboration with other members of the class. If you have any doubts about what is okay and what is not, it’s much safer to ask than to risk violating the Academic Integrity Policy.

Your Task

For this assignment, write answers to the questions below and upload them to your personal GitHub repository for the assignment. You can find your repository at:

git@github.swarthmore.edu:cs71-f17/written3-<username>.git

where <username> should be replaced by your Swarthmore username. You may add whatever materials to this repository you please, but your answers to each question must be clearly identified.

Refactoring

Refactoring is the process of changing design without changing behavior. We discussed this concept in lecture, worked through some examples, and looked at a list of common refactorings. Be sure to familiarize yourself with that list.

Below is a list of scenarios in which existing code should be changed to include a new feature. When refactoring, the ideal approach is first to modify the code so that the design can accommodate the new feature and then to introduce the feature. For each scenario below, perform the following tasks:

  1. An travel agency specializing in ocean cruises allows its customers to specify their accommodation preferences. The company would like to accept more information about their clients’ preferences, but the code is becoming ungainly. The following method declaration appears on an interfacenamed Scheduler.

    public void addSchedulingRequest(
            Customer customer, Date startDate, Date endDate,
            MealType preferredMeal, boolean nonSmoking,
            boolean preferUpperDecks);
    
  2. A social networking side provides a client library for interacting with its servers. Quite some time ago, the site switched to a more sophisticated protocol than it used originally. Support for the old protocol has been in this client library for years, but no one is using it anymore and maintaining it is a problem. The next major version of the library will drop support for the legacy protocol.

    public interface ProtocolHandler {
        public void login(String username, String password)
            throws AuthenticationFailure;
        public void postMessage(String message);
        public void logout();
    }
    public class LegacyProtocolHandler implements ProtocolHandler { ... }
    public class NewProtocolHandler implements ProtocolHandler { ... }
    
  3. An online auction site permits three types of accounts: buyer accounts, seller accounts, and administrator accounts. It is often the case that a customer is both a buyer and a seller, but this currently requires two different accounts. The company would instead like to think of users as fitting into certain roles and would like to expand the number of roles in the future.

    public class Account { ... }
    public class SellerAccount extends Account { ... }
    public class BuyerAccount extends Account { ... }
    public class AdministratorAccount extends Account { ... }
    
  4. A computer game permits the player’s character to acquire a number of accessories, but adding new accessories to the game is becoming difficult.

    public int calculateGaudiness(Accessory accessory) {
        if (accessory.getName().equals("tiara")) {
            return 5;
        } else if (accessory.getName().equals("pope hat")) {
            return 9;
        } else if (accessory.getName().equals("belt") &&
                   accessory.getColors().equals(
                       Arrays.asList("lime", "fuschia"))) {
            return 28;
        } else if (accessory.getName().equals("snow boots")) {
            return 2;
        }
        return 0;
    }
    
  5. A university wants to allow non-student affiliates (such as employees) to take classes at their institution. Currently, this would require copying a lot of logic from the Student class to the Employee class.

    public class Person { ... }
    public class Employee extends Person { ... }
    public class Student extends Person {
        private Schedule schedule;
        ...
        public void enrollInCourse(Course course) {
            if (!schedule.isFreeDuring(course.getTimeSlot())) {
                throw new SchedulingConflictException();
            } else if (schedule.getCourseCount() >= schedule.getMaxCourses()) {
                throw new OverenrollmentException();
            } else {
                schedule.getCourseSet().add(course);
            }
        }
    }