Menu
Coddy logo textTech

Intro to Design Patterns

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 89 of 110.

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 established templates to solve similar challenges in a consistent, maintainable way.

Think of design patterns as recipes. Just as a chef doesn't invent a new way to make bread every time, programmers use patterns that have been refined over decades. These patterns leverage the OOP concepts you've already learned - classes, inheritance, interfaces, and polymorphism - and combine them in specific ways to solve particular problems.

Design patterns are typically grouped into three categories:

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

In the upcoming lessons, you'll learn specific patterns from each category. Each pattern addresses a distinct problem: ensuring only one instance of a class exists, creating objects without specifying exact classes, or allowing objects to notify others of changes. Understanding when and why to use each pattern is just as important as knowing how to implement it.

challenge icon

Challenge

Easy

Let's build a simple problem-solving system that demonstrates how design patterns organize code by category! You'll create a structure that classifies different software challenges and suggests which type of pattern might help solve them.

You'll organize your code into two files:

  • patterns.dart: Create an enum called PatternCategory with three values: creational, structural, and behavioral. Each should have a String description field explaining its purpose:
    • creational: "Handles object creation"
    • structural: "Handles object composition"
    • behavioral: "Handles object communication"
    Also create a class called Problem that represents a software challenge. It should have a String name field and a PatternCategory category field. Include a method suggest() that returns a string in the format: [name] -> Use a [category description] pattern
  • main.dart: Import your patterns file and demonstrate how different problems map to different pattern categories. Create three Problem instances:
    • A problem named Ensure single database connection with category creational
    • A problem named Make old API work with new system with category structural
    • A problem named Notify users when data changes with category behavioral
    Call suggest() on each problem and print the results.

This challenge helps you see how design patterns are categorized by the type of problem they solve - a key concept as you begin learning specific patterns in the upcoming lessons!

Expected output:

Ensure single database connection -> Use a Handles object creation pattern
Make old API work with new system -> Use a Handles object composition pattern
Notify users when data changes -> Use a Handles object communication pattern

Cheat sheet

Design patterns are proven solutions to common programming problems. They are templates that combine OOP concepts (classes, inheritance, interfaces, polymorphism) in specific ways.

Design patterns are grouped into three categories:

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

Try it yourself

import 'patterns.dart';

void main() {
  // TODO: Create three Problem instances:
  // 1. name: "Ensure single database connection", category: creational
  // 2. name: "Make old API work with new system", category: structural
  // 3. name: "Notify users when data changes", category: behavioral

  // TODO: Call suggest() on each problem and print the results
}
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