Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Coding to an interface

Options
  • 20-08-2016 11:43am
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    If I want to use a Java linked list, to make use of the queueing functionality, I would do this:
    LinkedList<String> myList = new LinkedList<String>();
            myList.peek();
    

    But if I want code to an interface, I'd prefer to use:
    List<String> myList = new LinkedList<String>();
    

    Is the 'code to an interface' rule actually 'code to an interface - when possible'?


Comments

  • Registered Users Posts: 6,236 ✭✭✭Idleater


    Unless you specifically want to use the peek() method of linkedlist, I see no reason not to code to the list interface.

    Personally I would code to the most generic interface/abstraction possible as a guide and work down the implementation tree when needed.


  • Registered Users Posts: 11 SionnachRoe


    I would consider the rule to be 'Code to the interface'.

    If there is a need to use a method in that is not in the interface then most, the interface chosen is incorrect. In the above example, if you need to use the queuing functionality then maybe

    Queue<String> myQueue = new LinkedList<String>();
    myList.peek();

    would be a better choice.

    The core concept behind 'Code to the interface' is that we should consider our code in terms of what is does, rather than how it does it. If for example, we think that we wish to work with a queue, then we define our object as a queue which allows/forces to use only queue-like functionality with it. If we find that we are, for example, casting it to a list to do something, then we should re-examine if we really want to use a queue.


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    Coding to an interface makes it much easier to change implementation later, especially when you are using a dependency injection framework. You would only need to change it once for you whole app in the module where you provide the implementation. Your unit tests should also run against the interface.


  • Registered Users Posts: 51 ✭✭happycoach


    I would consider the rule to be 'Code to the interface'.

    If there is a need to use a method in that is not in the interface then most, the interface chosen is incorrect. In the above example, if you need to use the queuing functionality then maybe

    Queue<String> myQueue = new LinkedList<String>();
    myList.peek();

    would be a better choice.

    The core concept behind 'Code to the interface' is that we should consider our code in terms of what is does, rather than how it does it. If for example, we think that we wish to work with a queue, then we define our object as a queue which allows/forces to use only queue-like functionality with it. If we find that we are, for example, casting it to a list to do something, then we should re-examine if we really want to use a queue.

    +2


Advertisement