COMP2511

24T3 Week 9

Wednesday 3PM - 6PM (W15A)

Thursday 11AM - 2PM (H11C)

Slides by Christian Tolentino (z5420628)

This week

  • Identifying patterns and UML diagrams
  • Identifying code smells
  • Visitor Pattern
  • Discussion of revision exercises (if enough time)

Identifying design patterns

In groups, determine a possible pattern that could be used to solve each of the following problems:

  • Sorting collections of records in different orders.
  • Listing the contents of a file system.
  • Traversing through a linked list without knowing the underlying representation.
  • Updating a UI component when the state of a program changes.
  • Allowing users to remap their movement controls to different buttons on a game controller.
  • Creating a skeleton implementation for a payment processing algorithm that varies in logic based on the type (e.g. credit card, PayPal)
  • A frozen yogurt shop model which alters the cost and weight of a bowl of frozen yogurt based on the toppings that customers choose to add before checkout.

Then pick one and start to think about potential entities and draw up a rough UML diagram.

Identifying design patterns

In groups, determine a possible pattern that could be used to solve each of the following problems:

  • Sorting collections of records in different orders - Strategy
  • Listing the contents of a file system. - Composite
  • Traversing through a linked list without knowing the underlying representation. - Iterator
  • Updating a UI component when the state of a program changes. - Observer
  • Allowing users to remap their movement controls to different buttons on a game controller. - Command
  • Creating a skeleton implementation for a payment processing algorithm that varies in logic based on the type (e.g. credit card, PayPal) - Template
  • A frozen yogurt shop model which alters the cost and weight of a bowl of frozen yogurt based on the toppings that customers choose to add before checkout. - Decorator

Then pick one and start to think about potential entities and draw up a rough UML diagram.

Code and Design Smells

In groups, discuss the following examples. Identify the code smells and any underlying design problems associated with them.

  • Divergent Change: One class is commonly changed in different ways for different reasons, high coupling
  • Solution: Strategy Pattern

a) Mark, Bill and Jeff are working on a PetShop application. The PetShop has functionality to feed, clean and exercise different types of animals. Mark notices that each time he adds a new species of animal to his system, he also has to rewrite all the methods in the PetShop so it can take care of the new animal.

Text

  • Code smell: Long parameter list
  • Solution: Builder pattern, introduce parameter object, method calls inside construction
  • Feature envy: one class is more interested in the details of another class than its own (Law of Demeter violations)
  • Solution: Move method/Method forwarding
public class MathLibrary {
    List<Book> books;

    int sumTitles() {
        int total = 0
        for (Book b : books) {
            total += b.title.titleLength;
        }
        return total;
    }
}

public class Book {
    Title title; // Our system just models books as titles (content doesn't matter)
}

public class Title {
    int titleLength;

    int getTitleLength() {
        return titleLength;
    }

    void setTitleLength(int tL) {
        titleLength = tL;
    }
}

How do these code smells cause problems when developing code?

  • Maintainability: Code can get complex and fragile
  • Readability: Code can be hard to understand and inconsistent
  • Scalability: Rigid/not flexible, hard to extend (think open-closed principle)

Is a code smell always emblematic of a design problem?

  • Design tradeoffs: fixing some code smells might introduce new ones
  • Some code smells are low risk, and are okay for small programs

Visitor Pattern

Visitor Pattern

What type of pattern?

Behavioural

Allows you to separate algorithms from the objects on which they operate. By doing so, it enables you to add new operations to existing object structures without modifying those structures (avoid LSP violation). This pattern is particularly useful when you need to perform various unrelated operations (decouples operations) across a complex object structure.

Code Demo

Computer.java

In this scenario we have Computers, Keyboards and Mouses which all are of type ComputerComponent. We want to be able to 'visit' different types of Computer components by logging the following messages:

 

Looking at computer Corelli with memory 500 GB.
Looking at keyboard Mechanical keyboard which has 36 keys.
Looking at mouse Bluetooth mouse.

 

In particular though, anyone which is visiting a Computer must be validated prior to being able to visit.

Extend/modify the starter code to use the Visitor Pattern to allow different computer components to be visited.

Visitor Pattern

Revision Exercises

If anyone has any questions about one of them we can discuss together :)

COMP2511 Week 9 24T3

By Christian Tolentino

COMP2511 Week 9 24T3

  • 160