There are typically two types of classes, divided by how they are used
- Value types: These are classes that represent the what. They track information.
- Transformer types: These are classes that represent business logic. They act upon value types and transform them according to business rules.
There are situations where we want to have default values for some properties in a value type.
The "Builder" pattern was designed to handle scenarios where you might want to configure small portions of a class.
Reference: Java Builder
Builders are also used to make copies of existing classes with minor changes.
Kotlin has two features on data classes which make this obsolete.
- Default parameters: The ability to declare a default value for a constructor/function parameter
- Copy methods: The ability to make a copy of an existing object while changing only desired properties
Reference: Data Classes
- Define a
PhoneNumber
type, which should track a given phone number and the country ISD code. - Define an
EmailAddress
type, which should track the username, the email provider host. - Define a
User
type, which should have an integer user ID, a name, aPhoneNumber
and anEmailAddress
- Create an instance of the following user Name: Vinay Shenoy Phone Number: +91-1234567890 Email Address: [email protected]
- Change my phone number to +91-0987654321
Interfaces let you do two things:
- Provide configuration behaviour
- Support event driven programming
Note: Lambdas can be BOTH class properties as well as parameters to methods.
References: Lambdas
- Add a
removeAccount
method to theBank
Kotlin class - Add a lambda method that will get invoked whenever an account is removed that gets invoked with the removed account
- Configure
firstBank
to log the removed accounts using the Android logcat - Configure
secondBank
to log the removed account, but only if the account balance is over 5000 rupees
Reference: Null safety
- Kotlin's type system variations are mainly to remove null references from code. We'll see elvis operator, not-null assertion operator and safe calls as well. Kotlin also supports smart cast and type inference.
- Nullable type variables
- Non-nullable types
- Create a data class
Dessert
that has your favorite pastries with theamountOfCalories
as non-null Int - Create an instance and add a
setCalories
function that takes nullableamountOfCalories
as a parameter and use a safe call to have an integer value or a -1.
Common operations typically done on collections (List
):
- Iteration: Do something on every item
- Transformation: Convert each item into some other item
- Filtering: Select some items out of the list
- Grouping: Group information based on some criteria
Reference: Collections
- Find all mutants with 10 or more years of experience
- Find the real names of all the good mutants
- Concatenate the code names of all the senior mutants with a ", " (Hint: look at `joinToString``)
- Concatenate the code names of all the good and evil mutants separately with a ", " and then concatenate these two strings together with a " VS. "
Reference: Sealed classes
- Better state handling with restricted types.
- Create a
Failure
sealed class and add types of errors. Every error must be a data class that takes a string as a parameter. Eg:ServerError
,AuthenticationError
andExceptions
where exceptions would be unknown errors. - Create a function that takes
Failure
as a parameter. Implement all the failures in it using awhen
condition. Print "something went wrong` for unknown exceptions and the error names for the rest of them.
Reference: Extension functions
- Utility classes are generally made when we don't have access to the original source code.
- Example : if we want to add an email validation method to a string. In Java, we would create a StringUtil class, and add a method like isEmail(String) , so we can call StringUtil.isEmail(value). This can be replaced with extension methods in Kotlin, so we can call something like value.isEmail
- Syntax .
- Ability to extend the class without having to inherit from a class.
- We wont get private information in the class, we will still have access to public fields and functions. It's just a syntactic sugar for Util classes.
- Add a extension function for
String
to check whether it's an email or not. We can check if the string contains@
to determine if it's email or not. - Add a extension function for
Int
that takes in parameter to check if it's divisible by it or not.