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 2a7745e

Browse files
author
evitwilly
committed
added pattern Chain Of Responsibilities
1 parent 6033427 commit 2a7745e

File tree

11 files changed

+114
-2
lines changed

11 files changed

+114
-2
lines changed
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
750 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ Content:
3333
* [Singleton](/src/main/kotlin/design_patterns/Singleton.kt)
3434
* [Strategy](/src/main/kotlin/design_patterns/Strategy.kt)
3535
* [Observer](/src/main/kotlin/design_patterns/Observer.kt)
36-
* [Dependency Injection](/src/main/kotlin/design_patterns/Dependency%20%20Injection.kt)
36+
* [Dependency Injection](/src/main/kotlin/design_patterns/Dependency%20Injection.kt)
3737
* [Adapter](/src/main/kotlin/design_patterns/Adapter.kt)
3838
* [Memento](/src/main/kotlin/design_patterns/Memento.kt)
39+
* [Chain Of Responsibilities](/src/main/kotlin/design_patterns/Chain%20Of%20Responsibilities.kt)
3940

4041
2. package <code>structures</code> - data structure
4142
* [Binary tree](/src/main/kotlin/structures/BinaryTree.kt)

‎README_ru.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
* [Singleton](/src/main/kotlin/design_patterns/Singleton.kt)
3232
* [Strategy](/src/main/kotlin/design_patterns/Strategy.kt)
3333
* [Observer](/src/main/kotlin/design_patterns/Observer.kt)
34-
* [Dependency Injection](/src/main/kotlin/design_patterns/Dependency%20%20Injection.kt)
34+
* [Dependency Injection](/src/main/kotlin/design_patterns/Dependency%20Injection.kt)
3535
* [Adapter](/src/main/kotlin/design_patterns/Adapter.kt)
3636
* [Memento](/src/main/kotlin/design_patterns/Memento.kt)
37+
* [Chain Of Responsibilities](/src/main/kotlin/design_patterns/Chain%20Of%20Responsibilities.kt)
3738

3839
2. пакет <code>ru.structures</code> - структуры данных
3940
* [Бинарное дерево](/src/main/kotlin/structures/BinaryTree.kt)
File renamed without changes.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package design_patterns
22

33
/**
4+
*
45
* pattern: Visitor
56
*
67
* description: it's a behavioral pattern that allows you to add a new operation
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package design_patterns
2+
3+
/**
4+
*
5+
* pattern: Chain of responsibility
6+
*
7+
* description: a design pattern consisting of "a source of command objects and a series of processing objects".
8+
* Each processing object in the chain is responsible for a certain type of command, and the processing is done,
9+
* it forwards the command to the next processor in the chain.
10+
*
11+
*/
12+
13+
enum class BlockFactor {
14+
ONE, TWO, THREE
15+
}
16+
17+
/**
18+
*
19+
* I decided to give an analogy from the Minecraft game.
20+
* In this game there are blocks that can be broken with a stone pickaxe, iron and diamond.
21+
* For example: diamond may mine by iron and diamond pickaxes unlike cobblestone, which is mined by any
22+
*
23+
*/
24+
abstract class Block(private val factor: BlockFactor) {
25+
fun mayMine(factor: BlockFactor) = this.factor.ordinal <= factor.ordinal
26+
}
27+
28+
/**
29+
*
30+
* blocks from the game
31+
*
32+
*/
33+
class StoneBlock: Block(BlockFactor.ONE)
34+
class DiamondBlock: Block(BlockFactor.TWO)
35+
class ObsidianBlock: Block(BlockFactor.THREE)
36+
37+
abstract class Pickaxe(private val factor: BlockFactor) {
38+
39+
private var nextPickaxe: Pickaxe? = null
40+
fun changeNextPickaxe(pickaxe: Pickaxe) {
41+
this.nextPickaxe = pickaxe
42+
}
43+
44+
/**
45+
*
46+
* we mine the block, if it doesn't work, we take another pickaxe, if there is one
47+
*
48+
* @return return true if a pickaxe can mine
49+
*/
50+
fun mine(block: Block): Boolean =
51+
if (block.mayMine(factor)) {
52+
true
53+
} else {
54+
nextPickaxe?.mine(block) ?: false
55+
}
56+
57+
}
58+
59+
/**
60+
*
61+
* pickaxes from the game
62+
*
63+
*/
64+
class StonePickaxe: Pickaxe(BlockFactor.ONE)
65+
66+
class IronPickaxe: Pickaxe(BlockFactor.TWO)
67+
68+
class DiamondPickaxe: Pickaxe(BlockFactor.THREE)

0 commit comments

Comments
(0)

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