Last active
January 8, 2024 00:12
-
-
Save GabrielBrasileiro/30c39a276f4c5f58514743e94d090785 to your computer and use it in GitHub Desktop.
Functional Replacement Pattern (En)
This file contains hidden or 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
// Control interface | |
interface ClientFlow { | |
fun executeFlow() | |
} |
This file contains hidden or 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
class ClientFlowTransform( | |
private val executionFlow: ExecutionFlow, | |
private val emitterFlow: EmitterFlow | |
) : ClientFlow { | |
override fun executeFlow() { | |
executionFlow.contextFlow { data: Any -> | |
// Transforms, creations, instances, updates, etc. | |
emitterFlow.emitFlow(anotherObject) | |
} | |
} | |
} |
This file contains hidden or 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
interface ClientFlow { | |
fun createState() | |
} | |
class ClientFlowTransform( | |
private val executionFlow: ExecutionFlow, | |
private val emitterFlow: EmitterFlow | |
) : ClientFlow { | |
override fun createState() { | |
executionFlow.onScope { newContext: NewContext -> | |
emitterFlow.emitFlow(newContext.param.uppercase()) | |
} | |
} | |
} |
This file contains hidden or 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
// Functional interface | |
fun interface ContextScope { | |
fun onContext(contextData: Any) | |
} | |
// Isolation interface | |
interface ExecutionFlow { | |
fun contextFlow(scope: ContextScope) | |
} |
This file contains hidden or 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
// Receiving interface | |
interface EmitterFlow { | |
fun emitFlow(data: Any) | |
} |
This file contains hidden or 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
interface EmitterFlow { | |
fun emitFlow(data: String) | |
} | |
class EmitterFlowReceiverFake : EmitterFlow { | |
private var lastMessage = String() | |
override fun emitFlow(data: String) { | |
lastMessage = data | |
} | |
fun getLastMessage(): String = lastMessage | |
} |
This file contains hidden or 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
interface ExecutionFlow { | |
fun contextFlow(scope: ContextScope) | |
} | |
class ExecutionFlowFake( | |
private val newContext: NewContext | |
) : ExecutionFlow { | |
override fun contextFlow(scope: ContextScope) { | |
scope.onContext(newContext.param) | |
} | |
} |
This file contains hidden or 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
data class NewContext( | |
val param: String | |
) | |
// Manual test structure | |
fun main() { | |
val expected = "ANY" | |
val newContext = NewContext("any") | |
val fakeEnvironment = ExecutionFlowFake(newContext) | |
val emitterFlowReceiverFake = EmitterFlowReceiverFake() | |
val processState: ClientFlow = ClientFlowTransform(fakeEnvironment, emitterFlowReceiverFake) | |
processState.executeFlow() | |
val result = terminateEnvironmentFake.getLastMessage() | |
println(expected == result) // Output: True | |
} |
This file contains hidden or 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
class LegacyExecutionFlow : ExecutionFlow { | |
// Isolated, non-interpretable context. | |
private val legacyContext = LegacyContext() | |
// Transformed and emitted context. | |
override fun contextFlow(scope: ContextScope) { | |
/** | |
* Creations, invocations, and definitions of | |
* data which you would like to pass to the | |
* new code execution structure. | |
*/ | |
val contextData = // Código final mapeado. | |
scope.onContext(newContext) | |
} | |
} |
This file contains hidden or 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
class NewFeatureContext : EmitterFlow { | |
// Reception and final processing method. | |
override fun emitFlow(data: Any) { | |
/** | |
* Creation of the new algorithms that will be | |
* maintained and structured in a testable manner. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment