Skip to content

Instantly share code, notes, and snippets.

@gentritabazi
Created April 23, 2024 09:40
Show Gist options
  • Save gentritabazi/d286349b2f282a83425189ce1c264cbf to your computer and use it in GitHub Desktop.
Save gentritabazi/d286349b2f282a83425189ce1c264cbf to your computer and use it in GitHub Desktop.
Logger.js
// Define the interface for different logging strategies
class LoggerStrategy {
log(message) {}
}
class ConsoleLogger extends LoggerStrategy {
log(message) {
// Code logic to log message
}
}
class FileLogger extends LoggerStrategy {
log(message) {
// Code logic to log message
}
}
class SentryLogger extends LoggerStrategy {
log(message) {
// Code logic to log message
}
}
// Context class that uses a logging strategy
class Loger {
constructor(strategy) {
this.strategy = strategy;
}
logMessage(message) {
this.strategy.log(message);
}
}
// Example usage
const fileLoggerStrategry = new FileLogger();
const logger = new Loger(fileLoggerStrategry);
logger.logMessage("Warning!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment