Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save omarkdev/93f26cbc7564152675259b8e87152af2 to your computer and use it in GitHub Desktop.
Save omarkdev/93f26cbc7564152675259b8e87152af2 to your computer and use it in GitHub Desktop.
Exemplo ANTES da refatoração do artigo Design Patterns: Null Object.
interface Logger {
warn(message: string): void;
}
class ConsoleLogger implements Logger {
warn(message: string): void {
console.warn(message);
}
}
class LoggerFactory {
factory(env: string): Logger | null {
if (env === 'development') {
return new ConsoleLogger();
}
return null;
}
}
class Client {
execute() {
const logger = (new LoggerFactory()).factory('production');
if (logger !== null) {
logger.warn('It\'s on fire, bicho');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment