Menu
Coddy logo textTech

Intro to Design Patterns

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 64 of 87.

Design patterns are proven solutions to common problems that developers encounter repeatedly when building software. Rather than reinventing the wheel each time, you can apply these time-tested templates to solve recurring design challenges.

Think of design patterns as blueprints. They don't give you finished code to copy-paste, but they describe how to structure your classes and objects to solve a specific type of problem. The patterns you'll learn leverage the OOP concepts you've already mastered—inheritance, interfaces, polymorphism, and composition.

Design patterns fall into three main categories:

CategoryPurposeExamples
CreationalHow objects are createdSingleton, Factory, Builder
StructuralHow objects are composedAdapter, Decorator, Composite
BehavioralHow objects communicateObserver, Strategy, Command

Why learn design patterns? They give you a shared vocabulary with other developers—saying "use a Factory here" instantly communicates a complex idea. They also help you write more flexible, maintainable code by applying principles like "program to an interface, not an implementation."

In the upcoming lessons, you'll implement several essential patterns, starting with creational patterns like Singleton and Factory, then moving to behavioral patterns like Observer and Strategy.

challenge icon

Challenge

Easy

Let's build a simple pattern classifier that demonstrates your understanding of the three design pattern categories! You'll create a system that categorizes patterns and explains their purpose, organizing your code across multiple files.

You'll create three files:

  • Pattern.java: Create an interface called Pattern that defines the contract for all design patterns. Your interface should declare two methods: String getCategory() which returns the pattern's category (Creational, Structural, or Behavioral), and String getPurpose() which returns a description of what that category of patterns does.
  • PatternInfo.java: Create three classes that implement your Pattern interface, each representing one category of design patterns:

    CreationalPattern - should return "Creational" for category and "How objects are created" for purpose.

    StructuralPattern - should return "Structural" for category and "How objects are composed" for purpose.

    BehavioralPattern - should return "Behavioral" for category and "How objects communicate" for purpose.

  • Main.java: Bring your pattern classifier together! You'll receive one input: a pattern name (String) that will be one of "Singleton", "Factory", "Adapter", "Decorator", "Observer", or "Strategy".

    Based on the input, create the appropriate pattern object:

    • "Singleton" and "Factory" are Creational patterns
    • "Adapter" and "Decorator" are Structural patterns
    • "Observer" and "Strategy" are Behavioral patterns

    Store your pattern object in a variable of type Pattern (the interface), demonstrating the principle of programming to an interface. Then print the following three lines:

    Pattern: [input pattern name]

    Category: [category from getCategory()]

    Purpose: [purpose from getPurpose()]

You will receive one input: the name of a design pattern (String).

This challenge reinforces the key insight from the lesson: design patterns are organized into categories based on what problem they solve. By using an interface, you're also practicing the principle of "programming to an interface, not an implementation" - a core concept that makes design patterns so powerful!

Cheat sheet

Design patterns are proven solutions to common problems in software development. They are blueprints that describe how to structure classes and objects to solve specific types of problems.

Design patterns fall into three main categories:

CategoryPurposeExamples
CreationalHow objects are createdSingleton, Factory, Builder
StructuralHow objects are composedAdapter, Decorator, Composite
BehavioralHow objects communicateObserver, Strategy, Command

Design patterns provide a shared vocabulary among developers and help write more flexible, maintainable code by applying principles like "program to an interface, not an implementation."

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String patternName = scanner.nextLine();
        
        // TODO: Declare a variable of type Pattern (the interface)
        Pattern pattern = null;
        
        // TODO: Based on patternName, create the appropriate pattern object:
        // - "Singleton" and "Factory" are Creational patterns
        // - "Adapter" and "Decorator" are Structural patterns
        // - "Observer" and "Strategy" are Behavioral patterns
        
        // TODO: Print the output in the following format:
        // Pattern: [input pattern name]
        // Category: [category from getCategory()]
        // Purpose: [purpose from getPurpose()]
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming