Skip to content

Instantly share code, notes, and snippets.

@GabrielBrasileiro
Last active January 8, 2024 00:12
Show Gist options
  • Save GabrielBrasileiro/30c39a276f4c5f58514743e94d090785 to your computer and use it in GitHub Desktop.
Save GabrielBrasileiro/30c39a276f4c5f58514743e94d090785 to your computer and use it in GitHub Desktop.
Functional Replacement Pattern (En)
// Control interface
interface ClientFlow {
fun executeFlow()
}
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)
}
}
}
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())
}
}
}
// Functional interface
fun interface ContextScope {
fun onContext(contextData: Any)
}
// Isolation interface
interface ExecutionFlow {
fun contextFlow(scope: ContextScope)
}
// Receiving interface
interface EmitterFlow {
fun emitFlow(data: Any)
}
interface EmitterFlow {
fun emitFlow(data: String)
}
class EmitterFlowReceiverFake : EmitterFlow {
private var lastMessage = String()
override fun emitFlow(data: String) {
lastMessage = data
}
fun getLastMessage(): String = lastMessage
}
interface ExecutionFlow {
fun contextFlow(scope: ContextScope)
}
class ExecutionFlowFake(
private val newContext: NewContext
) : ExecutionFlow {
override fun contextFlow(scope: ContextScope) {
scope.onContext(newContext.param)
}
}
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
}
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)
}
}
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