Search code examples
javadecoratortype-mismatch

Doing a decorator Java program. Why does it show a mismatch?


I have a question regarding a program I'm doing for school. I have to do the following:

Write a Java program that uses decorator classes to add capabilities to employees. In a typical company, an employee will be asked to perform a number of duties, such as Department Head, Safety Coordinator, Recruiter, or Community Liason. You should have additional ones besides those. Your Java program will create Employees and then decorate these employees at runtime.

Create an abstract class named Employee, with last name and description fields, and a getDescription() method. Create a concrete class named SalariedEmployee that extends Employee. Create an abstract class named ResponsibilityDecorator that is able to decorate an employee and return the employee's responsibility as a string. It will have an abstract getDescription method. Create some job category classes that extend the ResponsibilityDecorator class and implement the getDescription() method.

In your main test program, create at least 5 Employee objects and pass them to the constructors of each of your decorator classes. The first employee should be you so use your last name. Then, print each Employee by calling its getDescription() method. All employees should not have the same number of responsibilities. The program's output should look something like this for each :

[LastName]: Manager, Recruiter, CommunityLiaison, ProductionDesigner

Here's what I've done so far:

Employee.java:

import java.util.*;

public abstract class Employee 
{
    String lastName, description;

    public String getDescription()
    {
        return description;
    }

}

SalariedEmployee.java:

import java.util.*;

public abstract class SalariedEmployee extends Employee
{
    String lastName, description;

    public abstract String getDescription();
}

ResponsibilityDecorator.java:

import java.util.*;

public abstract class ResponsibilityDecorator
{
    public String getDescription()
    {
        return employee.getDescription();
    }

    Employee employee;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public boolean equals(Object obj) {
        return employee.equals(obj);
    }

    public int hashCode() {
        return employee.hashCode();
    }

    public String toString() {
        return employee.toString();
    }

    public ResponsibilityDecorator(Employee employee) {
        super();
        this.employee = employee;
    }

    public ResponsibilityDecorator() {
        super();
        // TODO Auto-generated constructor stub
    }
}

Rodriguez.java (first employee class)

import java.util.*;

public abstract class Rodriguez extends ResponsibilityDecorator 
{
    Employee employee1;

    String lastName = "Rodriguez";
    String description = "Tech Support";

    public Rodriguez (Employee employee1)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Doe.java (second employee class):

import java.util.*;

public abstract class Doe extends ResponsibilityDecorator 
{
    Employee employee2;

    String lastName = "Doe";
    String description = "Security Guard, Police Officer";

    public Doe (Employee employee2)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Jill.java (third employee object):

import java.util.*;

public abstract class Doe extends ResponsibilityDecorator 
{
    Employee employee2;

    String lastName = "Doe";
    String description = "Security Guard, Police Officer";

    public Doe (Employee employee2)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Maria.java (fourth employee class):

import java.util.*;

public abstract class Maria extends ResponsibilityDecorator
{
    Employee employee4;

    String lastName = "Maria";
    String description = "Receptionist, Valet, Cashier, Restock";

    public Maria (Employee employee4)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

James.java (fifth employee class):

import java.util.*;

public abstract class James extends ResponsibilityDecorator
{
    Employee employee5;

    String lastName = "James";
    String description = "Manager, CEO, Economy, President, Analytics";

    public James (Employee employee5)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Test.java:

import java.util.*;

public class Test 
{

    public static void main(String[] args)
    {
        Employee employee1 = new Rodriguez();
        System.out.println("Rodriguez:" + 
                employee1.getDescription());

        Employee employee2 = new Doe();
        System.out.println("Doe" + 
                employee2.getDescription());

        Employee employee3 = new Jill();
        System.out.println("Jill:" + 
                employee3.getDescription());

        Employee employee4 = new Maria();
        System.out.println("Maria:" + 
                employee4.getDescription());

        Employee employee5 = new James();
        System.out.println("James:" + 
                employee5.getDescription());
    }
}

For some reason, it shows a type mismatch error. I 've been searching around the Internet and I even asked some people. I tried many fixes, but they just create another error. I don't know how to fix this problem.


Solution

  • There seem to be several problems here. Let's go through the assignment text:

    Create an abstract class named Employee, with last name and description fields, and a getDescription() method.

    This looks OK

    Create a concrete class named SalariedEmployee that extends Employee.

    You made SalariedEmployee an abstract class. Remove abstract and implement the getDescription method.

    Create an abstract class named ResponsibilityDecorator that is able to decorate an employee and return the employee's responsibility as a string. It will have an abstract getDescription method.

    According to the decorator pattern I'm familiar with, ResponsibilityDecorator should extend Employee if it's supposed to "decorate an employee" - have you been taught something else?

    From the description of the task I would expect to see:

    abstract class ResponsibilityDecorator extends Employee {
        Employee employee;
        ResponsibilityDecorator(Employee e) { this.employee = e; }
        abstract String getDescription();
    }
    

    Create some job category classes that extend the ResponsibilityDecorator class and implement the getDescription() method.

    You have not done this, unless you consider Maria, Rodriguez etc "job categories."

    A "Manager" job category could look like this:

    class Manager extends ResponsibilityDecorator {
        Manager(Employee employee) { super(employee); }
        String getDescription() { return employee.getDescription() + " Manager"; }
    }
    

    In your main test program, create at least 5 Employee objects

    Perhaps you confused this step the previous one and created 5 employee classes (Maria, Rodriguez, ...)?


    Do note that the solution to this exercise would leave professional developers scratching their head, because this is not a reasonable way to represent job categories or responsibilities in a program.