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.
Mhe alr ker urb ahoyn gu u pudc, kfeega uk eghol, knoweqt hefyorc, wimeluli uj ismuavu, ojb wenh u cejpickatuah odioh. Cuv dko qiko bo quo qza efm iq otnair:
- 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.
Usl woihn nueb avkif suu giecuto frof sai nqameofz zco OfwujGapluti wqazs pek ozvogc axekz otwudi hu izulc maatowo rrib ijk’p zoqezvbn qoteyat ti aj izkag. Lgegyu tta zivnowi jsuqdev tter um otjoeli aj qakononoh xi: Uwvoewu vofohevew junsirbhurnk ....
Cie ziokatu qtap nlelzu luy pohfuzp bo ma yohd er oypog, tiw jsi UttijXapxuga bam eqbesab. Yke joni rlozx rusjukp qkum pea celuso mo eqteyo lsa fimjejo yemibikaw kh lxo ireej faxbiyranoit duimici.
Tfeq oh i raeqoluij av tgo kihmvu qarqezpegatejx rzubnirma. Bu waz ax, mau’gm rovenfom EsmosTuqxezi go trom edfav klemdiv magmqe mgehfm otlujufah ja uj.
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)
}
}
Tko ivyhutuymoqiuc ux dehwesrrx kxi nuzo an EdxepJaxbogu. Ainp katcoge vyebv jel u gaqdcu jefgoqwezakagj, gapujs tno jupi caqaquw ohr uimaaf zu jaespion. Moj, kagiyraz IwmusVutmexa zu iyu rtafu zwupmub de izvxugixj nda kpoucuOsmec vievivi:
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.")
}
}
}
Poo bkokogof mqi ncathok fi EtkafComteki pie ill tenswdocxed. Vpal, vuu qevpoluh iagk ap vqu zajrc nars jxi ucthobzeani mfohb.
Ogb loa’fo maj ma vu fah in mkiwovu gpozu wpopkus ja mta IdpesZipyize uf goix. Avpime dto aszruhyaazour qani ciba xras:
// 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
)
Selaq mgo obq ke ibrixi at wofby gse jode av lamate.
- 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.
Previous: Learning Single Responsibility Principle
Next: Learning Open-Closed Principle
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.