Advanced Object-Oriented Programming in Kotlin

May 22 2024 · Kotlin 1.9, Android 14, Kotlin Playground

Lesson 01: Composition & Aggregation

Working with Composition

Episode complete

Play next episode

Next
Transcript

In this demo, you’ll learn how to use composition to build an e-commerce app. An e-commerce platform generally has products available to purchase, a cart to keep track of the items you want to order, the items you’re ordering, and the final order you’ll pay for. This demo simplifies the process by utilizing object-oriented programming (OOP) concepts by creating classes for products, orders, and order items.

To get started, open the Kotlin playground in your browser. This environment is all you need to learn this concept, as Kotlin supports object-oriented programming by default and doesn’t require any external libraries or tools.

Download the course material from the GitHub link at the side of the video. Copy and paste the code from Composition.kts in the Starter folder for Lesson 1. You’ll notice three classes with a bunch of TODOs:

  • Product: This holds the name and price of a product.
  • OrderItem: This represents an item that forms part of an order. It contains a product and the quantity.
  • Order: This represents an order placed to purchase an item. It’s composed of a list of OrderItems.

Run the app. It’ll show details of an order that has already been created for you:

- You have 1 order(s), containing 3 items, at the cost of $2,800.0

Create a new order. Pick the ankleProtector as your order item, and add it to your list of orders in the section marked // TODO: Create new order with ankleProtector:

val order2 = Order()
order2.addOrderItem(OrderItem(ankleProtector, 2))
orders.add(order2)

Rerun the app to see details of your orders now:

- You have 2 order(s), containing 5 items, at the cost of $2,910.0

Soon afterward, you find out that your favorite sports brand is running a promo for the ankle protector you just ordered. Since the items haven’t been shipped yet, you rush to the e-commerce app to cancel and delete the order.

In programming, objects can have relationships with other objects through composition. This relationship means that the lifecycle of one object is tied to that of another. For example, in an e-commerce system, it makes sense for the lifecycle of an order to be tied to the order items. If there are no order items, there’s no reason to create an order. And if an order is paid for, all the order items should be marked as sold.

However, whether or not an order is canceled has nothing to do with the product. If an order is canceled, the product can be returned to the shelf. This suggests that it’s not appropriate for a product and an order to be related by composition. An order must have at least one item to be relevant, but a product can remain on the shelf or be marked as sold if it’s eventually bought.

With this understanding, implement this termination by removing the order from your list of orders in the section marked // TODO: Remove order:

orders.remove(order2)

To enforce composition, deleting an order removes it from the list of orders. This affects your overall order summary. Rerun the app to confirm your changes:

- You have 1 order(s), containing 3 items, at the cost of $2,800.0

That order, together with its order items, no longer exists. By removing the order from your list of orders, you have implicitly removed the total number of items you’ve ordered and, consequently, reduced how much you’ll pay.

That’s all for this demo. Continue to the next section to learn about aggregation.

See forum comments
Cinema mode Download course materials from Github
Previous: Understanding Composition Next: Understanding Aggregation