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).
Prototype
- Create objects by cloning an object passed as a parameter (already seen in JavaScript).
Observer
- A subject keeps a list of observers and notifies them on state changes by calling one of their methods.
Iterator
- Provides sequential access to elements of a collection without knowing its physical representation.
Facade
- Presents a simplified signature over a complex set of objects, making the system easier to use.
Flyweight
- Reduces the cost of creating/manipulating many similar objects by reusing a single instance of identical objects.
- Java Integer uses Flyweight:
Integer.valueOfcaches 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.
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.
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)callscompare(p1, p2)on a strategy (e.g.,NomStrategyusescompareToon names).
- Template Method refactor:
 -
ListePersonnesis abstract withtri()and abstractcompare(p1, p2)implemented by subclasses (e.g., sort by age).
Composite
-
Combine similar objects so they behave like a single object (tree structures).
-
Example drawing software:
Shapeinterface; leaf shapes (Circle, Square);CompoundShapeholds a list ofShapeand forwardsmove/drawto its children; can nest compound shapes.
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.
Command
-
Encapsulates an action to execute later.
-
Example:
TextFilehasopen/save;TextFileOperationinterface;OpenTextFileOperationimplementsexecute;TextFileOperationExecutorcollects and runs operations.
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.
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.