KEMBAR78
Design Patterns - Day 1 | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
9 views36 pages

Design Patterns - Day 1

Uploaded by

alwin
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)
9 views36 pages

Design Patterns - Day 1

Uploaded by

alwin
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
You are on page 1/ 36

Design Patterns

Day 1 - History, Bene ts, Classi cation

Manimaran / Aug 30, 2025


fi
fi
References
📖 Books

• Head First Design Patterns by Eric Freeman & Elisabeth


Robson

• Design Patterns: Elements of Reusable Object-Oriented


Software
by Erich Gamma, Richard Helm, Ralph Johnson, John
Vlissides (Gang of Four)

🌐 Online Resource

• https://refactoring.guru/design-patterns
(Excellent beginner-friendly explanations with diagrams and
code samples in multiple languages)
History of Design Patterns

• Christopher Alexander (architecture → software)

• GoF (Gang of Four) - Design Patterns: Elements of Reusable


Object-Oriented Software, 1994 book → 23 core patterns

• In uence on modern frameworks


fl
Why Use Patterns?

• Avoid reinventing the wheel

• Reusable, proven solutions

• Common vocabulary among developers

• Some Criticisms
Metrics for Good Design

• Low coupling

• High cohesion

• Reusability, Extensibility, Maintainability


Classi cation of Patterns

• Creational – object creation

• Structural – object composition

• Behavioral – object interaction

• Application – architectural styles (MVC, MVP, MVVM, MVI,


VIPER, Microservices)
fi
Catalog Introduction
Creational Patterns
Structural Patterns
Behavioral Patterns
Application Patterns

• MVC (Model-View-Controller)

• MVP (Model-View-Presenter)

• MVVM (Model-View-ViewModel)

• MVI (Model-View-Intent)

• VIPER (View-Interactor-Presenter-Entity-Router)

• Microservices Patterns (API Gateway, Circuit Breaker, CQRS)


Intro to Creational Patterns

• Encapsulate object creation → exibility, maintainability

• Creational design patterns provide various object creation


mechanisms, which increase exibility and reuse of existing
code. fl
fl
Factory Method
• De ne a common interface for creating objects.

• Let subclasses (or a factory class) decide which concrete


class to instantiate.

• Helps avoid spreading new keyword + if/else everywhere.

• Real World: Hiring factory (developer, tester)


fi
Abstract Factory

• Create families of related objects

• Real World: GUI toolkit → Light & Dark theme components

• Code Demo → FurnitureFactory


Builder
• Step-by-step object construction

• Real World: House Builder

• House h = new House(2, true, true, false, "marble", "sloped");


Quiz
Q: You need di erent payment methods (Card, UPI, Wallet). Which
Creational Pattern ts?

Ans: Factory Method

Q: Which pattern helps you switch between Light and Dark UI themes
easily?

Ans: Abstract Factory

Q: You need di erent Laptop con gurations (RAM, SSD, GPU). Which
pattern?

Ans: Builder
ff
ff
fi
fi
Prototype
• Clone existing object → save creation cost

• Real World: Shapes


Singleton
• Only one instance → global access point

• Real World: Database connection, Logger


Quiz
Q: In a game, you duplicate enemies with same properties. Which
pattern?

Ans: Prototype

Q: Which pattern ensures a single instance across the app?

Ans: Singleton
Review
• Factory Method
Creates one object at a time - Hire Developer or Tester

• Abstract Factory
Creates a family of objects - Modern furniture set (Chair + Sofa)

• Builder
Builds objects step by step - Build a House ( oors, garage, garden)

• Prototype
Clone an existing object - Copy shapes in a drawing app

• Singleton
Only one instance in the system - Logger or Database connection
fl
Problem 1: Vehicle Factory
You are building a transport booking system. The system should
support Car, Bike, and Bus as di erent vehicle types.

• Each vehicle must implement a drive() method.

