Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was recently given the following task, and failed, the reason given that I misunderstood the question

Create a 'LibraryItem' interface.
Create 3 concrete implementations of LibraryItem: 'Book', 'CD', 'DVD'.
Apply the visitor pattern to your data model.
Write a MaximumBorrowTimeVisitor that computes the maximum time you can borrow a LibraryItem.
Create an arbitrary instance of a data model and apply the MaximumBorrowTimeVisitor.

I thought this was simple, and yet I failed the task.

How would you solve this task?

share|improve this question

closed as not a real question by gnat, BЈовић, Walter, Glenn Nelson, Kilian Foth Jan 29 '13 at 14:24

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
what kind of task is this? Homework? –  Louis Rhys Jan 29 '13 at 6:36
    
    
You'll likely get more assistance if you post your own efforts and ask where you could improve, rather than just asking people to solve it from scratch. –  Baqueta Jan 29 '13 at 9:27
    
@lori How did you "fail" the task? Did you manage to write anything or what? –  Spoike Jan 29 '13 at 12:19

1 Answer 1

I would do it like this. What was your solution?

interface LibraryItem {
    void acceptVisitor(LibraryItemVisitor visitor);
}

class Book implements LibraryItem {
    void acceptVisitor(LibraryItemVisitor visitor) {
        visitor.visit(this)
    }
}

class CD implements LibraryItem {
    void acceptVisitor(LibraryItemVisitor visitor) {
        visitor.visit(this)
    }
}

class DVD implements LibraryItem {
    void acceptVisitor(LibraryItemVisitor visitor) {
        visitor.visit(this)
    }
}

interface LibraryItemVisitor {
    void visit(Book book);
    void visit(CD cd);
    void visit(DVD dvd);
}

class MaximumBorrowTimeVisitor implements LibraryItemVisitor {
    private int maxDays;

    int getMaxDays() {
        return maxDays;
    }

    void visit(Book book) {
        maxDays = 28;
    }

    void visit(CD cd) {
        maxDays = 14;
    }

    void visit(DVD dvd){
        maxDays = 7;
    }
}

public class Demo {
    public static main(String[] args) {
        CD myCD = new CD();
        MaximumBorrowingTimeVisitor borrowingTime = new MaximumBorrowingTimeVisitor();
        myCD.acceptVisitor(borrowingTime);

        System.out.println("max borrowing time of CD: " + borrowingTime.getMaxDays());
    }
}
share|improve this answer
    
Somehow i fell abused :-( –  jmaschad Jan 29 '13 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.