-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(design): new packages for examples
- Loading branch information
BrianLusina
authored and
BrianLusina
committed
Nov 27, 2024
1 parent
01e538d
commit 1eea7bf
Showing
14 changed files
with
105 additions
and
90 deletions.
There are no files selected for viewing
68 changes: 0 additions & 68 deletions
68
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/Parser.kt
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/Server.kt
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
.../src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/IntProperty.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
11 changes: 11 additions & 0 deletions
11
...n/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/JsonParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/Parser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
6 changes: 6 additions & 0 deletions
6
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/Property.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
5 changes: 5 additions & 0 deletions
5
...src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/ServerConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
5 changes: 5 additions & 0 deletions
5
...main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/ServerConfigImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
...c/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/StringProperty.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
11 changes: 11 additions & 0 deletions
11
...n/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/YamlParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/factory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/parser/main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
26 changes: 26 additions & 0 deletions
26
design/src/main/kotlin/com/kotlinground/design/creationalpatterns/factory/server/Server.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
6 changes: 3 additions & 3 deletions
6
design/src/main/kotlin/com/kotlinground/design/structuralpatterns/facade/server.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters