-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle_Responsibility.java
More file actions
89 lines (74 loc) · 2.77 KB
/
Single_Responsibility.java
File metadata and controls
89 lines (74 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
______________________________________________________________________________________________________________________________________________________
Title : Demonstrating the Single Responsibility Principle (SRP)
Student : Md. Farid Hossen Rehad
Computer Science & Engineering
Discipline
From Khulna University
_______________________________________________________________________________________________________________________________________________________
*/
import java.io.FileWriter;
import java.io.IOException;
/**
* Represents a Logger responsible for logging messages to a file.
*/
class Logger {
private String logFilePath;
/**
* Constructs a Logger with the specified log file path.
*
* @param logFilePath The path to the log file.
*/
public Logger(String logFilePath) {
this.logFilePath = logFilePath;
}
/**
* Logs a message to the log file.
*
* @param message The message to log.
*/
public void log(String message) {
try (FileWriter writer = new FileWriter(logFilePath, true)) {
writer.write(message + "\n");
} catch (IOException e) {
System.err.println("Failed to write to log file: " + e.getMessage());
}
}
}
/**
* Represents a data processor responsible for processing data.
*/
class DataProcessor {
/**
* Processes the provided data.
*
* @param data The data to be processed.
*/
public void processData(String data) {
// Placeholder method for data processing
System.out.println("Processing data: " + data);
}
}
/**
* This class demonstrates the usage of Logger and DataProcessor classes.
*/
class SRPExample {
public static void main(String[] args) {
// Create an instance of Logger
Logger logger = new Logger("log.txt");
// Log a message using Logger
logger.log("Application started");
// Create an instance of DataProcessor
DataProcessor dataProcessor = new DataProcessor();
// Process data using DataProcessor
dataProcessor.processData("Sample data");
// Log another message using Logger
logger.log("Data processing completed");
}
}
/**
* In the above program, the Logger class has a single responsibility of logging messages to a file.
* The SRPExample class demonstrates the usage of Logger and DataProcessor classes without involving
* in the logging or data processing processes themselves, thus adhering to the Single Responsibility
* Principle (SRP).
*/