官术网_书友最值得收藏!

  • 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:

  1. Ensure that Lesson 2/3-project is open in IDE.
  2. In the UserOutput file, find the ConsoleOutput file and change ??? to the body of the write method. The resulting method should look like this:
    object ConsoleOutput extends UserOutput{
    def write(message: String): Unit = {
    Console.print(message)
    }
    }
    }
  3. In the TimeProvider file, add the DefaultTimeProvide object which extends from TimeProvider and implements the currentTime and currentDate functions. The resulting code should look like this:
    object DefaultTimeProvider extends TimeProvider {
    override def currentTime(): LocalTime = LocalTime.now()
    
      override def currentDate(): LocalDate = LocalDate.now()
      }
    
     }
主站蜘蛛池模板: 尼木县| 剑川县| 瑞金市| 桐柏县| 洛阳市| 日照市| 白山市| 濮阳县| 邢台市| 漯河市| 鹤庆县| 运城市| 榕江县| 兴国县| 大英县| 休宁县| 文化| 潮州市| 商都县| 高邮市| 葫芦岛市| 沈丘县| 滨海县| 永康市| 开封市| 湘潭市| 乃东县| 宜川县| 手游| 黎城县| 长阳| 新闻| 惠东县| 安顺市| 桐梓县| 吉首市| 得荣县| 磐石市| 察雅县| 监利县| 靖州|