COMP2511

24T3 Week 10

Wednesday 3PM - 6PM (W15A)

Thursday 11AM - 2PM (H11C)

 

Slides by Christian Tolentino (z5420628)

Agenda

  • Admin
  • Builder pattern
  • Kahoot
  • Exam tips

Admin

  • If you did assignment-iii, please make sure you submit it following the instructions in the spec.
    • No autogenerated UMLs
    • No ChatGPT blogs pls
    • Include video that is public. We will not be chasing this up since marking will happen during week 11/12

MyExperience

Please fill it in (10 mins)

Builder Pattern

Builder Pattern

 Encapsulation: It encapsulates the construction logic, keeping it separate from the object’s representation.

What type of pattern?

Creational

Provides a flexible solution for constructing complex objects. It decouples the creation of an object from its representation, allowing the same construction process to create different representations. This pattern is particularly useful when an object can be created with various configurations or when the construction process involves multiple steps (or a long parameter list)

Code Demo

Trains.java

Code Demo

In our system, we have trains which contain many engines and/or wagons. As in a normal train, these engines/wagons are ordered sequentially from the front of the train to the back.

Engines provide a certain amount of power and wagons require a certain amount of power to pull. At each point along the train, taking into account preceeding engines/wagons, the sum of the engine power must be greater than the sum of the wagon's required power. Otherwise, the rest of the train will be left behind!

There are three types of wagons:

  • Passenger wagons which require 500 engine power each

  • Cargo wagons which require 1,000 engine power each

  • Engines which provide power. They can be created at any power. For our system, we will create them at two variants. A "high powered" engine (2,000 power) and a "normal" engine (1,000) power.

 

Code Demo

Create a train builder that constructs a train in order, wagon by wagon with the above constraint. If the above constraint is violated at any point, throw an IllegalStateException.

Write a simple test in main that calls the builder directly to construct a train.

Question: How could you extend this solution to add ToyTrains and a ToyTrainBuilder to the system?

 

Kahoot

Exam Tips

Exam Tips

  • Practice good exam techniques.
  • Get familiar with writing Java & Generics
  • Learn the patterns and the differences (YouTube has some really good resources if you don't understand certain ones).
  • Know code smells and methods of refactoring (refactoring guru)
  • Please ensure that your code compiles. Even if you didn't finish the question, at least get it to a compliable state (Write all the placeholder code you need to, to achieve this)
  • Please ensure that you get familiar with VSCode.
  • Open VSCode in the correct folder (for syntax support)
  • Don't waste all your time on 10% of the exam
  • You can use dot points if you're short on time or prefer to

COMP2511 Week 10 24T3

By Christian Tolentino

COMP2511 Week 10 24T3

  • 161