Skip to content

Commit

Permalink
refactor(design): new packages for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina authored and BrianLusina committed Nov 27, 2024
1 parent 01e538d commit 1eea7bf
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 90 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kotlinground.design.creationalpatterns.factory.parser

data class IntProperty(
override val name: String,
override val value: Int
) : Property
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kotlinground.design.creationalpatterns.factory.parser

class JsonParser : Parser {
override fun property(prop: String): Property {
TODO("Not yet implemented")
}

override fun server(props: List<String>): ServerConfig {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Demonstrates how to create abstract factory methods
*/
package com.kotlinground.design.creationalpatterns.factory.parser

interface Parser {
fun property(prop: String): Property
fun server(props: List<String>): ServerConfig
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kotlinground.design.creationalpatterns.factory.parser

interface Property {
val name: String
val value: Any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kotlinground.design.creationalpatterns.factory.parser

interface ServerConfig {
val properties: List<Property>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kotlinground.design.creationalpatterns.factory.parser

data class ServerConfigImpl(
override val properties: List<Property>
) : ServerConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kotlinground.design.creationalpatterns.factory.parser

data class StringProperty(
override val name: String,
override val value: String
) : Property
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kotlinground.design.creationalpatterns.factory.parser

class YamlParser : Parser {
override fun property(prop: String): Property {
TODO("Not yet implemented")
}

override fun server(props: List<String>): ServerConfig {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kotlinground.design.creationalpatterns.factory.parser

fun property(prop: String): Property {
val (name, value) = prop.split(":")

return when (name) {
"port" -> IntProperty(name, value.trim().toInt())
"environment" -> StringProperty(name, value.trim())
else -> throw Exception("Unknown property $name")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kotlinground.design.creationalpatterns.factory.parser

fun main() {
val portProperty = property("port: 8080")
val environmentProperty = property("environment: production")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Demonstrates Static Factory method
*/
package com.kotlinground.design.creationalpatterns.factory.server

class Server(private val port: Long) {
fun init() {
println("Server started on $port")
}

companion object {
fun withPort(port: Long): Server = Server(port)

fun withPort(port: String): Server {
return runCatching { port.toLong() }
.map { Server(it) }
.getOrThrow()
}
}
}

fun main() {
val server = Server.withPort(8080)
val server2 = Server.withPort("8081")
val server3 = Server(8082)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.kotlinground.design.structuralpatterns.facade

import com.kotlinground.design.creationalpatterns.factory.JsonParser
import com.kotlinground.design.creationalpatterns.factory.Server
import com.kotlinground.design.creationalpatterns.factory.YamlParser
import com.kotlinground.design.creationalpatterns.factory.parser.JsonParser
import com.kotlinground.design.creationalpatterns.factory.server.Server
import com.kotlinground.design.creationalpatterns.factory.parser.YamlParser
import java.lang.RuntimeException
import kotlin.io.path.Path

Expand Down

0 comments on commit 1eea7bf

Please sign in to comment.