Commit inicial

This commit is contained in:
Luiz F Picolo 2024-09-12 08:29:05 -04:00
commit 9ff4ab61d7
3 changed files with 63 additions and 0 deletions

14
ex1.js Normal file
View File

@ -0,0 +1,14 @@
class Logger {
log(message) {
console.log(`Log message: ${message}`);
}
}
function executeTask(task, logger) {
logger.log("Iniciando tarefa...");
task();
logger.log("Tarefa concluída!");
}
const logger = new Logger();
executeTask(() => console.log("Executando tarefa principal..."), logger);

29
ex2.js Normal file
View File

@ -0,0 +1,29 @@
class UserService {
constructor(database) {
this.database = database;
}
getUser(id) {
return this.database.find(user => user.id === id);
}
}
class Database {
constructor() {
this.users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
}
find(id) {
return this.users.find(id);
}
}
// Inversão de Controle a dependência (Database) é injetada no UserService
const database = new Database();
const userService = new UserService(database);
const user = userService.getUser(1);
console.log(user); // { id: 1, name: 'Alice' }

20
ex3.js Normal file
View File

@ -0,0 +1,20 @@
class Database {
query(sql) {
console.log(`Executando consulta: ${sql}`);
}
}
class UserService {
constructor(database) {
this.database = database; // Dependência injetada
}
getUser(id) {
this.database.query(`SELECT * FROM users WHERE id = ${id}`);
}
}
const database = new Database();
const userService = new UserService(database);
userService.getUser(1); // Executa com a dependência injetada