Why do we need sealed classes?  

Sealed classes are designed for situations in which a limited set of functionalities are required and no other class is allowed to be part of this set. For example, let's say we are writing an application for an order-delivery service in which a limited number of steps are involved:

  1. The order is received at depot
  2. The order is dispatched to the customer
  3. The order is delivered to the customer

Let's create an Order class with the item property:

class Order(val item : String)

Create a sealed class, OrderDelivery, with one property, Order. We then need to create three child classes, each of which represents one of the steps:

sealed class OrderDelivery(val order : Order) 

class ReceivedAtDepot(val depotName : String, order: Order) : OrderDelivery(order)

class Dispatched(var truckId : String, var driverName : String, order: Order) : OrderDelivery(order)

class Delivered(var destination : String, var isDelivered : Boolean, order: Order) : OrderDelivery(order)

Let's create an orderStatus function with a when expression, using which we can see the current status of the order delivery:

fun orderStatus(delivery: OrderDelivery) {
when (delivery) {
is ReceivedAtDepot -> println("${delivery.order.item} is received at ${delivery.depotName} depot.")
is Dispatched -> println("${delivery.order.item} is dispatched, Truck ID is ${delivery.truckId} and driver is ${delivery.driverName}")
is Delivered -> println("${delivery.order.item} delivered at ${delivery.destination}. "+
"Delivery to customer = ${delivery.isDelivered}. ")
}
}

In the main function, create an order and pass it to the ReceivedAtDepot class. Create a Dispatched class object with the vehicle and driver information and dispatch the order to the customer. When the order is delivered to the correct address, update the delivery status. By using polymorphism, we can create a list of the delivery statuses and pass them to the orderStatus function one by one:

fun main(args: Array<String>) {

var book = Order("OOP in Kotlin Book")
var atDepot = ReceivedAtDepot(depotName = "Stockholm City", order = book)
var dispatched = Dispatched(truckId = "AXV-122", driverName = "Logan", order = book)
var delivered = Delivered(destination = "älvsjö kommun", isDelivered = true, order = book)

var orderDeliverySteps = listOf(atDepot, dispatched, delivered)

for (step in orderDeliverySteps) {
orderStatus(step)
}

var knife = Order("Kitchen knife set")
atDepot = ReceivedAtDepot(depotName = "Stockholm City", order = knife)
dispatched = Dispatched(truckId = "JVY-354", driverName = "Peter Parker", order = knife)
delivered = Delivered(destination = "Stockholm city", isDelivered = true, order = knife)

orderDeliverySteps = listOf(atDepot, dispatched, delivered)

for (step in orderDeliverySteps) {
orderStatus(step)
}
}

The main function contains two orders, each of which represents an order status. We have a list of OrderDeliverySteps that contain instances of different classes. The orderStatus function takes the parent class as a parameter and displays the order-related information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.176