Last active
October 18, 2023 23:58
-
-
Save GabrielBrasileiro/c68fed78e2307aa52aa2e59c7866ee7a to your computer and use it in GitHub Desktop.
Functional Replacement Pattern
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 de controle | |
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 -> | |
// Edições, criações, atualizações, 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 { | |
override 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
// Interface funcional | |
fun interface ContextScope { | |
fun onContext(contextData: Any) | |
} | |
// Interface de isolamento | |
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
// Interface de recepção | |
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 | |
) | |
// Estrutura de testes manual | |
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) // Saída: 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 { | |
// Contexto não interpretável isolado | |
private val legacyContext = LegacyContext() | |
// Contexto transformado e emitido | |
override fun contextFlow(scope: ContextScope) { | |
/** | |
* Criações, invocações e definições dos dados que | |
* gostaria de passar para a nova estrutura de execução | |
* do código. | |
*/ | |
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 { | |
// Método de recepção e processamento final. | |
override fun emitFlow(data: Any) { | |
/** | |
* Criação dos novos algoritmos que serão | |
* mantidos e estruturados de forma testável. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment