COMP2511

24T2 Week 9

Wednesday 2PM - 5PM (W14A)

Thursday 11AM - 2PM (H11A)

Slides by Christian Tolentino (z5420628)

This week

  • Identifying patterns and UML diagrams
  • Identifying code smells
  • Template Pattern
  • Discussion of revision exercises

This tutorial should be relatively short :)

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. 
  • Modelling a file system. 
  • Updating a UI component when the state of a program changes. 
  • Parsing and evaluating arithmetic expressions. 
  • Adjusting the brightness of a screen based on a light sensor. 

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
  • Modelling a file system. Composite
  • Updating a UI component when the state of a program changes. State
  • Parsing and evaluating arithmetic expressions. Composite
  • Adjusting the brightness of a screen based on a light sensor. Observer

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.
  • 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 (potential Law of Demeter violations)
  • Solution: Move method/Method forwarding

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

Template Pattern

Template Pattern

What type of pattern?

Behavioural pattern

The template pattern is a pattern that defines the skeleton of an algorithm in a method, deferring some steps to subclasses. This pattern lets subclasses redefine certain steps of an algorithm without changing its structure.

Revision Exercises

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

COMP2511 Week 9 24T2

By Christian Tolentino

COMP2511 Week 9 24T2

  • 152