@@ -2,51 +2,101 @@ package design_patterns
2
2
3
3
/* *
4
4
*
5
- * pattern: Facade
5
+ * Facade is a structural design pattern that simplifies the interface to a group of interfaces
6
6
*
7
- * using: used to simplify access to an object with a complex implementation
8
- *
9
- * description: a complex object contains several dependencies within itself, which it combines with each other
7
+ * with a more complex implementation
10
8
*
11
9
*/
12
10
11
+ data class GoodsEntity (
12
+ private val id : Long ,
13
+ private val name : String ,
14
+ private val description : String ,
15
+ private val price : Double
16
+ )
13
17
14
- /* *
15
- * imitation of local data storage (database)
16
- */
17
- class LocalDataSource {
18
-
19
- private val data = mutableListOf<String >()
18
+ class GoodsDatabase {
19
+ private val cachedGoods = mutableListOf<GoodsEntity >()
20
20
21
- fun save (data : List <String >) {
22
- this .data. addAll(data )
21
+ fun save (goods : List <GoodsEntity >) {
22
+ cachedGoods. addAll(goods )
23
23
}
24
24
25
- fun read () = data
26
- fun isEmpty () = data.isEmpty()
25
+ fun read () = cachedGoods
26
+ }
27
+
28
+ class GoodsNetworkService {
29
+
30
+ fun fetch () = listOf (
31
+ GoodsEntity (
32
+ id = 1 ,
33
+ name = " Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software 2nd Edition" ,
34
+ description = " You know you don't want to reinvent the wheel, so you look to Design Patterns: the lessons learned by those who've faced the same software design problems." ,
35
+ price = 41.94
36
+ )
37
+ )
27
38
28
39
}
29
40
30
- /* *
31
- * network request simulation
32
- */
33
- class NetworkDataSource {
34
- fun get () = listOf (
35
- " Harry Potter" ,
36
- " Ronald Weasley" ,
37
- " Hermione Granger"
41
+ data class CategoryEntity (
42
+ private val id : Long ,
43
+ private val name : String
44
+ )
45
+
46
+ class CategoryDatabase {
47
+ private val cachedCategories = mutableListOf<CategoryEntity >()
48
+
49
+ fun save (goods : List <CategoryEntity >) {
50
+ cachedCategories.addAll(goods)
51
+ }
52
+
53
+ fun read () = cachedCategories
54
+ }
55
+
56
+ class CategoryNetworkService {
57
+
58
+ fun fetch () = listOf (
59
+ CategoryEntity (
60
+ id = 1 ,
61
+ name = " Books"
62
+ )
38
63
)
64
+
39
65
}
40
66
41
- class Repository (private val localSource : LocalDataSource , private val networkSource : NetworkDataSource ) {
67
+ data class GoodsResult (
68
+ val goods : List <GoodsEntity >,
69
+ val categories : List <CategoryEntity >
70
+ )
71
+
72
+ // we have a group of interfaces (databases and network services)
73
+ class GoodsRepository (
74
+ private val goodsDatabase : GoodsDatabase ,
75
+ private val goodsNetworkService : GoodsNetworkService ,
76
+ private val categoryDatabase : CategoryDatabase ,
77
+ private val categoryNetworkService : CategoryNetworkService
78
+ ) {
42
79
43
- fun fetch () : List <String > {
44
- // I omitted error handling for simplicity
45
- if (localSource.isEmpty()) {
46
- val data = networkSource.get()
47
- localSource.save(data)
80
+ // we need a simpler interface
81
+ fun goodsAndCategories () : GoodsResult {
82
+ val goods = goodsDatabase.read().toMutableList()
83
+ if (goods.isEmpty()) {
84
+ val networkGoods = goodsNetworkService.fetch()
85
+ goodsDatabase.save(networkGoods)
86
+ goods.addAll(networkGoods)
48
87
}
49
- return localSource.read()
88
+
89
+ val categories = categoryDatabase.read().toMutableList()
90
+ if (categories.isEmpty()) {
91
+ val networkCategories = categoryNetworkService.fetch()
92
+ categoryDatabase.save(networkCategories)
93
+ categories.addAll(networkCategories)
94
+ }
95
+
96
+ return GoodsResult (
97
+ goods = goods,
98
+ categories = categories
99
+ )
50
100
}
51
101
52
102
}
0 commit comments