Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7e868b

Browse files
refactored Strategy pattern
1 parent f4159fe commit d7e868b

File tree

2 files changed

+124
-21
lines changed

2 files changed

+124
-21
lines changed

‎src/main/kotlin/design_patterns/Strategy.kt

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,83 @@ package design_patterns
22

33
/**
44
*
5-
* pattern: Strategy
5+
* Strategy is a behavioral design pattern used to define a family of algorithms,
66
*
7-
* using: used when we need to change the behavior of an object
7+
* encapsulate each one and make them interchangeable
88
*
99
*/
1010

11-
interface ExchangeStrategy {
12-
fun into(price: Double) : Double
11+
// encapsulates the filtering algorithm
12+
interface FoodFilterStrategy {
13+
fun filter(items: List<FoodEntity>): List<FoodEntity>
14+
}
15+
16+
class OnlyChipsFilterStrategy : FoodFilterStrategy {
17+
override fun filter(items: List<FoodEntity>): List<FoodEntity> {
18+
return items.filter { it.category == "chips" }
19+
}
20+
}
21+
22+
class OnlyChocolateFilterStrategy : FoodFilterStrategy {
23+
override fun filter(items: List<FoodEntity>): List<FoodEntity> {
24+
return items.filter { it.category == "chocolate" }
25+
}
26+
}
1327

14-
class Dollar : ExchangeStrategy {
15-
override fun into(price: Double): Double {
16-
return price / 70
17-
}
28+
class PriceFilterStrategy(private val price: Int) : FoodFilterStrategy {
29+
override fun filter(items: List<FoodEntity>): List<FoodEntity> {
30+
return items.filter { it.price >= price }
1831
}
32+
}
1933

20-
class Tenge : ExchangeStrategy {
21-
override fun into(price: Double): Double {
22-
return price * 6
23-
}
34+
class SearchWordFilterStrategy(private val search: String) : FoodFilterStrategy {
35+
override fun filter(items: List<FoodEntity>): List<FoodEntity> {
36+
return items.filter { it.title.contains(search, ignoreCase = true) }
2437
}
2538
}
2639

27-
class RubleExchangeRate {
28-
private var strategy : ExchangeStrategy = ExchangeStrategy.Dollar()
40+
data class FoodEntity(
41+
val title: String,
42+
val price: Int,
43+
val category: String
44+
)
45+
46+
// FoodStore returns a list of food filtered by strategy
47+
class FoodStore(private var filterStrategy: FoodFilterStrategy) {
2948

30-
fun changeStrategy(strategy: ExchangeStrategy) {
31-
this.strategy = strategy
49+
// we can change strategy
50+
fun changeStrategy(strategy: FoodFilterStrategy) {
51+
filterStrategy = strategy
3252
}
3353

34-
fun exchange(priceInRuble: Double) = strategy.into(priceInRuble)
54+
fun foodItems(): List<FoodEntity> {
55+
val foodItems = fetchFoodItems()
56+
return filterStrategy.filter(foodItems)
57+
}
58+
59+
private fun fetchFoodItems() =
60+
listOf(
61+
FoodEntity(
62+
"Lays Potato Chips Fried Crab Flavor",
63+
2,
64+
"chips"
65+
),
66+
FoodEntity(
67+
"Lay's Potato Chips, Classic",
68+
3,
69+
"chips"
70+
),
71+
FoodEntity(
72+
"Dove Chocolate",
73+
3,
74+
"chocolate"
75+
),
76+
FoodEntity(
77+
"Ritter Sport Chocolate",
78+
4,
79+
"chocolate"
80+
)
81+
)
82+
3583
}
3684

‎src/test/kotlin/design_patterns/StrategyTest.kt

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,66 @@ internal class StrategyTest {
77

88
@Test
99
fun test() {
10-
val exchange = RubleExchangeRate()
11-
assertEquals(1.0, exchange.exchange(70.0), 0.0)
10+
val strategy = OnlyChipsFilterStrategy()
11+
val foodStore =FoodStore(strategy)
1212

13-
exchange.changeStrategy(ExchangeStrategy.Tenge())
14-
assertEquals(60.0, exchange.exchange(10.0), 0.0)
13+
assertEquals(listOf(
14+
FoodEntity(
15+
"Lays Potato Chips Fried Crab Flavor",
16+
2,
17+
"chips"
18+
),
19+
FoodEntity(
20+
"Lay's Potato Chips, Classic",
21+
3,
22+
"chips"
23+
)
24+
), foodStore.foodItems())
25+
26+
foodStore.changeStrategy(OnlyChocolateFilterStrategy())
27+
28+
assertEquals(listOf(
29+
FoodEntity(
30+
"Dove Chocolate",
31+
3,
32+
"chocolate"
33+
),
34+
FoodEntity(
35+
"Ritter Sport Chocolate",
36+
4,
37+
"chocolate"
38+
)
39+
), foodStore.foodItems())
40+
41+
foodStore.changeStrategy(PriceFilterStrategy(3))
42+
43+
assertEquals(listOf(
44+
FoodEntity(
45+
"Lay's Potato Chips, Classic",
46+
3,
47+
"chips"
48+
),
49+
FoodEntity(
50+
"Dove Chocolate",
51+
3,
52+
"chocolate"
53+
),
54+
FoodEntity(
55+
"Ritter Sport Chocolate",
56+
4,
57+
"chocolate"
58+
)
59+
), foodStore.foodItems())
60+
61+
foodStore.changeStrategy(SearchWordFilterStrategy("Ritter Sport"))
62+
63+
assertEquals(listOf(
64+
FoodEntity(
65+
"Ritter Sport Chocolate",
66+
4,
67+
"chocolate"
68+
)
69+
), foodStore.foodItems())
1570
}
1671

1772
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /