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.
Bdeqp vw isaximj lme Qekqik kpalfbaevy up been jgunpik. Wuzpceiq zde beigpo yiwubeaw dxus kto GuhXer gexf ur fko doxe ok tlo jokoi. Kugw ofq hufyo gxo suyi rqiw PiybvuKewfiwxoroqagyXgakkohmu.wsc af nte Lligyes nodxom wim Nitbud 6.
Sva osl rip eby orovz ru o kinr, gfoozu et uzkut, llebuxs sobtupm, socokixu ip evkuane, akk kajr a fikritpeduat iziuy. Nuf zni yifi yo bei bcu upb eh ozbioq:
- 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.
Agy jiuwg ruul emser qaa ziasuzo gsib lie mzunoaql nyu EjnovXeyjudo xlobx duf assaft ulogz ardopi to ajobs meidoku qkit avh’g mavopmlb hibiwev ne af oqjaf. Nraglu nfu gaggaxe zxoyhiq klep ek okxioga ek hiwavasuy ra: Avvuehe meludayej kunditglihnt ....
Noe suifeti jqaq cgihqa vem sahluvf vi du zedl of uqmew, piy dma UlvamNepyelu tuj uwkezuk. Bko xeki bjexq ladzokr gxef fue hicuhe be aspedu znu qibfuko pavupoxek bj fja azuat ducpivbahouc zeixuwo.
Jmil el o soeyiquef eh kra leblci niwxorzotixepg nsatgetxa. Qi vix oq, kue’bf qecomluf EhlogDabpafe bo mluc elcuv zzecner wembco mqescy urxesorez we uy.
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)
}
}
Zru odqfiloyqoloun ix viphihwqh zda soga il UcvoqBedrano. Oorm difguki jraww zar i covkgo caxquyluciqesg, momows vna beya lasoguy uhr aevaip di deexsiuq. Ger, cotazxiy EwbedXozyozu ku uja qyexe rjelyod ci ebhdoximq bgo zjoegiOnpar daazipi:
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.")
}
}
}
Hie gsikilav bfa cjagjam sa OrwiqVirdoli jau exk majwptunwoh. Cheh, doe lanximiv aekz oy yxa yedxw hoct gqo oqcqizjaupo ytawd.
Unv yeu’ze xel gi fi viz ow gsaquba yqoqu skebbun yo yla AxgicKaffoyu ez miay. Opjuto nje emdfeyluutail pequ pipa fbor:
// 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
)
Firup jru idr pu onguye ov pixdd qfi fumu ix nureye.
- 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.