Java Design Patterns
Test your knowledge of creational, structural, and behavioural design patterns with Java implementations and real-world use cases.
Questions in this quiz
20 questions on Java Design Patterns. Work through them here, then start the quiz to answer them and see your score.
1.What are the three categories of GoF design patterns?
- AInstance, Class, Package
- BAbstract, Concrete, Hybrid
- CCreational, Structural, Behavioural
- DSimple, Compound, Complex
2.What problem does the Builder pattern solve?
- AConstructing complex objects step by step, avoiding telescoping constructors with many parameters
- BLazily initialising objects only when needed
- CCreating objects without exposing the class
- DBuilding class hierarchies dynamically
3.What does this code demonstrate?
class HttpRequest { private final String url; private final String method; private final String body; private HttpRequest(Builder b) { this.url = b.url; this.method = b.method; this.body = b.body; } static class Builder { String url, method = "GET", body; Builder url(String u) { this.url = u; return this; } Builder method(String m) { this.method = m; return this; } Builder body(String b) { this.body = b; return this; } HttpRequest build() { return new HttpRequest(this); } } }- AFacade pattern — hiding HTTP complexity
- BFactory Method pattern
- CPrototype pattern — cloning an existing request
- DBuilder pattern — fluent API constructing an immutable HttpRequest
4.What is the Strategy pattern?
- AA pattern for providing a simplified interface to a subsystem
- BA pattern for creating objects based on a condition
- CDefining a family of algorithms, encapsulating each one, and making them interchangeable at runtime
- DA pattern for composing objects into tree structures
5.What does this code demonstrate?
interface DiscountStrategy { double apply(double price); } class Cart { private DiscountStrategy strategy; Cart(DiscountStrategy s) { this.strategy = s; } double finalPrice(double price) { return strategy.apply(price); } } // Usage: Cart cart = new Cart(price -> price * 0.9); // 10% off Cart vip = new Cart(price -> price * 0.7); // 30% off- ADecorator pattern — wrapping the price
- BTemplate Method — subclasses define the discount
- CCommand pattern — encapsulating the discount action
- DStrategy pattern — discount algorithm injected as a lambda
6.What is the Observer pattern?
- AA pattern for observing method execution time
- BA one-to-many dependency where when one object (subject) changes state, all registered observers are notified automatically
- CA pattern where a proxy observes all method calls
- DA pattern where one object monitors another for errors
7.What is the Decorator pattern?
- AExtending a class with additional methods via inheritance
- BAdding logging to a method using AOP
- CWrapping an object in another object that adds behaviour, conforming to the same interface
- DAdding annotations to a class
8.What does this code demonstrate?
interface Coffee { String description(); double cost(); } class Espresso implements Coffee { public String description() { return "Espresso"; } public double cost() { return 1.5; } } class MilkDecorator implements Coffee { private final Coffee coffee; MilkDecorator(Coffee c) { this.coffee = c; } public String description() { return coffee.description() + ", Milk"; } public double cost() { return coffee.cost() + 0.3; } }- ADecorator pattern — adding Milk behaviour to any Coffee without subclassing
- BAdapter pattern — adapting Coffee to another interface
- CProxy pattern — controlling access to Espresso
- DComposite pattern — combining coffees
9.What is the Factory Method pattern?
- ADefining an interface for creating an object but letting subclasses decide which class to instantiate
- BA static method that creates objects
- CA pattern for pooling and reusing object instances
- DA method that creates multiple related objects
10.What is the Proxy pattern?
- AA pattern for bridging two incompatible interfaces
- BA surrogate that controls access to another object — adding lazy init, access control, logging, or remote access
- CA pattern providing a different interface to an existing object
- DA pattern that caches method results
11.What is the Template Method pattern?
- AUsing templates (generics) to define algorithm skeletons
- BDefining the skeleton of an algorithm in a base class, letting subclasses fill in specific steps without changing the overall structure
- CA method that generates boilerplate code
- DA pattern for creating objects using generic type parameters
12.What does this code demonstrate?
abstract class DataProcessor { // Template method final void process() { readData(); processData(); writeData(); } abstract void readData(); abstract void processData(); void writeData() { System.out.println("Writing to default output"); } } class CsvProcessor extends DataProcessor { void readData() { System.out.println("Reading CSV"); } void processData() { System.out.println("Processing CSV rows"); } }- ADecorator — adding behaviour to DataProcessor
- BTemplate Method — process() defines the skeleton; subclasses fill in steps
- CFactory Method — creating processors
- DStrategy pattern — algorithms swapped at runtime
13.What is the Facade pattern?
- AProviding a simplified, unified interface to a complex subsystem of classes
- BA pattern that adapts one interface to another
- CA pattern that adds a new layer to a subsystem
- DHiding implementation details using private fields
14.What is the Command pattern?
- AA pattern for chaining method calls
- BEncapsulating a request as an object, enabling undo/redo, queuing, logging, and parameterisation of operations
- CA pattern for processing CLI commands
- DA pattern for dispatching events to handlers
15.What is the Adapter pattern?
- AConverting the interface of a class into another interface that clients expect — making incompatible interfaces work together
- BA pattern for adapting class fields to a new type
- CA pattern that extends an object with new methods
- DA pattern for adapting to different databases
16.What is the Composite pattern?
- ASharing state between a group of related objects
- BCombining multiple design patterns in one class
- CWrapping multiple behaviours using multiple decorators
- DComposing objects into tree structures to represent part-whole hierarchies — treating individual objects and compositions uniformly
17.What does this code demonstrate?
interface Shape { double area(); } class Circle implements Shape { double r; Circle(double r) { this.r = r; } public double area() { return Math.PI * r * r; } } class ShapeAdapter implements Shape { private final LegacyShape legacy; ShapeAdapter(LegacyShape ls) { this.legacy = ls; } public double area() { return legacy.computeArea(); } }- AAdapter — wrapping LegacyShape to conform to the Shape interface
- BFacade — simplifying LegacyShape
- CProxy — controlling access to LegacyShape
- DDecorator — adding area() to LegacyShape
18.What is the difference between the Proxy and Decorator patterns?
- AProxy is structural; Decorator is behavioural
- BProxy controls access to an object (often created by the proxy itself); Decorator adds behaviour to an object (passed in from outside)
- CThey are identical — just different names
- DDecorator replaces the original; Proxy only wraps it
19.What is the Flyweight pattern?
- ASharing common state among many fine-grained objects to reduce memory usage
- BA lightweight version of the Singleton pattern
- CA pattern for fast object creation using object pools
- DA pattern for reducing method call overhead
20.Which SOLID principle does the Strategy pattern most directly support?
- ALiskov Substitution Principle
- BInterface Segregation Principle
- COpen/Closed Principle — open for extension (new strategies), closed for modification (no changes to the context class)
- DSingle Responsibility Principle