fixed Abstract Factory design pattern
This commit is contained in:
40
src/main/kotlin/design_patterns/Abstract Factory.kt
Normal file
40
src/main/kotlin/design_patterns/Abstract Factory.kt
Normal file
@ -0,0 +1,40 @@
|
||||
package design_patterns
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
*
|
||||
* An abstract factory is a generative design pattern that allows
|
||||
* you to create families of related objects without being tied to
|
||||
* the specific classes of objects you create.
|
||||
*
|
||||
*/
|
||||
|
||||
interface Button {
|
||||
fun draw() {}
|
||||
}
|
||||
|
||||
class AndroidButton : Button
|
||||
class IOSButton : Button
|
||||
|
||||
interface Text {
|
||||
fun draw() {}
|
||||
}
|
||||
|
||||
class AndroidText : Text
|
||||
class IOSText : Text
|
||||
|
||||
interface ButtonFactory {
|
||||
fun createButton() : Button
|
||||
fun createText() : Text
|
||||
}
|
||||
|
||||
class AndroidButtonFactory : ButtonFactory {
|
||||
override fun createButton() : Button = AndroidButton()
|
||||
override fun createText(): Text = AndroidText()
|
||||
}
|
||||
|
||||
class IOSButtonFactory : ButtonFactory {
|
||||
override fun createButton() : Button = IOSButton()
|
||||
override fun createText(): Text = IOSText()
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package design_patterns
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* pattern: Factory
|
||||
*
|
||||
* using: used to create many similar objects without directly calling the constructor
|
||||
*/
|
||||
|
||||
abstract class Screen {
|
||||
abstract fun size() : Pair<Float, Float>
|
||||
}
|
||||
|
||||
class LargeScreen : Screen() {
|
||||
override fun size(): Pair<Float, Float> {
|
||||
return 1920f to 1080f
|
||||
}
|
||||
}
|
||||
|
||||
class SmallScreen : Screen() {
|
||||
override fun size(): Pair<Float, Float> {
|
||||
return 1280f to 720f
|
||||
}
|
||||
}
|
||||
|
||||
enum class Screens {
|
||||
FULL_HD, HD
|
||||
}
|
||||
|
||||
class ScreenFactory {
|
||||
fun screenBy(type: Screens) = when (type) {
|
||||
Screens.FULL_HD -> LargeScreen()
|
||||
Screens.HD -> SmallScreen()
|
||||
}
|
||||
}
|
||||
28
src/test/kotlin/design_patterns/AbstractFactoryTest.kt
Normal file
28
src/test/kotlin/design_patterns/AbstractFactoryTest.kt
Normal file
@ -0,0 +1,28 @@
|
||||
package design_patterns
|
||||
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.core.IsInstanceOf.instanceOf
|
||||
import org.junit.Test
|
||||
|
||||
internal class AbstractFactoryTest {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val iosFactory = IOSButtonFactory()
|
||||
|
||||
val iosButton = iosFactory.createButton()
|
||||
val iosText = iosFactory.createText()
|
||||
|
||||
assertThat(iosButton, instanceOf(IOSButton::class.java))
|
||||
assertThat(iosText, instanceOf(IOSText::class.java))
|
||||
|
||||
val androidFactory = AndroidButtonFactory()
|
||||
|
||||
val androidButton = androidFactory.createButton()
|
||||
val androidText = androidFactory.createText()
|
||||
|
||||
assertThat(androidButton, instanceOf(AndroidButton::class.java))
|
||||
assertThat(androidText, instanceOf(AndroidText::class.java))
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
package design_patterns
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
|
||||
internal class FactoryTest {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val factory = ScreenFactory()
|
||||
|
||||
val fullHdScreen = factory.screenBy(Screens.FULL_HD)
|
||||
val fullHdSize = fullHdScreen.size()
|
||||
|
||||
assertEquals(1920f, fullHdSize.first)
|
||||
assertEquals(1080f, fullHdSize.second)
|
||||
|
||||
val hdScreen = factory.screenBy(Screens.HD)
|
||||
val hdSize = hdScreen.size()
|
||||
|
||||
assertEquals(1280f, hdSize.first)
|
||||
assertEquals(720f, hdSize.second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user