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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Coding to an interface

  • 20-08-2016 10: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 11,264 ✭✭✭✭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, Registered Users 2 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