Now that you know the theoretical basics, let's look at these facilities and how they are
used in our program. Let's open
Lesson 2/3-project
in our IDE and extend our chatbot, which was developed in the previous chapter.
To do this, we
must decouple the environment and logic, and integrate only one in the
main
method.
Let's open the
EffectsProvider
class:
trait EffectsProvider extends TimeProvider { def input: UserInput def output: UserOutput } object DefaultEffects extends EffectsProvider { override def input: UserInput = ConsoleInput override def output: UserOutput = ConsoleOutput override def currentTime(): LocalTime = LocalTime.now() override def currentDate(): LocalDate = LocalDate.now() }
Here, we encapsulate all of the effects into our traits, which can have different implementations.
For example, let's look at
UserOutput
:
For full code, refer to
Code...