Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces. Joseph Sant Sheridan College.. Objectives Discuss reasoning behind interfaces using a scenario. Interfaces and Contracts. Syntax examples.

Similar presentations


Presentation on theme: "Interfaces. Joseph Sant Sheridan College.. Objectives Discuss reasoning behind interfaces using a scenario. Interfaces and Contracts. Syntax examples."— Presentation transcript:

1 Interfaces. Joseph Sant Sheridan College.

2 Objectives Discuss reasoning behind interfaces using a scenario. Interfaces and Contracts. Syntax examples. Usage in Java.

3 Reasoning. Large Java projects often involve hundreds of classes with many people working on different parts. Often one team is assigned to build a tool (e.g. a class) for other teams(s) classes to call. How do we make sure ahead of time that the two sets of code will work together.

4 Scenario – Acme Contacts. Acme Software is building a best of breed contact manager to be run on several different platforms. It will try to optimize both the speed of the product and reduce total software cost by: Building general purpose code for all contact management logic. Using the best-of-breed storage solutions for each platform.

5 Scenario – Acme Contacts. We looked at how the business logic has to interact with data storage from a high level. Initialize the database the first time through. Store (replace) a contact object. Retrieve a contact object based on an employee id string. Delete a contact object based on an employee id string. Retrieve a full list of contacts

6 PRESENTATION init() list() store() delete() Logic Data retrieve()

7 Problem! We have different specialists in the different databases (e.g. MySQL, Oracle, SQLServer) that will be coding the specific DataStore code. From experience we know SOME WILL GET IT WRONG.

8 Solution! Force a contract on the implementers of our DataStore classes.

9 Contracts. Contracts specify conditions that must be met. Contracts typically have a penalty for non-compliance.

10 Interface = A Contract Specifies which methods must be implemented. Provides a penalty that a Class that promises to implement the interface and doesn’t do so properly will not compile.

11 Specifying Interfaces. public interface IDataStore { void init(); void delete(String contactID); void store(Contact contact); Contact retrieve( String contactID); ContactList list(); } Interface only describe the public interface. No need to specify abstract or provide An implementation. It is assumed that Required methods Will be public

12 Writing code that uses Interfaces. public class MySQLDataStore implements IDataStore { public void init(){ //code to implement MySQL initialization. } public void delete(String contactID){ //code to implement MySQL delete. } public void store(Contact contact ); //code to implement MySQL insert or replace. } public Contact retrieve( String contactID); //code to implement MySQL query. } public ContactList list(); //code to implement MySQL listing of a table. } public void extraMethod(){ // code performs task not specified in Interface. } implements keyword is a promise to implement all the methods in IDataStore Class must code all methods in IDataStore Class can have more methods than required.

13 Using Interfaces in a program. public void aMethod(){ IDataStore ourData = new MySQLDataStore(); ourData.init(); // we know we can use init() // because all IDataStore s have an init() }

14 Using Interfaces in a program. public void aMethod(){ IDataStore ourData = new MySQLDataStore(); ourData.init(); ourData.extraMethod(); // compile time error. // We know the underlying class does have // an extraMethod() but we can’t access it because // we are treating it as an IDataStore which only has // 5 methods. }

15 Your Turn!

16 IResizable Graphic programs often have objects that can be resized (also called scaling). Scaling is usually done using double values. If we scale to.85, it means that the object is represented on screen at.85 the size of the original, if we scale to 1.5 it should resize to 1.5 the size. Create a project that contains two classes, a Circle and Rectangle that implements the IResizeable interface specified below. public interface IResizeable { void scale( double scalingFactor); }


Download ppt "Interfaces. Joseph Sant Sheridan College.. Objectives Discuss reasoning behind interfaces using a scenario. Interfaces and Contracts. Syntax examples."

Similar presentations


Ads by Google