0% found this document useful (0 votes)
9 views28 pages

Unit 5 Iuhbg SMD

The document explains the Façade Design Pattern, which simplifies complex systems by providing a single interface for users. It details two examples: a Home Theater System and a Banking System, illustrating how a façade class can streamline multiple operations. Additionally, it introduces GOF design patterns, specifically the Singleton, Adapter, and State patterns, highlighting their purposes, real-life examples, and benefits in software design.

Uploaded by

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

Unit 5 Iuhbg SMD

The document explains the Façade Design Pattern, which simplifies complex systems by providing a single interface for users. It details two examples: a Home Theater System and a Banking System, illustrating how a façade class can streamline multiple operations. Additionally, it introduces GOF design patterns, specifically the Singleton, Adapter, and State patterns, highlighting their purposes, real-life examples, and benefits in software design.

Uploaded by

omkaratole01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Unit 5 smd

Explain Façade Behavioral Patterns. Write in detail any 2


Façade Behavioral Patterns and Explain the solution of
facade pattern using UML diagram
Sure! Here's a simple and clear 7-mark answer on the Façade Design Pattern,
written in easy-to-understand language without any code, and explained step
by step.

Façade Design Pattern (Structural Pattern)


✅ What is Façade Pattern?
The Façade Pattern is used to hide the complexity of a system by providing a
simple interface. Instead of interacting with many classes and methods, the
user (or client) just uses one class that does everything for them.

✅ Why use Façade Pattern?


 To make the system easier to use.
 To reduce confusion caused by too many classes.
 To hide the inner details of how things work.
 To make the code clean and easy to maintain.

✅ 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.

✅ Components of Façade Pattern:


1. Client – The user who wants to do something (e.g., turn on the TV).
2. Facade – The main class that gives simple functions (e.g., remote
control).
3. Subsystems – The real classes doing the work (e.g., screen, speakers,
power supply), hidden from the client.

✅ Two Examples of Façade Pattern (in theory):


1. Home Theater System:
If you want to watch a movie at home, you have to:
 Turn on the projector
 Lower the screen
 Start the DVD player
 Turn on the sound system
All of this is confusing!
Solution: Use a single class called something like HomeTheaterController. You
just call one method like “watchMovie()”, and it does all the steps for you.

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.

✅ UML Diagram of Façade Pattern (Simple View):


+-----------------+
| Client |
+-----------------+
|
v
+-----------------+
| Facade | <---- Simple interface
+-----------------+
| | |
v v v
+-------+ +-------+ +---------+
| Class | | Class | | Class | <---- Complex subsystems
+-------+ +-------+ +---------+
Simple Explanation:
 The Client uses the Facade.
 The Facade talks to the inner Subsystems.
 The client doesn’t need to know how subsystems work.

✅ 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

Type of design pattern


Strategy Design Pattern,
Singleton Design Pattern,
State Design Pattern,
Adapter Design Pattern

🧱 1. Singleton Design Pattern (Creational Pattern)


📌 What is it?
The Singleton Design Pattern makes sure that only one object of a class is ever
created in the whole program. No matter how many times you try to create the
object, you will always get the same instance.

💡 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.

Explanation Based on the Image:


The class is named Singleton.
It has a static method called Instance() which:
Checks if the instance already exists.If not, it creates a new one.Then it returns
that instance.
Methods like SingletonOperation() and GetSingletonData() are the operations
that can be performed using the singleton object.

The arrow and box (return uniqueInstance) shows that only one object
(uniqueInstance) is returned every time someone asks for the Singleton.

🧠 Why Use Singleton?

1. To control object creation (only one is allowed).


2. To reduce memory usage
3. To avoid conflicts caused by multiple instances.
4. To ensure a centralized point of control.

🎯 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

📷 Real-Life Examples (imagine photos):


 President of a country – Only one at a time
 One printer in an office – All people use the same printer
 Single light switch for the main hall – One switch controls everything
 Single database connection shared by all users

🧠 Why do we need Singleton?


 Sometimes we want only one shared resource, like a printer or
database.
 Multiple objects can create confusion or waste memory.
 It is better to have a single access point for a task.

✔ Features of Singleton Pattern:


 Only one object is created
 Same object is used everywhere
 It is global – available throughout the program
 Good for shared resources
📌 Where is it used?
 Database connection objects
 File logging systems
 Configuration managers
 Cache systems
 Controllers in game engines

💬 Summary in Simple Words:


Singleton means only one object for the whole program. It is useful when we
need centralized control and don’t want to create multiple objects. It gives
easy access to one object across the system.

🧩 2. Adapter Design Pattern (Structural Pattern)


📌 What is it?
The Adapter Design Pattern helps to connect two incompatible systems. It acts
as a bridge or middleman that helps one thing work with another even if their
formats or languages are different

Based on the Image You Shared (Fig. 5.5.8):