• You should use a Factory Method to create the correct


vehicle at runtime based on input.

Task:

1. De ne a Vehicle interface with drive().

2. Implement Car, Bike, and Bus.

3. Implement a VehicleFactory class that returns the correct object.

4. In main(), take input "car", "bike", "bus" → return and call drive().
fi
ff
Problem 2: Pizza Builder
You need to design a Pizza ordering system.

• A Pizza can have dough, sauce, and toppings.

• You should allow exible combinations (e.g., thin crust +


tomato sauce + cheese).

• Implement the Pizza creation using the Builder Pattern.

Task:

1. Create a Pizza class with attributes: dough, sauce, toppings.

2. Implement a PizzaBuilder class with setDough(), setSauce(),


addTopping() methods.

3. Build a custom pizza in main() and print the details.


fl
Intro to Structural Patterns

• Object composition → exibility, reusability

• Structural design patterns explain how to assemble objects


and classes into larger structures, while keeping these
structures exible and e cient.
fl
fl
ffi
Adapter

• Convert one interface into another

• Real World: Power plug adapter, legacy payment API


wrapper
Bridge
• Decouple abstraction from implementation

• Real World: Shape + Color combinations


Composite
• Hierarchical structures (tree) / object tree

• Real World: File system (Folders + Files), Org Chart


Decorator
• Add responsibilities dynamically / Wrapper

• Real World: Noti er example (SMS, Email, Slack)


fi
Quiz
Q: TV remote controlling di erent TV brands. Which pattern?

Ans: Bridge

Q: Windows Explorer tree is example of?

Ans: Composite

Q: Using a USB-to-HDMI connector is an example of?

Ans: Adapter
ff
Facade
• Simplify complex subsystem

• Real World: Home Theater system → one remote


Flyweight
• Share data across objects → memory optimization, also
known as: Cache

• Real World: Characters in a text editor


Proxy
• Substitute object to control access

• Types of Proxy:

◦ Virtual → delay heavy object creation

◦ Protection → control access (permissions)

◦ Remote → represent object in another system/network

◦ Smart → add caching, logging, security

• Real World: Virtual proxy for image loading, Credit card proxy
to bank
Quiz
Q: Which pattern optimizes memory in large-scale apps?

Ans: Flyweight

Q: Net ix button to play movie (instead of multiple service calls). Which


pattern?

Ans: Facade

Q: Which pattern delays loading heavy objects until needed?

Ans: Proxy
fl
Problem 3: Media Player Adapter
You are building a media player that should play both MP3 and MP4 les.

• The existing MP3Player can play only MP3.

• A new MP4Player is available but has a di erent interface (playMp4()).

• Use an Adapter Pattern to integrate MP4 into your MediaPlayer


interface.

Task:

1. De ne MediaPlayer interface with play(filename).

2. Implement MP3Player normally.

3. Wrap MP4Player using MediaAdapter so it can be used as MediaPlayer.

4. Test with both MP3 and MP4 les.


fi
fi
ff
fi
Problem 4: Co ee Decorator
You need to design a Co ee ordering system.

• Base product: SimpleCoffee.

• Add-ons: Milk, Sugar, Whipped Cream .

• Customers can order co ee with multiple add-ons.

Task:

1. Create a Coffee interface with getDescription() and getCost().

2. Implement SimpleCoffee.

3. Implement decorators (MilkDecorator, SugarDecorator, etc.).

4. In main(), build co ee with di erent combinations and print


description + cost.
ff
ff
ff
ff
ff
Prep Assignment
📝 Recall from Today

• Pick 1 Creational + 1 Structural pattern.

• Write a real-world analogy for each.

🔮 Look Ahead

• Find a situation where:

◦ One change a ects many others.

◦ You switch approach depending on context.

◦ A button triggers an action .

🌐 Mini Research

• Identify one design pattern used in:

◦ Java libraries / Android / React / any framework.


ff

You might also like