This file contains 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
// Solution step 1: Create an interface called Notifier. | |
// It has one method: jobAlert(String jobDescription) | |
interface Notifier { | |
fun jobAlert(jobDescription: String) | |
} | |
// Step II: | |
// Create an emailClient class that implements | |
// Notifier interface | |
class EmailClient : Notifier { |
This file contains 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
import kotlin.jvm.JvmStatic | |
/** | |
* Interface for generating job alerts | |
*/ | |
interface AlertGenerator { | |
fun generateJobAlert(job: String): String | |
} | |
/** |
This file contains 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
import android.text.Editable | |
import android.text.TextWatcher | |
import android.widget.EditText | |
// We create an interface with one method | |
interface TextWatcherWithInstance { | |
fun onTextChanged(editText: EditText, s: CharSequence, start: Int, before: Int, count: Int) | |
} | |
// We create a custom class called MultiTextWatcher. |
This file contains 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
import android.text.Editable | |
import android.text.TextWatcher | |
import android.widget.EditText | |
editText.addTextChangedListener(object : TextWatcher { | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |
// Empty implementation | |
} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { |
This file contains 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 ClickListener { | |
fun onClick() | |
} | |
class Fragment1 : ClickListener { | |
override fun onClick() { | |
decrementClickCount() | |
//handle logic | |
} |
This file contains 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 ClickListener { | |
fun onClick() | |
} | |
class Fragment1 : ClickListener { | |
override fun onClick() { | |
//handle logic | |
} | |
fun decrementClickCount() { |
This file contains 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 TimeOfDay { | |
fun greet(): String | |
} | |
class TimeOfDayGreeting(private val timeOfDay: TimeOfDay) { | |
fun getGreetingFromTimeOfDay(): String { | |
return timeOfDay.greet() | |
} | |
} |
This file contains 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 TimeOfDayGreeting { | |
private var timeOfDay: String? = null | |
/* | |
* Every time this method is called it will | |
* called an if else logic, which is in violation of the | |
* OCP principle. | |
*/ | |
fun getGreetingFromTimeOfDay(): String { | |
return when (timeOfDay) { |
This file contains 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
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val movie = movies[position] | |
holder.title.text = movie.title | |
holder.rating.text = movie.rating | |
// all the logic is moved into util class...now is clean! | |
holder.authors.text = AppUtils.getGenres(movie) | |
} |
This file contains 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
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val movie = movies[position] | |
holder.title.text = movie.title | |
holder.rating.text = movie.rating | |
// SRP violation, onBindViewHolder has only the responsibility to display data | |
// & not make data formatting operations | |
val genres = movie.genres |
NewerOlder