M8 — Inversion of Control, MVC, Visitor, and Concurrency

SENG 365 — Software Engineering

Neil Ernst

University of Victoria

2026-07-24

Learning Objectives

Learning Objectives

Reading: Robillard, 3rd ed. — Ch. 8, Inversion of Control (pp. 193–239). (Concurrency material is supplementary.)

  • Inversion of control
  • GUI concepts and event handling / control flow
  • Model-View-Controller and its wider applications
  • The Visitor pattern
  • Concurrent programming concepts

Inversion of Control

Inversion of Control

  • Demo: Lucky Number — design problems without Observer.
  • Separate the program elements responsible for:
    • storing state (a database and associated objects)
    • viewing state (a View — a web page or UI)
    • changing state (commands sent by the user)
  • Observer pattern: the model is uninterested in observers; register/deregister as needed; callback approach.

IoC and Dependency Injection

  • Something else controls program flow (often a framework).
  • Observer/Observable in the JDK.
  • Dependency Injection (Guice): separate behaviour from dependency resolution.

DI, Step by Step

  • Unit-test as normal — not always possible (e.g. charging credit cards).
  • A Factory offloads dependency management, but exposes dependencies and grows in number.
  • DI stage 1: inject dependencies as constructor params — but clients now manage them.
  • DI with Guice: bind dependencies in a separate module.
  • Essential complexity remains; DI reduces accidental complexity (Brooks, No Silver Bullet).

Responsibility-Driven Design

  • CRC cards
  • Single Responsibility Principle
  • Write simple, testable code

Visitor

Visitor Pattern

Problem: support an open-ended number of operations on an object graph without changing the objects’ interface.

Visitor (Wikipedia)

  • Inverts control and ensures loose coupling: operations are delegated to separate visitor objects via accept().

Visitor: Traversal

  • Example: an epidemiological tree — you are a child of the node that infected you; print all infected people.
  • Without Visitor, the data structure and traversal are tightly coupled.
  • Traversal can live in the Visitor or in the object’s accept() method (the latter has direct iterator access).
  • Motivation (Solitaire): add print/log, search, remove to CardSource without bloating the interface (ISP, YAGNI).

CRC Cards

Class–Responsibility–Collaborators (Beck & Cunningham, OOPSLA 1989).

  1. Class: the name/role
  2. Responsibilities: problems to solve
  3. Collaborators: objects exchanging messages

Apply CRC cards to design a library management system (separate from checkout/patron code): items (book/magazine/video), subject, purchase, author, condition.

Concurrency

What is Concurrency?

  • Concurrent programming is tricky: humans think sequentially; debugging is hard.
  • Concurrency is an aspect of the problem domain (handle multiple simultaneous events).
  • Parallelism is an aspect of the solution domain (run faster on multiple processors).

Threads and Processes

  • Process: self-contained, no directly shared memory (share via message passing).
  • Thread: lightweight process, shared memory → easy resource-contention problems.
    • Local memory per thread; global memory shared.
  • Service: the distributed analog to a process (λ / serverless).

Thread Mechanics (Java)

  • Thread created off the main thread.
  • Pass a function to run as a Runnable.
  • Sleep the thread instance.
  • Video 8-1/8-2: concurrency in Java; the debugger example.

Synchronization

The biggest challenge is avoiding resource contention. Thread execution is non-deterministic.

class Counter {
    private int c = 0;
    public void increment() { c++; }
    public void decrement() { c--; }
    public int value()      { return c; }
}
  • Thread interference: overlapping steps lose updates.
  • Synchronized blocks; deadlocks and starvation.

Concurrency OO Principles

  • Always lock during updates to object fields.
  • Always lock access of possibly-updated object fields.
  • Never lock invocation of methods on other objects.

Why Concurrency Helps Design

  • Abstraction: separate tasks without worrying about when to execute them.
  • Responsiveness: a responsive UI with independent tasks.
  • Performance: split complex tasks across processors.

Dining Philosophers

  • N philosophers (threads) around a table.
  • N chopsticks (resources) between them.
  • Must use the 2 nearest chopsticks to eat (critical section).

Challenge: design a solution that prevents deadlock and starvation. What if everyone picks up their left chopstick?

Other Approaches

  • Go: goroutines and channels. Pike: “don’t communicate by sharing memory; share memory by communicating.”
  • Actor model: Erlang, Elixir.

Levels of Reactive in Java

  1. Thread and Runnable (Java 1)
  2. ExecutorService, Callable, Future (5)
  3. ForkJoinPool (7)
  4. CompletableFuture (8)
  5. Flow and reactive streams (9)
  6. HTTP/2 client (11)
  7. Reactive libraries (RxJava)

In-Class Activities

Day 1

  • Group discussion

Day 2

  • Code review exercise and feedback
  • Intro to architecture design; architecture patterns vs. design patterns

Day 3

  • Office hours