From 5bc1e7d75e3b4c52968771bb22f910eafd409ab3 Mon Sep 17 00:00:00 2001 From: prasanthVijayy Date: Fri, 18 Oct 2024 14:08:29 +0530 Subject: [PATCH] organization structure - Composite Pattern --- .../organization-structure-management.md | 52 +++++++++++++++ .../organization-structure-management/main.js | 23 +++++++ .../organization.js | 18 ++++++ .../organizationStructure.js | 63 +++++++++++++++++++ .../reporting.js | 13 ++++ 5 files changed, 169 insertions(+) create mode 100644 Learning_2.0/Problems/Structural-pattern-problems/organization-structure-management.md create mode 100644 Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/main.js create mode 100644 Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organization.js create mode 100644 Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organizationStructure.js create mode 100644 Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/reporting.js diff --git a/Learning_2.0/Problems/Structural-pattern-problems/organization-structure-management.md b/Learning_2.0/Problems/Structural-pattern-problems/organization-structure-management.md new file mode 100644 index 0000000..c3c45c7 --- /dev/null +++ b/Learning_2.0/Problems/Structural-pattern-problems/organization-structure-management.md @@ -0,0 +1,52 @@ +# Composite Pattern: Advanced Organization Structure Management System + +## Overview + +This project implements an **Advanced Organization Structure Management System** using the **Composite Pattern**, allowing for a flexible and hierarchical representation of employees, teams, and departments. The system supports role-based access control, task management, and detailed reporting functionalities, making it suitable for complex organizational needs. + +## Features + +- **Hierarchical Structure**: Create a tree structure where employees can belong to teams or departments, enabling complex organizational hierarchies. +- **Role-Based Access Control**: Assign different roles (e.g., Manager, Employee, Admin) to users to control access to certain functionalities within the system. +- **Task Assignment**: Allow managers to assign tasks to employees or teams, track progress, and manage deadlines. +- **Detailed Reporting**: Generate reports on employee performance, departmental achievements, and overall organizational statistics. +- **Uniform Interface**: Provide a common interface for both individual employees and groups, allowing for consistent operations such as getting total salary, displaying employee details, or calculating task completion rates. + +## Internal Structure + +The project consists of the following components: + +1. **Component Interface** + + - Defines the common methods for both `Employee` and `Department`, ensuring that both can be treated uniformly. + +2. **Leaf Class (Employee)** + + - Represents individual employees in the organization. Implements methods for salary calculation, displaying details, and task management (e.g., adding, updating tasks). + +3. **Composite Class (Department)** + + - Represents a group of employees and/or other departments. Implements methods to manage its child components (employees or sub-departments) and handles task assignment at the departmental level. + +4. **Role Class** + + - Defines the different roles within the organization, including permissions associated with each role. + +5. **Reporting Class** + + - Responsible for generating reports based on organizational data, employee performance metrics, and task completion statistics. + +6. **Client Code** + - Demonstrates how to create the organization structure, adding employees and departments, assigning tasks, and generating reports. + +## How to Use + +1. **Create Employees**: Instantiate `Employee` objects with necessary details (name, position, salary, role). +2. **Create Departments**: Instantiate `Department` objects, which can contain employees or other departments. +3. **Add Employees to Departments**: Use methods to add employees to departments or to add sub-departments. +4. **Assign Tasks**: Managers can assign tasks to employees or teams, track their status, and set deadlines. +5. **Generate Reports**: Call reporting methods to generate insights into employee performance, departmental achievements, and overall statistics. + +## Solution + +You can find the solution to this problem in the [solution folder](/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/). diff --git a/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/main.js b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/main.js new file mode 100644 index 0000000..4e70edf --- /dev/null +++ b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/main.js @@ -0,0 +1,23 @@ +// main.js +const {Employee, Department} = require("./organizationStructure"); +const Reporting = require("./reporting"); + +// Create Employees +const emp1 = new Employee("Alice", "Developer", 60000, "Employee"); +const emp2 = new Employee("Bob", "Manager", 80000, "Manager"); + +// Create Department +const devDepartment = new Department("Development"); + +// Add Employees to Department +devDepartment.add(emp1); +devDepartment.add(emp2); + +// Assign Tasks +emp1.assignTask("Complete project A"); +emp2.assignTask("Manage team meetings"); + +// Generate Reports +Reporting.generateReport(devDepartment); +Reporting.generateEmployeeReport(emp1); +Reporting.generateEmployeeReport(emp2); diff --git a/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organization.js b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organization.js new file mode 100644 index 0000000..4aba782 --- /dev/null +++ b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organization.js @@ -0,0 +1,18 @@ +class Organization { + getDetails() { + throw new Error("Method getDetails() must be implemented"); + } + add(component) { + throw new Error("Method add() must be implemented"); + } + + remove(component) { + throw new Error("Method remove() must be implemented"); + } + + assignTask(task) { + throw new Error("Method assignTask() must be implemented"); + } +} + +module.exports = Organization; diff --git a/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organizationStructure.js b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organizationStructure.js new file mode 100644 index 0000000..0e41939 --- /dev/null +++ b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/organizationStructure.js @@ -0,0 +1,63 @@ +// organizationStructure.js +const Organization = require("./organization"); + +class Employee extends Organization { + constructor(name, position, salary, role) { + super(); + this.name = name; + this.position = position; + this.salary = salary; + this.role = role; + this.tasks = []; + } + + getDetails() { + return `\n Name: ${this.name}, \n -Position: ${this.position}, \n -Salary: ${this.salary}, \n - Role: ${this.role}`; + } + + assignTask(task) { + this.tasks.push(task); + } + + showTasks() { + return this.tasks; + } + + showSalary() { + return this.salary; + } +} + +class Department extends Organization { + constructor(name) { + super(); + this.name = name; + this.employees = []; + } + + getDetails() { + const details = `Department: ${this.name} \n Employees:`; + return details + this.employees.map((emp) => emp.getDetails()).join(""); + } + + add(component) { + if (component instanceof Organization) { + this.employees.push(component); + } + } + + remove(component) { + this.employees = this.employees.filter((emp) => emp !== component); + } + + assignTask(task) { + this.employees.forEach((emp) => { + emp.assignTask(task); + }); + } +} + +module.exports = { + Employee, + Department, +}; diff --git a/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/reporting.js b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/reporting.js new file mode 100644 index 0000000..6da841f --- /dev/null +++ b/Learning_2.0/Solutions/Structural-pattern-solutions/organization-structure-management/reporting.js @@ -0,0 +1,13 @@ +// reporting.js +class Reporting { + static generateReport(department) { + console.log(department.getDetails()); + } + + static generateEmployeeReport(employee) { + console.log(employee.getDetails()); + console.log("Tasks assigned:", employee.showTasks()); + } +} + +module.exports = Reporting;