-2

Requirement create different type of decorator and used it with user class so we can add specific details of different type of user based on decorator and in my code I have two decorator one is students decorator and second is teacher decorator

Question is my code follow design principles and more specifically class decorator design pattern

code

// Student decorator
function addStudentDecorator<T extends { new (...args: any[]): {} }>(target: T) {
    return class extends target {
        id!: string;
        rollNumber!: number;
        age!: number;
        type = 'student';

        constructor(...args: any[]) {
            const [name, id, rollNumber, age] = args;
            super(name);  // Pass only the name to the base class
            this.id = id;
            const student = new StudentClass(rollNumber, age);
            this.rollNumber = student.rollNumber;
            this.age = student.age;
        }
    };
}

// Teacher decorator
function addTeacherDecorator<T extends { new (...args: any[]): {} }>(target: T) {
    return class extends target {
        id!: string;
        subject!: string;
        experience!: number;
        type = 'teacher';

        constructor(...args: any[]) {
            const [name, id, subject, experience] = args;
            super(name);  // Pass only the name to the base class
            this.id = id;
            const teacher = new TeacherClass(subject, experience);
            this.subject = teacher.subject;
            this.experience = teacher.experience;
        }
    };
}

// Classes for student and teacher
class StudentClass {
    rollNumber: number;
    age: number;
    constructor(rollNumber: number, age: number) {
        this.rollNumber = rollNumber;
        this.age = age;
    }
}

class TeacherClass {
    subject: string;
    experience: number;
    constructor(subject: string, experience: number) {
        this.subject = subject;
        this.experience = experience;
    }
}

// Example for a Student
@addStudentDecorator
class UserClass {
    name: string;

    constructor(name: string, id?: string, rollNumber?: number, age?: number) {
        this.name = name;
    }
}

// Example for a Teacher
@addTeacherDecorator
class UserClassOne {
    name: string;

    constructor(name: string, id?: string, subject?: string, experience?: number) {
        this.name = name;
    }
}

// Create a student instance
let studentUser = new UserClass('Alice', 'S01', 1001, 20);
console.log(studentUser);  // This will print the student details

// Create a teacher instance
let teacherUser = new UserClassOne('Bob', 'T01', 'Math', 10);
console.log(teacherUser);  // This will print the teacher details
2
  • const student = new StudentClass(rollNumber, age); this.rollNumber = student.rollNumber; this.age = student.age;makes no sense Commented Aug 22, 2024 at 16:29
  • Please be sure to provide who it is you are quoting when you use the blockquote. If you are using that for its formatting, please stop and use standard text. Use bold and italics to emphasize words or short phrases that are particularly important, not whole paragraphs of text. Commented Aug 22, 2024 at 22:45

1 Answer 1

0

You misunderstand the decorator pattern.

First, you must have an interface. Second, you must have a class that implements that interface and encapsulates an object of that interface (adds behavior to an object without using inheritance and preserving the interface).

You can see a clear example in Typescript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.