- Professional Scala
- Mads Hartmann Ruslan Shevchenko
- 305字
- 2021-07-23 17:24:27
OO in Our Chatbot
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.
Decoupling Logic and Environment
To do this, we must decouple the environment and logic, and integrate only one in the main
method.
Let's open the EffectsProvider
class:
Note
For full code, refer to Code Snippets/Lesson 2.scala
file.
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 Snippets/Lesson 2.scala
file.
trait UserOutput { def write(message: String): Unit def writeln(message: String): Unit = { write(message) write("\n") } } object ConsoleOutput extends UserOutput { def write(message: String): Unit = { Console.print(message) } }
Here, we can see the trait and object, which implement the current trait. This way, when we need to accept commands that are not from standard input, but from the chatbot API or from Twitter, we only need to change the implementation of the UserOutput
/ ConsoleOutput
interfaces.
It's now time to implement ConsoleOutput
and DefaultTimeProvider
.
Replace ???
in main with the appropriative constructor.
These steps for implementing ConsoleOutput
and DefaultTimeProvider
are as follows:
- Ensure that
Lesson 2/3-project
is open in IDE. - In the
UserOutput
file, find theConsoleOutput
file and change???
to the body of thewrite
method. The resulting method should look like this:object ConsoleOutput extends UserOutput{ def write(message: String): Unit = { Console.print(message) } } }
- In the
TimeProvider
file, add theDefaultTimeProvide
object which extends fromTimeProvider
and implements thecurrentTime
andcurrentDate
functions. The resulting code should look like this:object DefaultTimeProvider extends TimeProvider { override def currentTime(): LocalTime = LocalTime.now() override def currentDate(): LocalDate = LocalDate.now() } }
- CorelDRAW X6 中文版圖形設(shè)計(jì)實(shí)戰(zhàn)從入門到精通
- 物聯(lián)網(wǎng)安全:理論、實(shí)踐與創(chuàng)新
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- PLC、現(xiàn)場(chǎng)總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- Wireshark網(wǎng)絡(luò)分析就這么簡(jiǎn)單
- 網(wǎng)絡(luò)基礎(chǔ)與網(wǎng)絡(luò)管理項(xiàng)目化教程
- Unity Artificial Intelligence Programming
- 搶占下一個(gè)智能風(fēng)口:移動(dòng)物聯(lián)網(wǎng)
- 現(xiàn)代通信系統(tǒng)(第5版)
- 物聯(lián)網(wǎng)的機(jī)遇與利用
- Qt5 Python GUI Programming Cookbook
- 商業(yè)的本質(zhì)和互聯(lián)網(wǎng)
- 工業(yè)以太網(wǎng)技術(shù):AFDX/TTE網(wǎng)絡(luò)原理、接口、互連與安全
- 區(qū)塊鏈技術(shù)與應(yīng)用:打造分布式商業(yè)新生態(tài)
- Corona SDK Application Design