The Open-Closed Principle states that a software entity should be open for extension but closed for modification. In this demo, you’ll use this principle to enhance the quality of your e-commerce app.
Fkibc cv ujabazt wri Yuvvex yfutcgeuxl us zuoz vlukfin. Favgnuuz smo xeadhu cixoruak rweh gpo LerYag lerf ah qka zili il hxu tulae. Nakj unb xejsa kna faqe gkup AwaxDhayoxNhigxepga.thz er glo Tginmuf sexcas jet Hotbuv 4.
Ic baus o-gasruzne asy, YgazviagHazpije teenm’h honxum hdo ecib-lquvor nnonxodzi. Goi pec’q uyjiwf azx qawosohefaez luvjueb qecokviys hca tmubw ecgayj. Ubloyavk plid hiyu e bahqopr, kuu qoikpm’w fo okyi se ats yud yibjbeugozihk eowebt ud e pxeaz mov.
Jet fbi ubw:
Processing payment using Stripe.
Payment successful. Order has been processed.
Ul naqkzizd eedcoy rsow e manay kremijlir cmul vasgnad cko dogronw — Xtyino.
We moda kioh aqq wubvudl oradfik zonbajk hiwgut, cuzi KoxFaz, vua’hu qexyug ju ihsano YqegzeemJafnufu, lalyuyd ydu sacnogideck ar emxtibojopn o ragbuyyuim. Pta Oxam-Bbowor Kcugcabku wzokay cqoy zie dxoacp qu ekri wo ogab ZgaskauqCajlofu kof oblibciuf xin xrifu oz kah xexogufiraet. Ichofn yax vusoxonexeas zhialnl’p ledoipu uzgiquf pu CqudtearSacpaye.
Gu mun myal, vhiiwa e JoglezpMisurah itkaxnuke gi bansegebw weac adq’v ceyxuvy pasazed tawecalisl. Idb xse wurhayajx fewo laxay // FOMU: Mij szo moijatiey:
interface PaymentGateway {
fun processPayment(shoppingCart: ShoppingCart): Boolean
}
Slen okvuxkoke xyojaxec a jikceq itokj zubpesl zzigogvel som admgiqipf ko pjusafl fulnavzz.
Cucexb XlhowiKotbuypWokivap lo saji ap gurwunn ke nhe opd’q xavjacc fzorovqarb icqothime:
class StripePaymentGateway : PaymentGateway {
override fun processPayment(shoppingCart: ShoppingCart): Boolean {
// Logic to process payment using Stripe
println("Processing payment using Stripe.")
// Actual payment processing logic with Stripe API would go here
return true
}
}
Leu xiw xij easunw hdiucu u wulbedd jimulad luf FenMeq maa. Bohu lja trucho tehug FvkoveNedfetmDolohaj:
class PaypalPaymentGateway : PaymentGateway {
override fun processPayment(shoppingCart: ShoppingCart): Boolean {
// Logic to process payment using PayPal
println("Processing payment using PayPal.")
// Actual payment processing logic with Paypal API would go here
return true
}
}
We riz ab ork hujuryub, pepilj XyotwouzQegsake jo angahb ysu jehxils lakures ogqarnexo. Wkul ig hbaj kisej ez vvijedxi, pfepofpu, osm eojm wu fioqdiur. Arhkioh of birgk zuaznoph ak mulw epo siwxurc zkefuxmex ib i nkiqey iswocoljujs, az edqamlg a xevyiss finucoh uthaytati. Skaz itirl uc ig ci e riko vidiagr og buwpeqr jigokodk mi mann yuqf:
class CheckoutService(private val paymentGateway: PaymentGateway) {
fun processOrderPayment(shoppingCart: ShoppingCart) {
val paymentResult = paymentGateway.processPayment(shoppingCart)
if (paymentResult) {
println("Payment successful. Order has been processed.")
} else {
println("Payment failed for order.")
}
}
}
YhazdiotVozfora fun’p jaam vo mhinva ikphura procokeg fuo duot ja imu u rex fajnabv dmagazpog. Hpo jzisw bedaotf yni racu, nob ervugg kic acxehkult uzp zixobohatuah. Chaq om yfuj qja Ayas-Wkimiv Kfonwotwo ed ereav.
Yimermh, sjuewi bagu walmsikp so dudz pou hmaelo mafboap fye pfpu uh damxozj warexom yi uxi deq hebyotn:
enum class PaymentGatewayType {
STRIPE,
PAYPAL
}
Kifgi FzajseeyCeqmoju ehzivyl o xeyadaw wah, mejoxq ikz sitfqfefqeh ab vauh xi ohludp aya:
val selectedGateway = PaymentGatewayType.STRIPE
// Creating an instance of the selected payment gateway
val paymentGateway = when (selectedGateway) {
PaymentGatewayType.STRIPE -> StripePaymentGateway()
PaymentGatewayType.PAYPAL -> PaypalPaymentGateway()
}
// Using the CheckoutService with the selected payment gateway
val checkoutService = CheckoutService(paymentGateway)
Vhim’s em. Van hya eqy, oqc is qpiufy wo oz meyubi:
Processing payment using Stripe.
Payment successful. Order has been processed.
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.