-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathСhain Of Responsibilities.kt
More file actions
68 lines (57 loc) · 1.66 KB
/
Copy pathСhain Of Responsibilities.kt
File metadata and controls
68 lines (57 loc) · 1.66 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package design_patterns
/**
*
* pattern: Chain of responsibility
*
* description: a design pattern consisting of “a source of command objects and a series of processing objects”.
* Each processing object in the chain is responsible for a certain type of command, and the processing is done,
* it forwards the command to the next processor in the chain.
*
*/
enum class BlockFactor {
ONE, TWO, THREE
}
/**
*
* I decided to give an analogy from the Minecraft game.
* In this game there are blocks that can be broken with a stone pickaxe, iron and diamond.
* For example: diamond may mine by iron and diamond pickaxes unlike cobblestone, which is mined by any
*
*/
abstract class Block(private val factor: BlockFactor) {
fun mayMine(factor: BlockFactor) = this.factor.ordinal <= factor.ordinal
}
/**
*
* blocks from the game
*
*/
class StoneBlock: Block(BlockFactor.ONE)
class DiamondBlock: Block(BlockFactor.TWO)
class ObsidianBlock: Block(BlockFactor.THREE)
abstract class Pickaxe(private val factor: BlockFactor) {
private var nextPickaxe: Pickaxe? = null
fun changeNextPickaxe(pickaxe: Pickaxe) {
this.nextPickaxe = pickaxe
}
/**
*
* we mine the block, if it doesn't work, we take another pickaxe, if there is one
*
* @return return true if a pickaxe can mine
*/
fun mine(block: Block): Boolean =
if (block.mayMine(factor)) {
true
} else {
nextPickaxe?.mine(block) ?: false
}
}
/**
*
* pickaxes from the game
*
*/
class StonePickaxe: Pickaxe(BlockFactor.ONE)
class IronPickaxe: Pickaxe(BlockFactor.TWO)
class DiamondPickaxe: Pickaxe(BlockFactor.THREE)