Advanced Object-Oriented Programming in Kotlin

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

Lesson 03: Single Responsibility & Open-Closed Principles

Implementing Single Responsibility Principle

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The Single Responsibility Principle states that a class should have only one responsibility and only one reason to change. In this demo, you’ll use this principle to enhance the quality of your e-commerce app.

- You have 2 order items, at the cost of $2000.0
Order created.
Credit card payment was processed with a total amount of $2000.0.
Payment successful.
Invoice generated...
Email sent: Order confirmed.
interface NotificationService {
  fun sendConfirmationEmail()
}

interface PaymentService {
  fun processPayment(shoppingCart: ShoppingCart): Boolean
}

interface InvoiceService {
  fun generateInvoice(shoppingCart: ShoppingCart)
}

interface LoggingService {
  fun logOrderActivity(activity: String)
}
class EmailNotificationService : NotificationService {
  override fun sendConfirmationEmail() {
    // Logic to send confirmation email
    println("Email sent: Order confirmed.")
  }
}

class CreditCardPaymentService : PaymentService {
  override fun processPayment(shoppingCart: ShoppingCart): Boolean {
    // Logic to process credit card payment
    println(
      "Credit card payment was processed with a total amount of $${
        shoppingCart.getTotalOrderPrice()
      }."
    )
    return true
  }
}

class InvoiceGenerationService : InvoiceService {
  override fun generateInvoice(shoppingCart: ShoppingCart) {
    // Logic to generate invoice
    println("Invoice generated...")
  }
}

class ConsoleLoggingService : LoggingService {
  override fun logOrderActivity(activity: String) {
    // Logic to log order activity to console
    println(activity)
  }
}
class OrderService (
  private val notificationService: NotificationService,
  private val paymentService: PaymentService,
  private val invoiceService: InvoiceService,
  private val loggingService: LoggingService
) {
  fun createOrder(shoppingCart: ShoppingCart) {
    // Business logic for creating an order
    loggingService.logOrderActivity("Order created.")

    // Payment processing
    if (paymentService.processPayment(shoppingCart)) {
      loggingService.logOrderActivity("Payment successful.")

      // Invoice generation
      invoiceService.generateInvoice(shoppingCart)

      // Notification
      notificationService.sendConfirmationEmail()
    } else {
      loggingService.logOrderActivity("Payment failed.")
    }
  }
}
// Creating instances of individual services
val emailNotificationService: NotificationService = EmailNotificationService()
val creditCardPaymentService: PaymentService = CreditCardPaymentService()
val invoiceGenerationService: InvoiceService = InvoiceGenerationService()
val consoleLoggingService: LoggingService = ConsoleLoggingService()

// Creating an OrderService with the individual services
val orderService = OrderService(
  emailNotificationService,
  creditCardPaymentService,
  invoiceGenerationService,
  consoleLoggingService
)
- You have 2 order items, at the cost of $2000.0
Order created.
Credit card payment processed with a total amount of $2000.0.
Payment successful.
Invoice generated...
Email sent: Order confirmed.
println("Invoice generated successfully...")
See forum comments
Cinema mode Download course materials from Github
Previous: Learning Single Responsibility Principle Next: Learning Open-Closed Principle