COMP2511

Tutorial 10

Today

  • Template Pattern
  • Visitor Pattern
  • Kahoot

Reminders

If you did assignment-iii, please make sure you submit it following the instructions in the spec

  • No autogenerated UMLs
  • No ChatGPT blogs
  • Make the video accessible to the public
    • We will not be chasing this up since marking will happen during week 11/12

By Alvin

Template Pattern

Template Pattern

Suppose:

  • I created a ResumeTemplate and gave it to everyone in the class
  • You can edit the content within each section of the template to match your personal information
  • But you cannot edit the structure of the template itself

 

Think of templates from MS word

Template Pattern

UML Diagram

  • generateResume is the template method that can be split into smaller "steps"
  • Steps can be overridden by subclasses and they are the hook methods
  • Methods that have default implementation and cannot be overridden are final methods
    • Why is generateResume final?

Template Pattern

Behavioural design pattern that lets you

  • Define the skeleton (i.e. steps) of an algorithm in a super class
  • Override specific steps of the algorithm in subclasses without modifying the overall structure of the algorithm

Why use it?

  • Subclasses do not need to duplicate the entire algorithm each time 
  • By only allowing subclasses to modify hook methods, they cannot accidentally break the algorithm 

What is it?

Template Pattern

Code Demo

Model how characters in a game can move around and attack other characters

  • A King can
    • Move one square in any direction (including diagonally)
    • Always deal 8 points of damage when attacking
  • A Queen can
    • Move to any square in the same column, row or diagonal as her current position
    • Deal 12 points of damage with a 1 in 3 chance or 6 points of damage otherwise
  • A Dragon can
    • Move up, down, left or right
    • Inflict 20 points of damage with a 1 in 6 change

Visitor Pattern

Visitor Pattern

Suppose:

  • I have different types of characters in a Game
  • If I want to allow users to save the game, I need to be able store information about the game onto disk 
  • Convert information from the game into JSON files
  • But how can I convert instances of some class into a JSON file?

Add an interface JSONExport and implement it for every class that can be exported 

Visitor Pattern

What if now I can't edit the source code of these classes directly?

  • The game could be a legacy system, so the original authors don't want anyone to change the code anymore
  • The game could be developed by a different team in the company and they also they want other people messing with their code

Visitor Pattern

  • Visitable: classes that need the new feature
    • Must have an accept method that takes a visitor
  • Visitor: classes that will add a new feature to the visitable
    • Must have a visit method for each type of visitable

UML Diagram

Visitor Pattern

Code Demo

Visitor Pattern

What is it?

Benefits

Behavioural design pattern that allow functionalities to be added to classes without needing to "open" them

When to use?

  • Obey the Open-close principle 
  • Obey the Single responsibility principle because every new functionality is a new "visitor"

New operations need to be added to all elements of an existing object structure

Kahoot

kahoot

The End

The End

Thank you all for a great term and good luck for your exams 🎉

COMP2511 Tutorial 10

By matthewliu-2

COMP2511 Tutorial 10

  • 55