Search code examples
javainterfaceclientfinal

Implementing an interface provided by a Client


I have following task

public class Event {

    private final Integer id;
    private final Collection<Market> market;
    private final Boolean completed;

    public Event(Integer id,
            Collection<Market> market, Boolean completed) {
        this.id = id;
        this.market = market;
        this.completed = completed;
    }

    public Integer getId() {
        return id;
    }

    public Boolean getCompleted() {
        return completed;
    }
}

public class Market {


    private final Integer marketId;


    public Market(Integer marketId) {
        this.marketId = marketId;
    }

    public Integer getMarketId() {
        return marketId;
    }
}

public interface Client
{

    public void addEvent(Event event);

    /**
     * Update event status to completed. 
     */
    public void eventCompleted(Integer id);

    /**
     * Add market to an existing event
     */
    public void attachMarketToEvent(Integer id, Market market);

    /**
     * Remove market from an existing event
     */
    public void removeMarketFromEvent(Integer id, Market market);
}

I need to implement the client without changing the Event or Market classes. How to make it? Any ideas? I could implement it but if I change this final fields. What is expected from me? I am really surprised.


Solution

  • Event is immutable. That is, it cannot be changed once it has been created. Looking at the Client interface, the only method that seems to be posing a problem is the eventCompleted method :

        /**
         * Update event status to completed. 
         */
        public void eventCompleted(Integer id);
    

    Looks like this method expects you to update the completed flag of an Event to true which is not possible.

    The rest of the methods won't be a problem because they don't modify the fields of Event :

    1. addEvent(Event event) : This shouldn't be a problem since a new Event can always be created
    2. attachMarketToEvent(Integer id, Market market) and removeMarketFromEvent(Integer id, Market market) : These shouldn't be a problem because they will be used to add new objects to the market collection which is allowed.

    You need to go back to whoever is expecting you to implement the client and ask them : How do I update the completed field if Event is immutable?