CodeBlanks Logo Icon CodeBlanks

What is a design pattern?

JavaScript course · Expert · Lesson 1/63

Learn what design patterns provide and common catalogs.

What is a design pattern?

A design pattern is a reusable solution template for a recurring problem in software design. You do not copy and paste the solution; you adapt the pattern to your own code.

Patterns are best practices shaped by experience. They give teams a shared vocabulary: when you say "use a Singleton here", everyone understands the intent.

A common catalog, known as "Gang of Four" (GoF), lists 23 patterns.

Pattern catalogs

The best-known catalog is the Gang of Four (GoF) book, which groups 23 patterns into three families:

  • Creational: organizes object creation. Examples: Singleton, Factory.
  • Structural: combines classes. Examples: Adapter, Decorator.
  • Behavioral: organizes communication. Examples: Observer, Strategy.

This group covers four common patterns: Singleton, Factory, Observer and Module.

Exercise Counter class

Declare a class named Counter. Its constructor sets this.count to 0. Its increment() method increases this.count by one and returns the new value.

Show example solution
Start with class, set this.count = 0 in the constructor, then in increment do this.count++ and return this.count.
Example solution:
class Counter {
  constructor() {
    this.count = 0;
  }
  increment() {
    this.count++;
    return this.count;
  }
}

Test yourself

  1. How is a design pattern best described?

    • A programming language feature
    • A reusable solution template
    • A debugging method
    • A browser API
    Show answer

    A reusable solution template — Correct! A design pattern is a reusable solution template for a recurring problem.

  2. Complete the console call

    class Logger {
      log(message) {
        console.____(message);
      }
    }
    Show answer
    class Logger {
      log(message) {
        console.log(message);
      }
    }
  3. Match the concepts

    Design pattern
    Reusable solution template
    GoF catalog
    Common pattern list
    Creational
    Object creation
    Behavioral
    Object interaction

All lessons in Expert