Skip to content
Computer Science · Grade 11 · Object-Oriented Programming and Design · Term 2

Introduction to Design Patterns

Students will be introduced to common software design patterns (e.g., Singleton, Factory) as reusable solutions to recurring problems.

Ontario Curriculum ExpectationsCS.HS.D.2

About This Topic

Design patterns offer proven, reusable solutions to common problems in object-oriented programming, such as managing single instances with the Singleton pattern or creating objects without specifying exact classes using the Factory pattern. Grade 11 students explore these patterns to solve recurring issues like object creation and state management. They examine how patterns improve code reusability and maintainability, directly addressing curriculum expectations for software design.

This topic builds on prior OOP knowledge by emphasizing abstraction and modularity, key to scalable software. Students analyze specific patterns, identify use cases, and critique over-application in simple projects, fostering critical thinking about when patterns add value versus unnecessary complexity. Connections to real-world software development prepare students for advanced studies and industry practices.

Active learning suits this topic well. When students refactor existing code to incorporate patterns in pairs or collaborate on pattern-matching exercises, they experience firsthand the benefits of cleaner, adaptable code. Group critiques of pattern implementations reveal trade-offs, making abstract concepts practical and memorable.

Key Questions

  1. Explain how design patterns promote code reusability and maintainability.
  2. Analyze a specific design pattern (e.g., Singleton) and identify its use cases.
  3. Critique the potential over-application of design patterns in simple software projects.

Learning Objectives

  • Explain how the Singleton pattern ensures a single instance of a class is created and accessible globally.
  • Analyze a given code snippet to identify which design pattern, if any, is being implemented.
  • Compare and contrast the Factory Method and Abstract Factory patterns in terms of their object creation strategies.
  • Critique the suitability of applying the Decorator pattern to a simple class structure, considering its impact on code complexity.
  • Design a basic implementation of the Observer pattern to manage updates between a subject and its observers.

Before You Start

Object-Oriented Programming Fundamentals

Why: Students need a solid understanding of classes, objects, inheritance, and polymorphism to grasp how design patterns build upon these concepts.

Encapsulation and Abstraction

Why: Design patterns often rely on controlling access to data and hiding implementation details, making prior knowledge of encapsulation and abstraction essential.

Key Vocabulary

Design PatternA general, reusable solution to a commonly occurring problem within a given context in software design. Patterns are not finished designs but templates for how to solve problems.
Singleton PatternEnsures that a class has only one instance and provides a global point of access to it. Useful for managing shared resources like database connections.
Factory PatternDefines an interface for creating an object, but lets subclasses decide which class to instantiate. It promotes loose coupling by separating the creation logic from the client code.
Creational PatternsA category of design patterns focused on object creation mechanisms, trying to create objects in a manner suitable to the situation. Singleton and Factory are examples.
Structural PatternsA category of design patterns concerned with class and object composition. The Decorator pattern, which adds responsibilities to objects dynamically, is an example.

Watch Out for These Misconceptions

Common MisconceptionDesign patterns should be used in every project for best practices.

What to Teach Instead

Patterns solve specific problems but can add complexity to simple code. Group refactoring activities let students compare pattern and non-pattern versions side-by-side, revealing when simplicity wins and building judgment skills.

Common MisconceptionDesign patterns are fixed templates that must be copied exactly.

What to Teach Instead

Patterns provide flexible guidelines adapted to context. Hands-on implementation in pairs shows students how to tweak structures for their needs, emphasizing creativity over rote application through peer reviews.

Common MisconceptionDesign patterns are only relevant for large-scale professional software.

What to Teach Instead

Patterns apply to student projects too, promoting good habits early. Collaborative hunts in existing codebases demonstrate everyday use cases, helping students see patterns as tools for their own work.

Active Learning Ideas

See all activities

Real-World Connections

  • Software engineers at Google use patterns like Singleton to manage global application state or configuration settings across large, complex applications like the Chrome browser.
  • Game developers often employ Factory patterns to create various types of game characters or items, allowing for easy expansion and modification of game content without altering core game logic.
  • Mobile app developers might use the Observer pattern to update the user interface in real-time when data changes in the background, such as live stock tickers or social media feeds.

Assessment Ideas

Quick Check

Present students with three short code examples. Ask them to identify which, if any, demonstrates the Singleton pattern and explain their reasoning. For example, 'Does this code allow multiple instances of `DatabaseManager`? Why or why not?'

Discussion Prompt

Facilitate a class discussion using the prompt: 'When might using the Singleton pattern be a bad idea? Consider a scenario where multiple independent parts of a program need their own unique configuration.' Encourage students to share specific examples and potential drawbacks.

Exit Ticket

On an index card, ask students to write down one common problem that the Factory pattern solves and then provide a brief, one-sentence description of how it solves that problem. For instance, 'Problem: Creating different types of `Shape` objects. Solution: The Factory pattern lets a `ShapeFactory` decide whether to create a `Circle` or `Square`.'

Frequently Asked Questions

How do design patterns improve code reusability and maintainability?
Design patterns standardize solutions to common problems, like Singleton ensuring one instance or Factory hiding creation logic. This reduces duplication, eases modifications, and makes code readable for teams. Students practicing refactoring see error rates drop and updates speed up in their projects.
What are good use cases for the Singleton pattern?
Singleton fits scenarios needing global access to one instance, such as loggers, database connections, or configuration managers. Students analyze code where multiple instances cause issues, then implement and test to grasp its role in resource control without overusing it.
How can active learning help teach design patterns?
Active approaches like pair refactoring and group debates make patterns tangible. Students code implementations, test failures without patterns, and critique trade-offs, shifting from passive reading to experiential understanding. This boosts retention and application in projects, as peers challenge assumptions collaboratively.
When should students avoid using design patterns?
Skip patterns in tiny scripts or prototypes where they add unneeded abstraction. Critique activities help students weigh costs, like increased code length against benefits, teaching balance. Simple if-statements often suffice early, reserving patterns for growth points.