Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Dependency Inversion Principle/Models/Employee.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Employee.h"

Employee::Employee(const string& name, const string& address)
: name(name), address(address) {}

string Employee::getName() const {
return name;
}

string Employee::getAddress() const {
return address;
}

void Employee::setName(const string& name) {
this->name = name;
}

void Employee::setAddress(const string& address) {
this->address = address;
}
23 changes: 23 additions & 0 deletions Dependency Inversion Principle/Models/Employee.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>

using namespace std;

class Employee {
public:
Employee(const string& name, const string& address);

// Getters and setters
string getName() const;
string getAddress() const;
void setName(const string& name);
void setAddress(const string& address);

private:
string name;
string address;
};

#endif // EMPLOYEE_H
8 changes: 8 additions & 0 deletions Dependency Inversion Principle/Repository/Database.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "Database.h"
#include <iostream>

using namespace std;

void Database::save(const Employee& employee) {
cout << "Saving employee " << employee.getName() << " to the database." << endl;
}
13 changes: 13 additions & 0 deletions Dependency Inversion Principle/Repository/Database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DATABASE_H
#define DATABASE_H

#include "IDatabase.h"

using namespace std;

class Database : public IDatabase {
public:
void save(const Employee& employee) override;
};

#endif // DATABASE_H
14 changes: 14 additions & 0 deletions Dependency Inversion Principle/Repository/IDatabase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef IDATABASE_H
#define IDATABASE_H

#include "Employee.h"

using namespace std;

class IDatabase {
public:
virtual void save(const Employee& employee) = 0;
virtual ~IDatabase() = default;
};

#endif // IDATABASE_H
8 changes: 8 additions & 0 deletions Dependency Inversion Principle/Services/EmployeeService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "EmployeeService.h"

EmployeeService::EmployeeService(IDatabase* database)
: database(database) {}

void EmployeeService::saveEmployee(const Employee& employee) {
database->save(employee);
}
17 changes: 17 additions & 0 deletions Dependency Inversion Principle/Services/EmployeeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef EMPLOYEESERVICE_H
#define EMPLOYEESERVICE_H

#include "IDatabase.h"

using namespace std;

class EmployeeService {
public:
EmployeeService(IDatabase* database);
void saveEmployee(const Employee& employee);

private:
IDatabase* database;
};

#endif // EMPLOYEESERVICE_H