From 0b3b8d4ed85c44425de9427c180c616ddb464c49 Mon Sep 17 00:00:00 2001 From: Pablo Fraile Alonso Date: Fri, 25 Feb 2022 15:33:21 +0100 Subject: [PATCH] refactor(iblock): use delegation, no inheritance With this, we can encapsulate the BlockImplementation type. --- .../{BlockImplementation.kt => BlockLogic.kt} | 6 ++++-- .../kotlin/blocks/implementation/IBlock.kt | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) rename src/main/kotlin/blocks/implementation/{BlockImplementation.kt => BlockLogic.kt} (86%) diff --git a/src/main/kotlin/blocks/implementation/BlockImplementation.kt b/src/main/kotlin/blocks/implementation/BlockLogic.kt similarity index 86% rename from src/main/kotlin/blocks/implementation/BlockImplementation.kt rename to src/main/kotlin/blocks/implementation/BlockLogic.kt index a70a0e9..3a44186 100644 --- a/src/main/kotlin/blocks/implementation/BlockImplementation.kt +++ b/src/main/kotlin/blocks/implementation/BlockLogic.kt @@ -5,8 +5,10 @@ import blocks.Rotation import movements.Direction import movements.Position -abstract class BlockImplementation(private val FORMATS_OF_BLOCK: List>, private var initialPosition: Position): - Block { +internal class BlockLogic( + private val FORMATS_OF_BLOCK: List>, + private var initialPosition: Position +) : Block { private var currentFormatIndex = 0 diff --git a/src/main/kotlin/blocks/implementation/IBlock.kt b/src/main/kotlin/blocks/implementation/IBlock.kt index de29e91..bcbe30b 100644 --- a/src/main/kotlin/blocks/implementation/IBlock.kt +++ b/src/main/kotlin/blocks/implementation/IBlock.kt @@ -1,13 +1,25 @@ package blocks.implementation +import blocks.Block +import blocks.Rotation +import movements.Direction import movements.Position -class IBlock(initialPosition: Position) : BlockImplementation(TYPES, initialPosition) - -private val TYPES = listOf( +internal val TYPES = listOf( listOf(Position(1, 0), Position(1, 1), Position(1, 2), Position(1, 3)), listOf(Position(0, 2), Position(1, 2), Position(2, 2), Position(3, 2)), listOf(Position(2, 0), Position(2, 1), Position(2, 2), Position(2, 3)), listOf(Position(0, 1), Position(1, 1), Position(2, 1), Position(3, 1)), ) +class IBlock(initialPosition: Position) : Block { + + private val blockImplementation = BlockLogic(TYPES, initialPosition) + + override fun getNeededPositions(): Collection = blockImplementation.getNeededPositions() + + override fun rotate(degree: Rotation) = blockImplementation.rotate(degree) + + override fun move(direction: Direction) = blockImplementation.move(direction) + +} \ No newline at end of file