GDGoC: BZU - Design Patterns: No More Spaghetti Code

rasheedsrq 7 views 28 slides Feb 27, 2025
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Slides for our Design Patterns Session, for more info: https://gdg.community.dev/events/details/google-gdg-on-campus-birzeit-university-birzeit-palestine-presents-design-patterns-no-more-spaghetti-code/


Slide Content

Design Patterns: No More Spaghetti Code 🍝

What are Design Patterns? Typical solution for occurring problem. Not copy/paste and run. General Concept, not algorithm. Algorithm Design Patterns

Why Design Patterns? Tried and tested solution. Teaches principles of OOD (Object Oriented Design). Common language.

W hen? When your code feels like spaghetti 🍝 (hard to read/maintain). When multiple developers need to collaborate (common language). When scaling your app (patterns ensure flexibility).

C ategories Creational Behavioral Structural Observer Strategy Adapter Decorator Factory Singleton

Benefits you’ll be getting How? Why? What?

Which P atterns for today? Singleton πŸšͺ One Door Factory β˜• Coffee Maker Observer πŸ“’ News Alerts

Quiz Time ❓ 🧠

Categories Creational Behavioral Structural Observer Strategy Adapter Decorator Factory Singleton

Factory Pattern

Factory What : Centralize object creation. When : Object creation logic is complex Multiple object types (e.g., UI components, payment gateways).

How: Factory class with create() method. Decouples client code from concrete classes. Gotchas: Avoid for simple object creation.

Why Use the Factory Pattern? Encapsulation of Object Creation Loose Coupling Improved Maintainability Scalability

Types of Factory Patterns Simple Factory (Static Factory Method) Factory Method Pattern Abstract Factory Pattern

Gonggi Time

Categories Creational Behavioral Structural Observer Strategy Adapter Decorator Factory Singleton

Singleton Pattern

Every time a class is instantiated, a new object is created Unnecessary memory usage and performance issues Risk of data conflicts when sharing resources Problem

Ensures only one instance of a class is created. The same object is shared across the application. Prevents data conflicts and saves resources. Solution - Singleton

How : Private constructor. Static instance method. Gotchas: Overuse β†’ Tight coupling. Multithreading risks.

Concurrent Access – Avoids conflicts when multiple processes access the object Global Access Point – A single access point for consistency Single Object – Centralized control and easier management Why Use Singleton?

W ordle Time πŸ” 

Categories Creational Behavioral Structural Observer Strategy Adapter Decorator Factory Singleton

Observer Pattern

Observer What : Subscribe/notify for state changes. When : Dynamic updates (e.g., chat apps, stock prices). Decoupling components (e.g., UI ↔ backend).

How: Subject (youtuber) + Observers (subscribers). Gotchas: Memory leaks (forget to unsubscribe).

What NOT to Do: Anti-Patterns 🚫 Monolithic Class: One class doing everything. C opy-Paste Programming: Duplicate code. Pattern Overkill: Using Singleton for everything.

Challenge TimeπŸ”₯πŸ†
Tags