Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Professional Scala

You're reading from   Professional Scala Combine object-oriented and functional programming to build high-performance applications

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher
ISBN-13 9781789533835
Length 186 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Ruslan Shevchenko Ruslan Shevchenko
Author Profile Icon Ruslan Shevchenko
Ruslan Shevchenko
Mads Hartmann Mads Hartmann
Author Profile Icon Mads Hartmann
Mads Hartmann
Arrow right icon
View More author details
Toc

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime