A simple FileManager application built in Java to practice and learn Low-Level Design (LLD) design patterns and principles.
This project serves as a learning tool for understanding and implementing common design patterns in object-oriented programming. It demonstrates the practical application of design patterns in a real-world scenario - managing file operations with extensibility and maintainability in mind.
- Location:
FileManagerclass andfileIOpackage - Purpose: Allows the FileManager to switch between different file I/O strategies without changing its code
- Implementation:
IFileIOStratergyinterface defines the contract for file operations (save, read, delete, find)LocalFileIOimplements the interface for local file system operationsFileManageruses composition to delegate file operations to the strategy
- Location:
EventNotifierclass - Purpose: Ensures only one instance of the event notifier exists throughout the application
- Implementation:
- Static
instancevariable holds the single instance getInstance()method provides global access to the instance- Used for centralized event management
- Static
- Location:
Notificationpackage - Purpose: Implements a publish-subscribe mechanism for file operation notifications
- Implementation:
IFileObserverinterface defines the observer contractUpdateFileObserverimplements the interface to handle notificationsEventNotifier(singleton) manages observers and notifies them of events
- Location:
fileIO/Decoratorspackage - Purpose: Allows dynamic addition of responsibilities to file I/O operations (logging, compression, encryption, etc.) without modifying existing code
- Implementation:
FileIODecoratorabstract class implementsIFileIOStratergyand holds a reference to anotherIFileIOStratergyLoggingFileIODecoratoradds logging functionality to file operationsCompressionFileIODecoratoradds compression/decompression functionality- Decorators can be stacked:
new LoggingFileIODecorator(new CompressionFileIODecorator(new LocalFileIO()))
src/
├── main/java/com/fileManager/
│ ├── App.java # Main application entry point
│ ├── FileManager/
│ │ └── FileManager.java # Main FileManager class (Strategy pattern)
│ ├── fileIO/
│ │ ├── IFileIOStratergy.java # Strategy interface
│ │ ├── LocalFileIO.java # Concrete strategy implementation
│ │ ├── FileIODecorator.java # Abstract decorator base class
│ │ └── Decorators/
│ │ ├── LoggingFileIODecorator.java # Logging decorator
│ │ └── CompressionFileIODecorator.java # Compression decorator
│ └── Notification/
│ ├── EventNotifier.java # Singleton + Subject (Observer pattern)
│ └── Observer/
│ ├── IFileObserver.java # Observer interface
│ └── UpdateFileObserver.java # Concrete observer
└── test/java/com/fileManager/
└── AppTest.java # Unit tests
- Java 17 or higher
- Maven 3.6+
-
Clone the repository (if applicable)
git clone <repository-url> cd fileManager
-
Build the project
mvn clean compile
-
Run tests
mvn test -
Package the application
mvn package
-
Run the application
java -cp target/classes com.fileManager.App
// Create a FileManager with LocalFileIO strategy
FileManager fileManager = new FileManager(new LocalFileIO());
// Register an observer for notifications
EventNotifier.getInstance().addObserver(new UpdateFileObserver());
// Perform file operations
fileManager.upload("example.txt", "Hello World".getBytes());
byte[] data = fileManager.download("example.txt");
// Using Decorator Pattern to add features dynamically
IFileIOStratergy decoratedIO = new LoggingFileIODecorator(
new CompressionFileIODecorator(
new LocalFileIO()
)
);
FileManager decoratedFileManager = new FileManager(decoratedIO);
// Now file operations will be logged AND compressed!
decoratedFileManager.upload("compressed_file.txt", "This data will be compressed".getBytes());
byte[] decompressedData = decoratedFileManager.download("compressed_file.txt");- Single Responsibility Principle: Each class has a single, well-defined responsibility
- Open/Closed Principle: The system is open for extension (new strategies, observers) but closed for modification
- Dependency Inversion: High-level modules (FileManager) don't depend on low-level modules (LocalFileIO), but both depend on abstractions (IFileIOStratergy)
- Composition over Inheritance: Uses composition to achieve flexibility (Strategy pattern)
- Add more file I/O strategies (CloudFileIO, DatabaseFileIO)
- Implement additional observers for different notification types
- Add error handling and logging
- Include more comprehensive unit tests
This is a learning project, but suggestions for improvements or additional patterns are welcome!
This project is for educational purposes only.