Java interfaces PowerPoint PPT Presentation

presentation player overlay
1 / 13
About This Presentation
Transcript and Presenter's Notes

Title: Java interfaces


1
Java interfaces
  • What vs How

2
Example List interface
  • public interface List
  • public Iterator iterator()
  • public Object get( int index )
  • public add( Object o )

3
Examples
  • List l new List () //Wont work
  • List l new ArrayList ()
  • List l new LinkedList ()

4
In a nutshell
  • Code
  • A class with only methods, methods which have
    no body.
  • Cannot be instantiated
  • A class implements an interface
  • Concept
  • A contract If you implement the interface, you
    must implement the methods defined by it.
  • Reward Other classes can treat you as if you are
    an instance of the interface.
  • Interface What. Implementation How.

5
Interfaces Overview
Implementation 1
Application
Interface
Implementation 2
6
A Student Register system
  • I want to be able to create, retrieve, update and
    delete (CRUD) Students.
  • The contract
  • A method which takes a Student object and returns
    its new id in the registery.
  • A method which takes an id and returns a
    corresponding Student object from the registry.
  • A method which takes an id and a Student object
    and updates the corresponding student data in the
    registry.
  • A methods which takes an id, removes the
    corresponding student data and returns a Student
    object with the removed data.

7
Example StudentRegister
  • public interface StudentRegister
  • int addStudent( Student s )
  • Student getStudent( int id )
  • void updateStudent( int id, Student s )
  • Student removeStudent( int id )
  • //JavaDocs!

8
Implementation
  • File system?
  • Database?
  • Memory?
  • Web service?

9
Example FileStudentRegister
  • public FileStudentRegister
  • implements StudentRegister
  • private File file
  • public FileStudentRegister ( String file )
  • //Setup file
  • public int addStudent( Student s )
  • //Store Student data to file
  • public Student getStudent( int id )
  • //Get Student data from file
  • public void updateStudent( int id, Student s
    )
  • //Update student data in file
  • public Student removeStudent( int id )
  • //Remove student data from file

10
Example DatabaseStudentRegister
  • public DatabaseStudentRegister
  • implements StudentRegister
  • private JDBC db .
  • public FileStudentRegister ( String host,
    String user, String passwd, String db )
  • //Setup db
  • public int addStudent( Student s )
  • //Store Student data in database
  • public Student getStudent( int id )
  • //Get Student data from database
  • public void updateStudent( int id, Student s
    )
  • //Update student data in database
  • public Student removeStudent( int id )
  • //Remove student data from database

11
Example StudentApplication
  • public class StudentApplication
  • StudentRegister register new
  • FileStudentRegister ( myFile )

12
Example StudentApplication 2
  • public class StudentApplication
  • StudentRegister register new
  • DatabaseStudentRegister (
  • host, user, passwd, db )

13
Where to choose the implementation?
  • Hard code it into the application (highly
    coupled)
  • Let a controlling class choose the implementation
    for the application. (Better, but still coupled)
  • Use a factory class or a registry (Need to
    recompile).
  • Use a configuration file (even better ?).
Write a Comment
User Comments (0)
About PowerShow.com