Java代写 | COMP2100/6442 Lab 9 – Refactoring / Code Smells


Introduction and Agenda

Task (2 marks): Refactor the given program to successfully pass all design tests and
logic tests.

This lab focuses entirely on refactoring a small program made up of several classes.
There are many code smells and possible improvements present in the lab with many of
them being captured in the design tests. Note that the logic tests should pass before and
after your refactoring. That is, the underlying functionality remains the same.

You will need to read the specifications of the program very carefully to understand the
intention of the program and why certain design decisions need to be made. The lab slides
will begin by going over the program and then a narrative of how things turned out as they
are. The final slides are diagrams of what the program currently looks like.

A Note on Realism

The following lab is made to be somewhat realistic but in no way fully represents the
complexity of creating a backend with frameworks or deploying such a program. Hence,
we are not considering many of the finer aspects of designing such a system that you may
learn in other courses. This lab simplifies things for education purposes.

Specifications – As Outlined by the Client

As is with most program specifications, they will be outlined by a client. In this case, our
alien friend. In the next slides, we will break these down into dot points and a diagram.

Specifications – An Overview Part 1

Let us start by getting an overview of how the different pieces work together:


  • The data in this test will be stored in memory (as a class field) as opposed to persistent data.
  • Any incoming requests will be of the String type and outgoing requests should be an array of
    String. This does not however mean that data should be stored as Strings or that the backend
    should use only Strings.

Specifications – An Overview Part 2

We can now discuss the backend in more detail:

● A request handler should be the gateway (facade design pattern) to the backend.

  • It should handle incoming requests.
  • It should convert the types of incoming and outgoing communication to those most
    appropriate types. Keep in mind, outgoing requests are arrays of String.
  • There should only ever be one instance of the request handler.

● A quote handler should handle requests to and from the database. In our case, the data is
just stored as a class field.

  • It should not work using primitives but instead abstractions (classes).
  • It should retrieve copies of data from storage and not a reference to it for security.
    This is a special consideration given the data will be stored in memory.
  • There should only ever be one instance of the quote handler.

● Additional considerations:

  • The abstraction of the primitive inputs to the backend can be handled by a factory
    design pattern.