COMP2511 Week 7

Agenda

  • Admin Stuff
  • Assignment 1 Feedback
  • Finding Patterns
  • Composite Pattern
  • Factory Pattern

Admin Stuff

  • Awesome work on your first assignment
    • Your feedback can be found in your feedback branch.
  • Forum bonus mark
  • You can skip one lab without affecting your overall course mark. (lab 8)
  • Start assignment 2, it is due in Week 8 Friday, 5pm
  • Come to help seshes (there is one with me straight after our lab).
  • Continuing with interviews and group work checkups in the lab.

General Assignment One Feedback 

Everyone did super well, and overall happy with how we went. Here is some general feedback for improvement:

  • Make blackout controller less chonky
  • Don't type check using strings.
    • Don't do sat1.getType().equals("RelaySatellite")
    • Do sat1 instanceof RelaySatellite
  • PLZ MAKE YOUR BLOGS PUBLIC <3
  • Missing stuff in submission such as: 
    • UML diagram
    • Blog Link

If you want more feedback, just ask me or Sophia in the lab

 

Group Task

Finding Pattern

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

  1. Sorting collections of records in different orders.
    •  
  2. Modelling a file system
    •  
  3. Updating a UI component when the state of a program changes
    •  
  4. Parsing and evaluating arithmetic expressions
    •  
  5. Adjusting the brightness of a screen based on a light sensitivity
    •  

Finding Pattern

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

  1. Sorting collections of records in different orders.
    • Strategy Pattern
  2. Modelling a file system
    •  
  3. Updating a UI component when the state of a program changes
    •  
  4. Parsing and evaluating arithmetic expressions
    •  
  5. Adjusting the brightness of a screen based on a light sensitivity
    •  

Finding Pattern

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

  1. Sorting collections of records in different orders.
    • Strategy Pattern
  2. Modelling a file system
    • Composite Pattern
  3. Updating a UI component when the state of a program changes
    •  
  4. Parsing and evaluating arithmetic expressions
    •  
  5. Adjusting the brightness of a screen based on a light sensitivity
    •  

Finding Pattern

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

  1. Sorting collections of records in different orders.
    • Strategy Pattern
  2. Modelling a file system
    • Composite Pattern
  3. Updating a UI component when the state of a program changes
    • Observer Pattern
  4. Parsing and evaluating arithmetic expressions
    •  
  5. Adjusting the brightness of a screen based on a light sensitivity
    •  

Finding Pattern

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

  1. Sorting collections of records in different orders.
    • Strategy Pattern
  2. Modelling a file system
    • Composite Pattern
  3. Updating a UI component when the state of a program changes
    • Observer Pattern
  4. Parsing and evaluating arithmetic expressions
    • Composite Pattern
  5. Adjusting the brightness of a screen based on a light sensitivity
    •  

Finding Pattern

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

  1. Sorting collections of records in different orders.
    • Strategy Pattern
  2. Modelling a file system
    • Composite Pattern
  3. Updating a UI component when the state of a program changes
    • Observer Pattern
  4. Parsing and evaluating arithmetic expressions
    • Composite Pattern
  5. Adjusting the brightness of a screen based on a light sensitivity
    • Observer Pattern

Composite Pattern

Composite Pattern

What is it?

Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

When should we use it?

  • When we want to treat a group of objects in similar ways as a single object

Composite Pattern

Design for type safety: Only one child-related operation in Composite class

Design for Uniformity: Include all child-related operations in the Composite interface.

Code Demo

Inside src/calculator, use the Composite Pattern to write a simple calculator that evaluates an expression. Your calculator should be able to:

  • Add two expressions
  • Subtract two expressions
  • Multiply two expressions
  • Divide two expressions

{1 + [2 * (4 + 3)]}

Expression 1

Expression 2

Expression 3

Factory Pattern

Factory Pattern

What is it?
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

 

When should we use it?

  • When we want to group the creation of objects together

Factory Pattern

Code Demo

Inside src/thrones, there is some code to model a simple chess-like game. In this game different types of characters move around on a grid fighting each other. When one character moves into the square occupied by another they attack that character and inflict damage based on random chance. There are four types of characters:

  • A king can move one square in any direction (including diagonally), and always causes 8 points of damage when attacking.
  • A knight can move like a knight in chess (in an L shape), and has a 1 in 2 chance of inflicting 10 points of damage when attacking.
  • A queen can move to any square in the same column, row or diagonal as she is currently on, and has a 1 in 3 chance of inflicting 12 points of damage or a 2 out of 3 chance of inflicting 6 points of damage.
  • A troll can only move up, down, left or right, and has a 1 in 6 chance of inflicting 20 points of damage.

Code Demo

We want to refactor the code so that when the characters are created, they are put in a random location in a grid of length 5.

 

Use the Factory Pattern to create a series of object factories for each of the character types, and change the main method of Game.java to use these factories.

COMP2511 22T3 Week 7

By Jayden Leung

COMP2511 22T3 Week 7

  • 92