COMP2511

Tutorial 9

Today

  • Finding Patterns
  • Code and Design Smells
  • Revision Exercises

Reminders

  • Follow the template for your blogs for assignment 2
  • Assignment 3 is due Friday, Week 10
    • You may switch partners or complete it by yourself
      (please let me know)
    • Completely optional (bonus 8%)!
  • Lab 10 will be a sample exam using the exam environment
    • ​For in person classes
    • Tutorial 10 is still on regardless

Finding Patterns

Finding Patterns

1. Sorting a list of students records in different orders

  • Strategy pattern
  • In Java, each ordering is an implementation of a Comparator that can be passed to Collections.sort                                                

 

 

2. Model a collection of toys. Some toys like a Box can store other toys

  • Composite pattern
  • Composite nodes: any toy that can store other toys
  • Leaf nodes: any toy that is not a composite                                                                   

3. Adjusting the brightness of computer screens based on a light censor

  • Observer pattern
  • The light censor is the Subject, the computer screens are the Observers
  • When the light censor changes, the corresponding action is that the brightness of the computer screens may change

Finding Patterns

4. Model a VendingMachine that could either be in idle mode, product selection mode or payment mode.

  • State pattern
  • Each mode (i.e. Idle, ProductSelection, PaymentPending) should be encapsulated into its own state   
  • It's not the strategy pattern because each state defines how it can transition to another state

5. Model PacMan where its ability (i.e. speed, strength) can be boosted when it runs over a buff

  • Decorator pattern
  • Each buff is a decorator that can be used to boost PacMan. At any given time, it can have different buffs and or multiples of the same buff

Finding Patterns

Design Patterns are tools used to solve common problems in software

Remember KISS, don't use design pattern if you don't need to

Code Smells & Design

Code Smells & Design

Identify any code smells and underlying design problems

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.

Code smell(s):

divergent change

  • Multiple changes needed to be made to PetShop every time a new animal is added

Design problem(s): 

  • Violating DRY, Open-Closed Principle
  • High coupling

Code Smells & Design

Two more questions will be done in a code editor

Check the notes on Github

How do code smells cause problems when developing code?

Is a code smell always a sign of a design problem?

No. For example, sometimes message chaining a.getB().getC() is unavoidable

  • There are usually no immediate consequences to introducing code smells
  • In some cases, it can actually speed up development because no brain juice is spent on design
  • However, over the long term, code smells will make the system very difficult to maintain and change

Revision Questions

The End

COMP2511 Tutorial 9

By matthewliu-2

COMP2511 Tutorial 9

  • 61