Cohesion_Coupling
Cohesion and Coupling in OOPs
Cohesion and coupling are important concepts in software engineering that describe the relationships and interactions between different components or modules of a program. In C++, cohesion and coupling play a crucial role in designing and maintaining software systems.
- Cohesion: Cohesion refers to the degree of relatedness or unity among the members of a module or class. It measures how closely the responsibilities and functionalities of a module or class are related to each other. High cohesion is desirable as it indicates that the members of a module work together towards a common purpose, leading to more maintainable and understandable code.
Types of cohesion in C++:
- Functional cohesion: Members of a module or class work together to perform a single function or task.
- Sequential cohesion: Members are organized in a sequential order, where the output of one member becomes the input for the next member.
- Communicational cohesion: Members operate on the same data or communicate closely with each other.
- Temporal cohesion: Members are called in a specific order or within a specific time frame.
- Procedural cohesion: Members are grouped based on a common procedural logic or algorithm.
Example of high cohesion:
class FileManager {
public:
void openFile(const string& filename) {
// Code to open a file
}
void readFile() {
// Code to read data from the file
}
void closeFile() {
// Code to close the file
}
};
In the above example, all the methods in the FileManager
class are closely related to file handling operations. They work together towards the common purpose of managing files, indicating high cohesion.
- Coupling: Coupling refers to the degree of interdependence between different modules or classes in a system. It measures how closely one module relies on or knows about the internals of another module. Low coupling is desirable as it promotes modularity, reusability, and maintainability of code.
Types of coupling in C++:
- Tight coupling: Modules are highly dependent on each other, making changes in one module impact others significantly.
- Loose coupling: Modules have minimal or no knowledge of the internal workings of other modules, reducing the impact of changes.
- Content coupling: Modules share internal data directly, leading to high dependency.
- External coupling: Modules depend on externally defined data or interfaces.
- Control coupling: Modules control the behavior of other modules through parameters or flags.
Example of low coupling:
class Database {
public:
void insertData(const Data& data) {
// Code to insert data into the database
}
};
class Logger {
public:
void logMessage(const string& message) {
// Code to log a message
}
};
class DataManager {
public:
DataManager(Database& db, Logger& logger)
: database(db), logger(logger) {}
void processData(const Data& data) {
// Process the data
database.insertData(data);
logger.logMessage("Data processed successfully");
}
private:
Database& database;
Logger& logger;
};
In the above example, the DataManager
class depends on the Database
and Logger
classes through their interfaces (references). It has no knowledge of the internal implementation details of the other classes, promoting loose coupling.
By considering both cohesion and coupling, you can design software systems that are more modular, maintainable, and extensible, leading to better code quality and easier development and maintenance.