Bloc 3Design Patterns

Design Patterns

Authors: Christophe Damas, Jose Vander Meulen

What are design patterns

  • Identify good recurring solutions to recurring problems in computing.
  • A competent developer should be able to identify these problems, implement the corresponding solution, and communicate with peers using the patterns vocabulary.

Pattern categories

  • Creational (object instantiation): Builder, Abstract Factory, Factory Method, Singleton, Prototype.
  • Structural (class/object composition): Adapter, Facade, Composite, Decorator, Flyweight.
  • Behavioral (object communication): Strategy, Command, Visitor, Observer, Template Method, Chain of Responsibility, Iterator, State.

Pattern notes and examples

Singleton

  • One instance for a given class (already seen in Java workshop).
Les types de base

Prototype

  • Create objects by cloning an object passed as a parameter (already seen in JavaScript).
Les types de base

Observer

  • A subject keeps a list of observers and notifies them on state changes by calling one of their methods.
Les types de base

Iterator

  • Provides sequential access to elements of a collection without knowing its physical representation.
Les types de base

Facade

  • Presents a simplified signature over a complex set of objects, making the system easier to use.
Les types de base

Flyweight

  • Reduces the cost of creating/manipulating many similar objects by reusing a single instance of identical objects.
  • Java Integer uses Flyweight: Integer.valueOf caches values from -128 to 127 and returns cached instances; outside that range it creates a new Integer.
  • Example idea: Integer i=1; Integer j=1; System.out.println(i==j); uses the cache.
Les types de base

Decorator

  • Dynamically increases an object’s behavior.

  • Example Mario:

  - IMario interface with jump, moveForward, takeDamage, update.

  - Mario stores health and decreases it in takeDamage.

  - StarMario wraps an IMario, ignores damage, delegates jump/move/update, counts down a timer (1000), and on expiration restores GameSingleton.instance.mario to the decorated Mario.

Les types de base

Strategy vs Template Method

  • Same goal: define a family of algorithms (example: sorting a list of persons by different criteria).
  • Initial issue: duplication in separate sort classes (by age vs by name).
  • Strategy refactor:   - ListePersonnes.tri(ComparatorStrategy) calls compare(p1, p2) on a strategy (e.g., NomStrategy uses compareTo on names).
Les types de base
  • Template Method refactor:   - ListePersonnes is abstract with tri() and abstract compare(p1, p2) implemented by subclasses (e.g., sort by age).
Les types de base

Composite

  • Combine similar objects so they behave like a single object (tree structures).

  • Example drawing software: Shape interface; leaf shapes (Circle, Square); CompoundShape holds a list of Shape and forwards move/draw to its children; can nest compound shapes.

Les types de base

Visitor

  • Represents an operation to apply on elements of an object structure (often a composite); similar to Strategy with one method per node type.

  • Example: export shapes to XML.

  - Visitor interface with visitCircle, visitSquare, visitCompoundShape.

  - Shape.accept(Visitor) delegates to the visitor.

  - XMLExportVisitor builds XML by visiting each shape and nesting compound shapes.

Les types de base

Command

  • Encapsulates an action to execute later.

  • Example: TextFile has open/save; TextFileOperation interface; OpenTextFileOperation implements execute; TextFileOperationExecutor collects and runs operations.

Les types de base

Adapter

  • Allows classes with incompatible interfaces to work together.

  • Example: Belgian plug to UK socket adapter; adapter implements UK plug interface and forwards to a Belgian plug’s method.

Les types de base

Other patterns

  • Factory Method
  • Abstract Factory
  • State
  • Builder
  • Chain of Responsibility

Course info / evaluation

  • Goal: master all patterns.
  • Each session provides exercises to:   - Identify a pattern already implemented in code.   - Identify a pattern for a given problem and implement it.   - Identify a pattern to improve existing code and adapt it (refactoring).
  • Final evaluation uses similar exercises; Moodle is accessible during the exam; one extra recto/verso sheet is allowed.