📦 Components:
1. Client
The code that wants to use a specific service.It expects to use the Target
interface.
2. Target (Interface)The interface that the client expects (has a method
Request()).
3. Adaptee
The existing class that has a different interface (has a method
SpecificRequest()).
4. Adapter
This is the bridge that connects the Target and Adaptee.
It inherits the Target and uses/implements the Adaptee.
It converts the Request() call from the client to SpecificRequest() of the
adaptee.

💡 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

📷 Real-Life Examples (imagine photos):


 Travel plug adapter for a foreign country
 Card reader that connects SD cards to USB ports
 Language translator between two people speaking different languages
 USB-to-Type-C cable adapter
🧠 Why do we need Adapter?
 Sometimes we need to connect old systems with new ones
 We can’t always change old code, so we create a middle layer
 Adapters make it easy to connect and communicate between
mismatched systems

✔ Features of Adapter Pattern:


 It converts one interface into another
 It allows reusability of existing code
 It makes systems more flexible and compatible
 Acts as a middle connector

📌 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

💬 Summary in Simple Words:


Adapter means a connector between two different things. It helps them work
together without changing their original structure. It is used to match
interfaces and make systems work smoothly.

🔄 3. State Design Pattern (Behavioral Pattern)


📌 What is it?
The State Design Pattern allows an object to change its behavior when its
internal state changes. It appears as if the object has changed its class, but in
reality, only its state has changed.

Based on the Image You Uploaded (Fig. 5.5.7):


📦 Components:
1. Context
The main class that contains the current state of the object.
It forwards the request to the current state object.
Example method: Request() → delegates to state.Handle().

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

📷 Real-Life Examples (imagine photos):


 Traffic signal – Red (stop), Yellow (wait), Green (go)
 Music player – Play, Pause, Stop
 ATM machine – Insert Card, Enter PIN, Withdraw Money, Exit
 Fan – Different speeds based on knob position

🧠 Why do we need State Pattern?


 Objects with multiple modes or behaviors need different handling
 Helps to separate each state’s logic in a clear way
 Easy to add or change states later

✔ Features of State Pattern:


 Object behavior changes when state changes
 Each state has its own class or logic
 Reduces complexity in the main class
 Easy to maintain and expand

📌 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

💬 Summary in Simple Words:


State pattern is useful when an object has many behaviors depending on its
condition. Like a traffic signal or fan, the object changes its action when the
state changes. It keeps the code clean and divides behavior into small parts.

✅ Final Recap Table (in Simple Words):


Real-Life
Pattern Type Key Idea Why Use It
Example
Only one object in One printer, One Save memory,
Singleton Creational
the program president centralized control
Make two different Connect
Plug adapter,
Adapter Structural things work mismatched
Translator
together systems
Change behavior Traffic signal, Clean handling of
State Behavioral
based on state Fan, Music player multiple states

**************************************************
What are design patterns?? advantages of using design
pattern?

📌 1. What are Design Patterns?

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:

 Design patterns are tried and tested solutions.

 They help make software flexible, reusable, and easier to understand.

 They are not code but ideas or methods.

⭐ 2. Advantages of Using Design Patterns

Here are some simple benefits of using design patterns:

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.

Developers understand patterns easily, so they can talk and plan


✅ Better Communication
better.

✅ 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

📌 1. What is High Cohesion?


Cohesion means how closely the tasks performed by a class or module are
related to each other.
✅ High Cohesion:
 A class should do only one type of job.
 All its functions should be closely related to that job.
 The class becomes easy to understand, test, and maintain.
💡 Simple Example:
If a Salesman class handles only sales-related tasks like visiting customers,
giving product info, and taking orders — that is high cohesion.
But if the same Salesman class also handles printing invoices, saving data to
the database, or sending emails, it is doing too many unrelated jobs — that’s
low cohesion.

📌 2. What is Low Coupling?


Coupling means how much one class depends on another.
✅ Low Coupling:
 Classes are independent from each other.
 One class can be changed without affecting the other.
 It makes the system more flexible and easy to maintain.
💡 Simple Example:
If the Salesman class works on its own and doesn’t directly depend on how
data is saved or how reports are printed, it has low coupling.
But if the Salesman class is tightly connected to database logic or invoice
generation, then it’s highly coupled, which is bad.

📘 3. What is Pure Fabrication?


Pure Fabrication is an artificial (made-up) class that we create to achieve high
cohesion and low coupling.
It doesn’t represent any real-world object, but we still make it to handle
technical responsibilities, like saving data, printing reports, logging activities,
etc.

💬 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)

🧾 4. Salesman Scenario with Pure Fabrication


🧍 Real-World Object: Salesman
Let’s say we have a Salesman class.
The Salesman is responsible for:
 Meeting customers
 Taking orders
 Explaining products
So far, this is good — all tasks are sales-related (✅ high cohesion).

❌ 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

✅ Solution: Use Pure Fabrication


We create two separate fabricated (technical) classes:
1. DataSaver – handles saving data to the database
2. ReportPrinter – handles printing sales reports
Now:
 The Salesman class only does sales work → ✅ High Cohesion
 It doesn’t depend on how data is saved or printed → ✅ Low Coupling
