UNIT - 4
1. Principles of Good Design
A. Cohesion
Definition:
Cohesion refers to how closely the tasks performed by a single module are related.
High cohesion means a module focuses on a single, well-defined task.
Types of Cohesion (from worst to best):
1. Coincidental – No meaningful relationship between parts.
2. Logical – Grouped by similar functions.
3. Temporal – Related by time of execution.
4. Procedural – Related by sequence of actions.
5. Communicational – Operate on the same data.
6. Functional – Perform a single well-defined task.
7. Informational – Each function performs on its own data.
Example:
// High Cohesion
float calculateArea(float radius) {
return 3.14 * radius * radius;
}
This function performs only one specific task—calculating area.
B. Coupling
Definition:
Coupling refers to the degree of interdependence between modules.
Low coupling = better modularity.
Types of Coupling (from worst to best):
1. Content Coupling – One module modifies another’s code/data directly.
2. Common Coupling – Shared global data.
3. Control Coupling – Pass control information.
4. Stamp Coupling – Pass entire data structures unnecessarily.
5. Data Coupling – Pass only necessary data (ideal case).
Example:
// Low Coupling
int sum(int a, int b) {
return a + b;
}
Void displaySum () {
int result = sum(3, 4);
printf ("%d", result);
Here, displaySum() just calls sum() — no shared variables or dependencies.
C. Modularity
Definition:
Dividing a software system into smaller, manageable modules where each handles a
specific function.
Benefits:
o Easier maintenance and testing
o Code reusability
o Parallel development possible
Example (Concept Diagram):
+-------------------------+
| Student Management |
+-------------------------+
| Student Module |
| Course Module |
| Exam Module |
| Result Module |
+-------------------------+
2. Unified Modeling Language (UML)
A. Class Diagram
Purpose: Shows classes, attributes, methods, and relationships.
Example:
+---------------------+
| Student |
+---------------------+
| - name: String |
| - rollNo: int |
+---------------------+
| + register() |
| + attendExam() |
+---------------------+
|
| 1..*
|
+---------------------+
| Course |
+---------------------+
| - courseName: String|
| - courseCode: int |
+---------------------+
| + enrollStudent() |
+---------------------+
B. Sequence Diagram
Purpose: Describes interaction between objects in a time sequence.
Example (Student enrolling in a course):
Student → Course → Database
| | |
| enroll() | |
|----------->| |
| | saveData() |
| |----------->|
| | <-----------|
| <-----------|
C. Activity Diagram
Purpose: Represents workflow or operations step-by-step.
Example (Online Payment Process):
[Start]
↓
Enter Details
↓
Verify Payment Info
┌───────────────┐
│Payment Valid? │
└─────┬─────────┘
Yes ↓ No ↓
Process Payment Display Error
↓
Send Confirmation
↓
[End]
3. Design Patterns
A. Creational Patterns
Deal with object creation mechanisms.
Examples:
o Singleton: Ensures only one instance of a class exists.
o class Singleton {
o private static Singleton instance = null;
o private Singleton() {}
o public static Singleton getInstance() {
o if (instance == null)
o instance = new Singleton();
o return instance;
o }
o }
o Factory: Creates objects without specifying exact class type.
o Builder: Constructs complex objects step by step.
B. Structural Patterns
Deal with object composition and relationships.
Examples:
o Adapter: Allows incompatible interfaces to work together.
o Decorator: Adds new functionality to existing objects dynamically.
o Composite: Treats group of objects as a single instance.
Example Diagram (Adapter):
Client --> Adapter --> Adaptee
C. Behavioral Patterns
Focus on communication between objects.
Examples:
o Observer: One-to-many dependency between objects.
o Strategy: Defines a family of algorithms and makes them interchangeable.
o Command: Encapsulates a request as an object.
Example (Observer Pattern):
+------------------+
| Subject |
|------------------|
| attach() |
| detach() |
| notify() |
+------------------+
|
| observes
v
+------------------+
| Observer |
|------------------|
| update() |
+------------------+
4. Software Architecture Styles
A. Client-Server Architecture
Concept:
System divided into two parts:
o Client: Sends requests
o Server: Processes requests and sends back responses
Diagram:
+--------+ +---------+
| Client | <-----> | Server |
+--------+ +---------+
Example: Web browser (client) communicating with web server.
B. Model-View-Controller (MVC)
Concept:
Separates application into three interconnected components:
o Model: Manages data and logic
o View: Handles UI
o Controller: Handles user input and coordinates Model & View
Diagram:
+-----------+ +-----------+ +-----------+
| Model | <--> | Controller| <--> | View |
+-----------+ +-----------+ +-----------+
Example: Django, Spring MVC, Angular.
C. Microservices Architecture
Concept:
System is composed of small, independent services communicating via APIs.
Diagram:
+-------------+ +-------------+ +-------------+
| User Service| | Order Service| | Payment Serv|
+-------------+ +-------------+ +-------------+
| | |
+----------------------------------+
REST API Gateway
Advantages:
o Independent deployment
o Scalability
o Fault isolation
Summary Table
Concept Focus Example
Cohesion Task specialization Single-purpose function
Coupling Module independence Minimal shared data
Modularity Separation of concerns Student/Course modules
UML Class Diagram Static structure Student–Course model
UML Sequence Diagram Object interaction Enrollment flow
UML Activity Diagram Workflow Payment process
Creational Pattern Object creation Singleton
Structural Pattern Object composition Adapter
Behavioural Pattern Communication Observer