In mission-critical applications, the ability to switch output destinations without altering core logic is vital. This project implements a Flexible Logging System using interface-based polymorphism. By defining a universal Logger contract, the system can treat ConsoleLogger and FileLogger as interchangeable components. This architectural approach follows the Dependency Inversion Principle, where high-level modules depend on abstractions (interfaces) rather than concrete implementations.
- Unified Contract: Established the
Loggerinterface with a mandatorylog(String message)method. - Behavioral Diversity: Developed
ConsoleLoggerfor immediate debugging andFileLoggerto simulate persistent storage. - Interchangeability: Demonstrated the ability to call the same method signature on different object types via an interface reference.
- System Flexibility: Proven that new logging destinations (e.g., DatabaseLogger) can be added without modifying existing client code.
- Java 8+ (Interfaces, Polymorphism, Decoupling)
- Logger: The core interface defining the event reporting protocol.
- ConsoleLogger: Direct standard output implementation.
- FileLogger: Simulated file-system output implementation.
- LogLauncherApp: The test harness demonstrating behavioral switching.
Test message
Log written to file: Test message
Project Structure:
JavaBasics_Task_360/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── LogLauncherApp.java
│ └── logging/
│ ├── contracts/
│ │ └── Logger.java
│ └── modules/
│ ├── ConsoleLogger.java
│ └── FileLogger.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.logging.contracts.Logger;
import com.yurii.pavlenko.logging.modules.ConsoleLogger;
import com.yurii.pavlenko.logging.modules.FileLogger;
public class LogLauncherApp {
public static void main(String[] args) {
Logger consoleLogger = new ConsoleLogger();
Logger fileLogger = new FileLogger();
String testMessage = "Test message";
consoleLogger.log(testMessage);
fileLogger.log(testMessage);
}
}package com.yurii.pavlenko.logging.contracts;
public interface Logger {
void log(String message);
}package com.yurii.pavlenko.logging.modules;
import com.yurii.pavlenko.logging.contracts.Logger;
public class ConsoleLogger implements Logger {
@Override
public void log(String message) {
System.out.println(message);
}
}package com.yurii.pavlenko.logging.modules;
import com.yurii.pavlenko.logging.contracts.Logger;
public class FileLogger implements Logger {
@Override
public void log(String message) {
System.out.println("Log written to file: " + message);
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT