AlgoViz

Which pattern, when

medium 45 min

A pattern is an answer to one kind of pressure.

Watch lesson4:38

Where it shows up

The same pattern appears across everyday software.

  • Java Comparator: Strategy

    A comparator is an object that orders two values. It can be passed to `Collections.sort`, so the sorting code never changes when the order does. Force: the rule comes from outside.

  • Java FilterInputStream: Decorator

    It wraps some other input stream, uses it as its source of data, and may add functionality. Buffering and decompression can be stacked in any mix.

  • Servlet FilterChain: Chain of responsibility

    A filter calls the chain to invoke the next filter, or the resource if it was the last. A filter that does not call it ends the request: a link may stop the rest.

  • Qt undo framework: Command

    Qt's documentation calls its undo framework an implementation of the Command pattern: all editing is done by creating command objects, which can undo themselves and sit on a stack.

  • Java Integer.valueOf: Flyweight

    Values from -128 to 127 are always taken from a cache. Force: very many equal, unchangeable objects.

  • Spring JdbcTemplate: Template method by callback

    The class keeps the fixed order (open, run, clean up) and you implement small callback interfaces for the open steps. Composition does the job that inheritance did in the book.

  • SQLite VFS: Adapter layer

    The VFS is the portability layer at the bottom of SQLite. The core calls one interface, and each operating system has its own object behind it.

  • Java Runtime: Singleton

    Every Java application has a single instance of Runtime, and a second one would make no sense, because there is one environment. A real force for one.

  • Java Iterator

    A pattern that became part of the library, and then of the language with the for-each loop. You use it without naming it.