Chapter 3 - Types and Interfaces Flashcards
1
Q
interface
A
- contains abstract methods which don’t have an implementation
- use implements keyword to tie a class to an interface; this class is a subtype of the interface and must implement the abstract methods
2
Q
Comparable
A
- uses o1.compareTo(o2)
- class that implements this method needs to use keyword “implements Comparable”; only one way to compare using this method
- returns: 0 if equal; negative if o1 < o2; positive if o1 > o2
- use Collections.sort(list) to then sort the objects based on implemented compare method
3
Q
Comparator
A
- separate class from the element type we’re comparing
- create multiple separator classes that implement Comparator that each have their own method to compare to objects (implement method compare(o1, o2))
- call Collections.sort(List<T> list, Comparator)</T>
- use static factory methods to define Comparator classes
4
Q
static factory method for Comparator createByRankComparator
A
pg. 63 textbook
5
Q
SWITCH STATEMENT antipattern
A
relying on a global variable that stores the required strategy and switching strategies based on this flag
6
Q
Iterable interface
A
- implement Iterator<T> iterator()</T>
- for lists (eg. aCards) -> return aCards.iterator(); suffices
7
Q
ITERATOR design pattern
A
provides a way to access the elements encapsulated within another object without exposing the underlying representation
8
Q
STRATEGY design pattern
A
- define a family of algorithms, encapsulate each one, and make them interchangeable (have an interface that is implemented by multiple classes that each have their own strategy)
9
Q
dependency injection technique
A
- inject the dependency into the client class (usually using a constructor)