0% found this document useful (0 votes)
3 views10 pages

Design Patterns in Programming

This document provides an introduction to three essential design patterns in programming: Singleton, Factory, and Observer. It explains their definitions, real-life analogies, and applications in app development, emphasizing the benefits of reusing proven solutions to common problems. The lesson aims to help beginners understand and identify these patterns to enhance their programming skills.

Uploaded by

Sourav Chhetry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Design Patterns in Programming

This document provides an introduction to three essential design patterns in programming: Singleton, Factory, and Observer. It explains their definitions, real-life analogies, and applications in app development, emphasizing the benefits of reusing proven solutions to common problems. The lesson aims to help beginners understand and identify these patterns to enhance their programming skills.

Uploaded by

Sourav Chhetry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Design Patterns in

Programming
A beginner-friendly lesson on Singleton, Factory, and Observer 4 the
smart shortcuts every programmer should know.

LEARNING OBJECTIVES

Understand Identify
What design patterns are and Singleton, Factory, and
why they exist Observer patterns

Connect
Relate each pattern to real-life and app scenarios
5 H¿¿‘: The Bug Qěeÿču¿±
"If you had to solve the same problem again and again& what would
you do?"

Students often answer: reuse a solution, use a shortcut, follow a template.


That's exactly right.

Design patterns are ready-made solutions to common


programming problems 4 like using a template instead of
starting from scratch every time.

When many programmers kept running into the same problems, they
came up with smart, reusable solutions. Those solutions became design
patterns.
n Reaš-Lufe C¿±±ecču¿±ÿ
Before we dive into code, let's spot these patterns in everyday life.

O±e Pñu±cuéaš Puśśa Sh¿é Aéé N¿čufucaču¿±ÿ


A school has only one principal A pizza shop makes different You get notified instantly when
4 not ten. One person, one pizzas for each customer 4 something changes 4 a new
authority, no duplicates. same process, different results. post, a message, an upload.

< These same problems repeat in software 4 so we use design patterns to solve them elegantly.
 Su±gšeč¿± Paččeñ±
ONLY ONE ALLOWED

Whač Ič D¿eÿ

The Singleton pattern ensures that only


one instance of an object is ever created
in your app.

Reaš-Lufe EŊa«éše

A school has one principal


A country has one president

Aéé EŊa«éše

A school app uses one database


connection 4 not a new one every time
a page loads.

' Key idea: Prevents


duplication and wasted
resources.
 Facč¿ñō Paččeñ±
Whač Ič D¿eÿ

A Factory is a class that creates objects


for you. You tell it what you need 4 it
builds it.

Reaš-Lufe EŊa«éše

A pizza shop takes your order and makes


your pizza 4 you don't go into the
kitchen yourself.

Aéé EŊa«éše

A school app uses a Factory to create


different user types:

Student accounts
Teacher accounts
Admin accounts

' Key idea: Simplifies object


creation 4 one method does
the work.
 Observer Pattern
The Observer pattern means: when one thing changes, everything
watching it gets notified automatically.

Real-Life Example App Example

You subscribe to a YouTube In a school app, when a teacher


channel. The moment the creator uploads notes ³ all subscribed
uploads ³ you get a notification. students are instantly notified.
You didn't have to check
manually.
' Key idea: Automatic
updates 4 no need to
keep asking "has
anything changed?"
Whō Uÿe Deÿug± Paččeñ±ÿ?
"Why not just write new code every time you face a problem?"

Saļeÿ Tu«e Leÿÿ Reéečuču¿±


Reuse proven solutions instead of starting from zero Write the logic once 4 don't copy-paste across your
codebase

Beččeñ Oñga±uÿaču¿± Eaÿueñ č¿ U±deñÿča±d


Your code has clear structure and is easier to maintain Other developers instantly recognise familiar patterns

D One-line summary: Design patterns make programming faster, smarter, and more collaborative.
§ Qěuc‘ Acčuļučō: Thu±‘3Pauñ3Shañe
Read the scenario below, then match each feature to its design pattern.

"A school app has: one database, creates different types of users, and sends notifications when teachers post."

1 2 3

O±e Dačabaÿe Cñeaču±g Uÿeñÿ N¿čufucaču¿±ÿ


Which pattern ensures only one Which pattern handles creating Which pattern automatically alerts
database connection is ever Student, Teacher, and Admin students when a teacher uploads
created? accounts from one place? new content?

³ Singleton ³ Factory ³ Observer


[ Exit Question
Before you leave, answer one of the following:

Option A
Give one real-life or app example of any design pattern we learned
today 4 and name the pattern.

Option B
Which pattern would you use for push notifications, and why?
Explain in 132 sentences.

T Share your answer with your partner first, then we'll hear a
few responses as a class.
í Teacheñ Tué
Keep every explanation anchored to this four-step structure. Students remember patterns best when they see the full journey
4 from problem to real world.

Reaš-šufe Aéé
Pñ¿bše« Paččeñ±
EŊa«éše EŊa«éše

Repeat this loop for each pattern. The more students hear this structure, the more naturally they'll apply it when writing their
own code.

D Pro tip: Ask students to come up with their own real-life analogy 4 ownership boosts retention.

You might also like