Software Construction: Interfaces

Cards to learn interfaces.

Cards to learn interfaces.


P. M.
Diese Lernkarten behandeln fortgeschrittene Konzepte der Softwareentwicklung an der Universität, mit Fokus auf Schnittstellen, Methoden und Problemlösungen. Sie erklären Definitionen und Implementierungen von Schnittstellen wie Iterable und Iterator, sowie die Verwendung von Funktion Objekten und dem Comparable Interface. Die Karteikarten zeigen, wie man interne Strukturen versteckt und verschiedene Sortierstrategien umsetzt. Programmierer und Entwickler profitieren davon, um ihre Fähigkeiten in der objektorientierten Programmierung zu vertiefen und effizientere Code-Strukturen zu schaffen.
Flashcards
22
Students
0
Language
German
Category
Computer Science
Level
University
Created / Updated
03.01.2020 / 10.01.2020

Flashcards

Interface (broad Definition)

An interface to a class consists of the methods of that class that  are accessibble to another class (e.g. it's public methods in case of an interface fused with the class definition).

When do we separate interface & class?

When different implementations are possible.

Interfaces in Java

In Java, interfaces provide a specification of the methods that it should be possible to invoke on the objects of a class.

What are abstract methods?

Methods that do not have an implementation.

Examples:

  • Card draw();
  • boolean isEmpty();

The "implements" keyword

Formal guarantee, that instances of class types will have concrete implementationa for all methods in the interface types.

Subtype relationship between the implementing class and the interface type (e.g. class Deck is of type CardSource).

Example: public class Deck implements CardSource { … }

Example seperate interface & class

Important

  • CardSource source = new Deck(); ⇒ Because Deck declares to implement interface CardSource
  • Now the method can operate on objects of any class that implements the CardSource interface

When do I need a seperate interface & what should this interface specify?

There are no general answers to these questions.

The Comparable Interface (Definition)

The library does not know how want to sort our cards (It can not anticipate all the possible objects a programmer may create).

The Comparable<T> interface helps to solve this problem by defining a method on how to compare objects.

To allow us to sort a list of cards, we have to provide the expected comparable behaviour. We declare it with the implements keyword.

Function Objects (Definition)

Function objects are objects that provide the implementation for a single method.

Their interface typically maps one-to-one to that of an interface (normally, a Java interface represents only a subset of the complete interface of classes that implement it).

Example Function Object

In our card game, we want to implement different sorting strategies.

Modifying the code of compareTo (e.g., by using a flag to switch between different strategy) is deprecated.

A better solution is to move the comparison code to a separate object.

The solution is supported by the use of the Comparator<T> interface, which implements the method compare (as opposed to compareTo).

 

Problem with Example Function Objects

In some cases the information available to the class implementing the Comparator interface is enough (e.g., using the getter methods).

Sometimes, however, implementing the compare method will require access to private members.

Three possible solutions

  1. Using a nested class
  2. Through an anonymous class
  3. Recurring to lambda expressions

Function Object Problem solution 1 (Nested class)

Function Object Problem solution 2 (Anonymous class)

Function Object Problem solution 3 (Lambda expression)

How to hide the internal structure of card? (i.e. don't return a reference to the internal card list)

We return a new list that copies the content of the internal list, thus having the same values but without being the same reference.

Iterator interface (Definition)

An iterator over a collection. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

  • boolean hasNext(): Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
  • E next(): Returns the next element in the iteration. Throws NoSuchElementException - if the iteration has no more elements.
  • default void remove(): Removes from the underlying collection the last element returned by this iterator (optional operation).

Example Iterator for Card class

Card returns an Iterator<Card> when getCards() is called. Caller can then iterate over collection without revealing underlying structure.

Iterable<T> interface (Definition)

The iterable<E> interface only contracts us to one method: public Iterator<E> iterator(){} which returns an Iterator<E> to the underlying container of the implementation.

After implementing the Iterable<E> interface, i.e. with public class Deck implements Iterable<Card>{} we can iterate over the implementation, the Deck class in this case, with for(Card card: deck){}.

Example of iterable Deck 

Example Class Diagram Iterable<Card>

Example Class Diagram Anonymous Type

The Interface Segregation Principle (ISP) (Definition)

Client code should not be forced to depend on interfaces it does not need. 

Study