The helper classes DataSaver and ReportPrinter are pure fabrication, created
just to organize the system better.

⭐ 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.

📘 What Are the Three Types of Design Patterns?


Design patterns are divided into three main types:
1. Creational Patterns
2. Structural Patterns
3. Behavioral Patterns
Let’s understand each one in simple words with easy examples.

1️⃣ Creational Design Patterns


📌 What It Means:
Creational patterns deal with how objects are created in a flexible and smart
way.
Instead of creating objects directly using new, these patterns give us ways to
control object creation depending on the situation.
✅ Simple Example:
Imagine you are ordering pizza. Sometimes you want to make it yourself,
sometimes order from a restaurant, or get it frozen. These are different ways of
creating pizza — that’s what creational patterns manage.
🎯 Purpose:
 To control how and when objects are created
 To make object creation simple and consistent
🧩 Common Patterns:
 Singleton
 Factory Method
 Builder
 Prototype

2️⃣ Structural Design Patterns


📌 What It Means:
Structural patterns are about how different parts of a system (classes and
objects) are connected or arranged.
They help us create strong and flexible relationships between components.
✅ Simple Example:
Imagine building a house with blocks. You arrange the blocks in a smart way to
build a strong structure. Similarly, structural patterns help arrange software
parts in a proper structure.
🎯 Purpose:
 To combine or arrange objects to form larger structures
 To reduce complexity by keeping relationships clean
🧩 Common Patterns:
 Adapter
 Decorator
 Facade
 Composite

3️⃣ Behavioral Design Patterns


📌 What It Means:
Behavioral patterns focus on how objects communicate and interact with each
other.
They define how responsibilities are shared between objects, and how they
respond to actions.
✅ Simple Example:
Think about a team working on a group project. Each member has a role and
they coordinate to complete the task. Behavioral patterns help manage such
interactions in software.
🎯 Purpose:
 To manage communication between objects
 To improve the flexibility of interaction
🧩 Common Patterns:
 Strategy
 Observer
 State
 Command

📝 Final Summary Table:


Type Focus Area Simple Meaning Example Pattern
Smart ways to create Singleton,
✅ Creational Object Creation
objects Factory
Object
✅ Structural How objects fit together Adapter, Facade
Arrangement
How objects talk to each
✅ Behavioral Object Interaction Strategy, State
other

**************************************************
Draw the Strategy pattern for Robotics application and
explain in brief.[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.

**************************************************
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.

Factory Pattern ASCII Diagram


+-----------------+
| Client | ← (Person placing the order)
+-----------------+
|
v
+------------------------+
| Factory Class | ← (Decides which object to create)
+------------------------+
|
+------------+------------+
| | |
v v v
+--------+ +--------+ +--------+
| Car | | Bike | | Truck |
+--------+ +--------+ +--------+
💬 What happens here:
 The Client asks the Factory to give a vehicle.
 The Factory checks the type (Car/Bike/Truck) and creates the correct
object.
 The Client does not directly create vehicles — it just uses the Factory.

SQL Table Design Based on Factory Pattern


Now let's imagine a Product Factory that creates different types of products.
We can represent this using SQL tables and an object factory approach.
📦 Main Product Table (All types)
+----------------------------+
| Product |
+---------------------------+
| ProductID (PK) |
| Name |
| Type (Electronics, |
| Furniture, etc.) |
| Price |
+---------------------------+

✅ 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?

📘 What is a Behavioral Design Pattern?


Behavioral design patterns explain how objects communicate and work
together.
They focus on how objects behave and how they interact with each other to
perform tasks.

💡 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.

🎯 Purpose of Behavioral Patterns:


 Define how objects share responsibilities.
 Help improve communication between objects.
 Make the program more flexible and easier to maintain.

📘 What is the Iterator Pattern?


The Iterator Pattern is a behavioral pattern that helps us go through (traverse)
a collection of items, like a list, without worrying about how the list is
implemented.

💡 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.

🎯 How Iterator Pattern Solves the Traversal Problem:


 It hides the details of how the list or collection is organized.
 It provides a simple way to access each item one by one.
 It allows you to use the same method to traverse any type of list (like
arrays, linked lists, trees).
 It makes your code cleaner and easier to maintain because you don’t
mix traversal logic with the collection itself.

📝 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.

📌 4 Important GRASP Principles (Explained in Detail)

✅ 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.

📝 Final Summary (for exam writing):


GRASP is a set of design principles that help in assigning responsibilities to
classes and objects in object-oriented programming. It improves the structure,
readability, and maintainability of software.
The four important GRASP principles are:
1. Information Expert – Give the job to the class that has the required data.
2. Creator – A class should create objects that it uses or contains.
3. Low Coupling – Keep classes independent and loosely connected.
4. High Cohesion – Each class should have a focused and related set of
tasks.

You might also like