My Blog Posts

Use of Solid Design Principle with Laravel 2

Use of Solid Design Principle with Laravel 2


What is It — The Principle of Off

By official recognition

Software entities are turned on for extension, but must be turned off for change.

Entities can be classes, modules, functions, and so on. Definition; changing existing code means extending functionality by adding new code instead.
It's quite confusing to understand by the head. Then let's see it with the examples in Laravel.

Let's say we have an e-commerce system that includes payments. I mean, we've got PaymentControllerand this controller, right now Share1st method credit card with two payment methods ) 2. Our method Paypal and the codes look like this:

PaymentController.php

And PaymentThis is how our class is:
Payment.php

What happens when we want to add a new payment method, say bank transfer

Sharemethod, and to call an appropriate incoming method Paymentclass.
So Sharemethod looks like this:
PaymentController.php

And Paymentchange class :
Payment.php

So what's going to happen next? Like If we have another payment method called cash ifPaymentWe added another one to the class and then a new method. Another payment method, Coupon Payment, is coming and we're changing classes again. Remember what this policy says, open for extension but close to changes. That's why we're breaking that principle here.
If so, how to fix it, let's apply the Open Shutdown Policy here.

about interfaces in PHP More Learn interfaceLet's do something new with a method called .Share
PayableInterface.php

classMake a payment for each payment method and PayableInterfaceExpand with .
CreditCardPayment.php

PaypalPayment.php

CreditCardPayment.php

Create a new class now
PaymentFactory.php

And finally our Shareour method
PaymentController.php. This payment method PaymentFactoryand the factory handles everything itself.

That's it. If the new payment method comes in, we're now solving the problem of changing classes, expanding functionality by adding a new class instead of changing what's available.

In this article, we learned how to make the second letter of the concept of solid, His, i.e. the open-closed principle. I hope this spelling has been useful.



Share this article


Comments (0)

Comment