Computer Science Grade 9 20 min

Design Patterns Intro

Design Patterns Intro

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a design pattern is in the context of software development. Explain why design patterns are useful for writing clean, reusable, and maintainable code. Identify the problem that the Singleton design pattern solves. Describe the basic structure of the Singleton pattern. Identify the problem that the Factory design pattern solves. Describe the basic structure of a simple Factory pattern. Choose the appropriate pattern (Singleton or Factory) for a simple programming scenario. Ever built with LEGOs? 🧱 You probably use the same clever techniques over and over to build strong walls or cool wheels, right? Design patterns are like those LEGO techniques, but for coding. They are proven, reusable solutions to common problems that programmers face. Le...
2

Key Concepts & Vocabulary

TermDefinitionExample Design PatternA reusable, well-tested solution to a commonly occurring problem in software design. It's like a blueprint you can customize to solve a particular problem in your code.A recipe for baking a cake is a pattern. You follow the steps, but you can change the ingredients to make a chocolate cake or a vanilla cake. Object-Oriented Programming (OOP)A programming style based on the concept of 'objects', which can contain data (attributes) and code (methods). It helps organize complex programs.A `Player` object in a game has data like `health` and `score`, and methods like `jump()` and `attack()`. Creational PatternA type of design pattern that deals with the process of creating objects. These patterns give you more flexibility in deciding which ob...
3

Core Syntax & Patterns

Singleton Pattern Structure 1. A private constructor. 2. A private static variable to hold the single instance. 3. A public static method that returns the single instance. Use this when you need exactly one object of a class to coordinate actions across the system. The private constructor stops anyone from creating new instances, and the static method ensures everyone gets the same, single object. Simple Factory Pattern Structure 1. A 'Factory' class. 2. A method in the factory that takes a parameter (like a string). 3. Inside the method, an if/else or switch statement that creates and returns different objects based on the parameter. Use this when you need to create different types of related objects, but you want to hide the creation logic from the code that uses...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A `DocumentFactory`'s `createDocument` method contains large blocks of code inside its `if/else` statement to set fonts, margins, and headers for each document type. Based on the tutorial's pitfalls, how should this be improved?
A.The factory should be converted into a Singleton.
B.All the setup logic should be moved into the factory's constructor.
C.The factory should only create the document object; the setup logic should be moved into each document's own constructor or methods.
D.separate factory should be created for each document type (e.g., `PDFFactory`, `WordFactory`).
Challenging
A team is building a `GameConfiguration` class. Team A wants to use a Singleton, arguing there's only one set of configurations at a time. Team B argues against it, saying they might need to load and compare two different configuration objects side-by-side (e.g., 'default' vs. 'user_saved'). Whose reasoning is stronger, based on the tutorial's advice?
A.Team A, because Singleton is the standard for any configuration object.
B.Team B, because the need to have more than one instance, even for comparison, violates the core requirement for a Singleton.
C.Team A, because a Factory could be used to create the single instance, making it more flexible.
D.Team B, because Singletons are too complex for a Grade 9 project.
Challenging
You are debugging a `FileSystemManager` class that is supposed to be a Singleton, but you discover that multiple instances are being created, causing errors. Which of the 'Core Rules' for a Singleton was most likely broken?
A.The constructor was made `public` instead of `private`.
B.The `getInstance()` method was made `static`.
C.The private variable holding the instance was not `static`.
D.The class was not placed in a special 'singleton' folder.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Object-Oriented Programming

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.