Unit 5 Iuhbg SMD
Unit 5 Iuhbg SMD
✅ Real-Life Example:
Think about using a TV remote. You don’t need to know how the TV works
inside. You just press one button like “ON” and the TV starts. That remote is like
the façade – it gives you a simple way to control something complicated.
2. Banking System:
To withdraw money, a bank system needs to:
Check your identity
Verify your account
Check your balance
Allow the transaction
This is a lot to handle for the user!
Solution: The bank app can have one class that does all these tasks when the
customer clicks on “Withdraw Money”. The customer doesn’t have to worry
about the steps.
✅ Conclusion:
The Façade Pattern makes working with complex systems easier.
It gives a clean and simple way to use many features.
It is useful in software, apps, machines, and many other areas.
**************************************************
Explain GOF design patterns.andList GOF patterns. Explain
any three patterns in detail
What are GOF Design Patterns?
GOF (Gang of Four) Design Patterns are common solutions to repeating
problems in software design. These patterns help make software more flexible,
reusable, and easy to maintain. Think of them like blueprints or templates that
experienced developers use to solve typical design issues.
These patterns are grouped into three main types based on what they do:
Type What it Does Example Idea
Like choosing how to build a house:
Creational Deals with object creation
pre-made, DIY, or hire a contractor
Deals with object structure Like organizing furniture in a room
Structural
& relationships for better layout
Deals with object Like deciding how people in a team
Behavioral
communication & behavior work together
💡 Simple Explanation:
Imagine there is only one manager in a company. Every time an employee
needs help, they go to that one manager. No one else can become a manager
while that one is already working.
Just like that, in programming, we create only one object (instance) of a class
and use it again and again. This pattern gives us controlled access to that one
object.
The arrow and box (return uniqueInstance) shows that only one object
(uniqueInstance) is returned every time someone asks for the Singleton.
🎯 Purpose:
To limit the creation of objects (only one instance)
To save memory
To avoid duplication
To have one central object that controls the entire system
💡 Simple Explanation:
Suppose you have a charger from the USA, but you're in India, and the socket
is different. You use a plug adapter to make the charger work. The adapter
adjusts the shape so both the plug and socket fit together.
In programming, if one class’s output doesn't match another class’s input, the
adapter helps make them compatible without changing their code.
🎯 Purpose:
To reuse existing code even if it's not directly compatible
To allow old systems to work with new systems
To connect mismatched interfaces
📌 Where is it used?
In software upgrades when old systems are still in use
Converters (file formats, power supply)
Payment gateways that connect different banks
Device drivers in operating systems
2. State (Interface)
An abstract interface that declares a method like Handle() which will be
implemented by concrete states.
3. ConcreteStateA and ConcreteStateB
These are the actual states.
Each has its own version of the Handle() method.
Example:
ConcreteStateA.Handle() might mean turning the TV ON.
ConcreteStateB.Handle() might mean turning the TV OFF.
💡 Simple Explanation:
Imagine a fan. It can be in Off, Low, Medium, or High speed. When you turn
the knob, the fan’s state changes, and its behavior (speed) also changes.
In software, we use the state pattern when an object has multiple states, and
each state performs different actions.
🎯 Purpose:
To allow an object to behave differently based on its current state
To avoid too many if-else or switch statements
To make code cleaner, organized, and easier to update
📌 Where is it used?
UI buttons: enabled, disabled, hover, click
Games: player idle, running, jumping
Mobile phones: silent, vibrate, general mode
Chat apps: online, offline, busy, typing
**************************************************
What are design patterns?? advantages of using design
pattern?
Design patterns are standard solutions to common problems that occur during software design.
Think of them like ready-made blueprints or templates that help developers solve problems quickly
and correctly without starting from scratch.
💡 Simple Example:
Imagine you're building a house. You don’t design everything from zero. You use standard layouts
like kitchen, bedroom, and bathroom designs. In the same way, in software, design patterns give us
standard ways to structure code.
✅ Key Points:
Advantage Explanation
✅ Saves Time You don’t have to solve the same problem again from scratch.
✅ Code Reusability Patterns allow you to reuse the design in many places.
✅ Improves Software Design Makes your software easier to maintain and modify.
✅ Solves Common Problems Many software problems already have a pattern-based solution.
********************************************************
How pure fabrication can solve issues related to high cohesion and
low coupling? Explain with the help of Sales man as the scenario
💬 In Simple Words:
Pure Fabrication is a helper class created to separate unrelated tasks from the
main class so that:
The main class stays focused (high cohesion)
The system becomes loosely connected (low coupling)
❌ Problem:
Now we also ask the Salesman class to:
Save order data to a database
Print bills
Generate sales reports
These tasks are not part of the sales role. They are technical tasks like data
handling and reporting.
Now the Salesman class is doing too many unrelated things → ❌ Low Cohesion
And it depends on how data is saved or how printing works → ❌ High
Coupling
⭐ Final Summary:
Concept Meaning Goal
Easy to understand and
High Cohesion Class does only one focused job
maintain
Changes in one class don’t
Low Coupling Classes are independent
affect others
Pure Extra helper class created just Helps achieve high cohesion
Fabrication for technical tasks and low coupling
💬 In One Line:
Pure Fabrication is a smart way to move unrelated responsibilities into
separate classes so the main class stays clean, focused, and easy to manage.
***********************************************
List and explain three types of design pattern.
**************************************************
Draw the Strategy pattern for Robotics application and
explain in brief.[6]
How It Works
1. The Robot doesn’t know how to move—it just asks its
current strategy (e.g., AgressiveBehaviour) to run moveCommand().
2. You can switch behaviors anytime:
o Need to attack? Set strategy = new AgressiveBehaviour().
o Need to hide? Set strategy = new DefensiveBehaviour().
3. The Robot’s move() method stays the same, but the behavior changes
dynamically.
**************************************************
Draw the class diagram for mobile scenario using state
design pattern.[6
Strategy Pattern Explained Simply (Robotics Example)
This image shows the Strategy Pattern, a design pattern that lets a robot (or
any object) change its behavior dynamically without rewriting its code.
Key Components in the Diagram
1. Robot
o The main object that needs to perform actions (like move()).
o It has a strategy (behavior) that decides how it moves.
2. iBehaviour (Interface)
o A contract that says: "All behaviors must have
a moveCommand() method."
o Ensures all behaviors work the same way, even if they do different
things.
3. Concrete Behaviours
o AgressiveBehaviour: Moves fast, attacks, or takes risks.
o DefensiveBehaviour: Moves carefully, avoids danger.
o NormalBehaviour: Balanced movement.
How It Works
1. The Robot doesn’t know how to move—it just asks its
current strategy (e.g., AgressiveBehaviour) to run moveCommand().
2. You can switch behaviors anytime:
o Need to attack? Set strategy = new AgressiveBehaviour().
o Need to hide? Set strategy = new DefensiveBehaviour().
3. The Robot’s move() method stays the same, but the behavior changes
dynamically.
**************************************************
Factory Design Pattern explanation
🧠 Factory Design Pattern (Simple Language + ASCII Diagram)
🧾 Simple Explanation:
The Factory Pattern is like a machine (factory) that makes different products
for you, based on your choice.
You don’t need to know how the product is made — you just place your order,
and the factory gives you the correct product.
✅ Summary:
Factory Pattern helps create objects based on input without showing the
creation logic.
It improves reusability, flexibility, and clean structure.
In SQL, the factory logic can be implemented by checking the "type"
column and mapping it to different tables or classes.
ASCII diagrams help show the flow from Client → Factory → Object in a
clear way.
**************************************************
What is a behavioural design pattern? How iterator pattern
solves the problem of traversal of any type of list?
What is a Behavioral Design Pattern?
How Iterator Pattern solves the problem of traversing any type of list?
💡 Simple Example:
Think about a team in an office — each member has a role and they need to
talk and work together smoothly to finish a project.
Behavioral patterns are like the rules and ways the team members
communicate and share work.
💡 Simple Explanation:
Imagine you have a box full of different things (like books, toys, clothes).
You want to look at each item, one by one, but you don’t want to open the box
and mess with how the things are arranged inside.
An Iterator is like a helper who knows how to go through the box and show you
one item at a time, no matter what kind of things or how they are arranged.
📝 Summary:
Term Meaning (Simple)
Behavioral Design
Ways objects talk and work together
Pattern
A helper that goes through a collection, one by one,
Iterator Pattern
without exposing its structure
**************************************************
elaborate on General Responsibility Assignment
Software Patterns(GRASP).
📘 What is GRASP?
GRASP stands for General Responsibility Assignment Software Patterns.
These are design guidelines used in object-oriented programming to decide:
➡️Which class or object should do what?
Just like in real life, where people are assigned jobs based on their skills, in
programming, GRASP helps us assign tasks to classes based on who knows
what, who should do what, and how to keep the system clean and easy to
change.
✅ 1. Information Expert
📖 What it Means:
Give the responsibility to the class that has the information (data) needed to
do the task.
📦 Example:
If you want to calculate the total bill for a customer’s shopping cart, who
should do it?
The answer is: the ShoppingCart class — because it has all the items and
prices.
✅ Why It's Useful:
It keeps logic in the right place.
It avoids confusion and mistakes.
Makes your code easy to understand and maintain.
💡 Real-Life Example:
If you want to know your grades, you don’t ask the Principal — you check your
Report Card, because it knows your marks.
✅ 2. Creator
📖 What it Means:
Give the responsibility of creating an object to a class that closely works with
or contains that object.
📦 Example:
An Order object should be responsible for creating OrderItem objects because:
It uses OrderItems.
It knows all the details needed to create them.
✅ Why It's Useful:
It creates clear relationships between classes.
Makes object creation organized and logical.
Reduces confusion on who should make what.
💡 Real-Life Example:
When you order pizza, the pizza shop makes it, not you. Why? Because it’s
connected to the product and has the right ingredients.
✅ 3. Low Coupling
📖 What it Means:
Coupling means how much one class depends on another.
Low coupling means keeping classes independent, so that changes in one class
don’t affect others.
📦 Example:
If a Payment class depends too much on a Bank class, and the Bank class
changes — it may break the Payment class.
By using low coupling, we can make sure:
Classes are independent.
Changes in one part don’t cause errors elsewhere.
Code is flexible and reusable.
💡 Real-Life Example:
If you use a charger with a USB port, you can use it with any phone — because
it’s not tightly connected to one brand. That’s low coupling.
✅ 4. High Cohesion
📖 What it Means:
Cohesion means how focused and related the responsibilities of a class are.
A class with high cohesion does only one main job, and does it well.
📦 Example:
If a class is doing too many unrelated tasks (like handling payment, printing
bills, and sending emails), it has low cohesion.
But if a class only does payment processing, it has high cohesion.
✅ Why It's Useful:
Makes the class easy to understand.
Reduces errors.
Each class has a clear purpose.
💡 Real-Life Example:
A cashier in a store only handles payments. He doesn’t cook food, stock
shelves, and sweep floors — that’s high cohesion.