-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.kt
45 lines (38 loc) · 1.01 KB
/
Adapter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package design_patterns
/**
* pattern: Adapter
*
* using: used when we cannot directly use the functionality of an object
*
* description: to use the functionality of an object, a special class is created, which is called an adapter
*/
interface Adapter<T> {
fun getItem(position: Int) : T
fun getItemCount() : Int
}
/**
* here is a simplified imitation of RecyclerView component from android
*/
class RecyclerView<T> {
private var adapter: Adapter<T>? = null
fun changeAdapter(adapter: Adapter<T>) {
this.adapter = adapter
}
/**
* renders elements from the adapter
*
* @return returns a list of elements to test
*/
fun draw() : List<T> {
val items = mutableListOf<T>()
val myAdapter = adapter
if (myAdapter != null) {
val count = myAdapter.getItemCount()
for (i in 0 until count) {
items.add(myAdapter.getItem(i))
// draw item
}
}
return items
}
}