Aj is article me ham parhen gen Strategy Pattern ke
bare me ke Design Patterns kia hoten hain? Strategy Pattern kia
hay, Uske ilawa Php Design Patterns ke bare me kuch general concepts aur
phir ham sikhen gen ke PHP me kis tarah ham is strategy pattern ka use krskte
hain.
What is Design Pattern?
Design pattern ko simply agar me define krun to “Design
Patterns are solution of commonly occurring problem”. Iska matlab yeh hay
ke aisi problems jo bar bar ek programmer ko face krni parti hay uska solution
pehle hi kisi programmer ne bana diya hay (Gang of Four)
Strategy Pattern:
Strategy ka matlab
hay ke koi plan ya action. Strategy design pattern ek behavioral design pattern
hay jis ko use krke ham kisi bhi object
ki strategy ko run time per strategy object ko use krke change krskte hain.
Iska matlab yeh hua ke ham run time per ek object kay
behavior ki implementation ko change krden, Behavior same rhe ga just implementation
different hogi, Jese aksar hamen zarorat
parti hay runtime per kay ek application me ek hi behavior ki different implementation
use krni hoti hay condition ki bina per to wahan ham ye pattern use krte hain.
Isko mazeed samajhne ke liye ham ek example samajhte hain.
Suppose ek ecommerce website hay jahan per different cars bhi sell hoti hain,
Ab har car buy krne per kuch discount hay jiske liye car ko buy krte we user ko ek coupon lagana pare ga aur iske liye hamen har page per car ke hisab se us
coupon ko show karana hay.
Jese agar user ne BMW car select ki to uske listing page per ajae ke “is car
per 10% discount hay aur uske liye ye coupon hay etc”
Ab yahan per ham dekhen to jo car select hui uske hisab se
se hamen coupon show karana hay, Yani hamara behavior same hay ke hamen coupon
generate krke show karana hay but har car ke liye coupon different hoskta hay
yani implementation change hoskti hay.
Yani ham kehskte hain ke hamen car ke hisab se strategy change krni hay coupon show karane ke liye, To ye example me ham ek bohat acha use krskte hain strategy pattern ka.
Strategy Pattern me hamare pas 3 main classes hoti hain.
1-Interface
2-Algorithm Classes
3-Client Classes
Algorithm classes basically ek group of behavior ko represent
kr rhi hain, Jese agar ham coupon generator wali example len to har car ke liye
coupon generate krne ke liye ek alag class bane gi.
Suppose hamare pas filhal 2 cars hain, i.e BMW and Mercedes.
Ab donu car ke liye alag coupon generator hoga yani do behavior class
(Algorithm) baney gen.
Let say unka name hoga BMWCouponGenertor and MercedesCouponGenerator
Ye donu classes ek interface ko implement karen gen take
hamare code me abstraction barqarar rhe !
Ab hamare pas har car ke liye coupon generate krne ke liye
classes ban gyi, Ab hamen in class ke behavior ko kahin use bhi krna hoga aur wo
kam ham karen gen client class me.
Client class basically ab inputs ke accordingly run time per
in do behavior classes me se kisi ek kay object ko create krey gi.
Yaha per behavior classes basically strategies hain aur jo
class in strategies ko use kr rhe hay yani client class, usko ham kahen gen ke
ham context class.
Concept ke baten hogaen ab finally ham code per ate hain
CouponGenerator.php ke name se file create kijiye aur usme
ye code paste kijiye:
1. <?php
2.
3. interface CouponGenerator{
4.
5. public function addCoupon();
6. }
7.
8.
9. ?>
Hamne ek interface create kiya hay jis me hamne simply ek
method add kiya hay addCoupon() ka. Ye method har behavior class jo is
interface ko implement kre gi me coupon add krde ga.
Similarly Mercedes aur BMW kay coupon generate krne ke liye ye
files create kijiye.
MercedesCouponGenerator.php
1. <?php
2.
3. include_once("CouponGenerator.php");
4. class MercedesCouponGenerator implements CouponGenerator{
5.
6. private $coupon;
7. public function addCoupon(){
8. return $this->coupon="HY76";
9. }
10. }
11.
12. ?>
BMWCouponGenerator.php
1. <?php
2.
3. include_once("CouponGenerator.php");
4. class BMWCouponGenerator implements CouponGenerator{
5.
6. private $coupon;
7. public function addCoupon(){
8. return $this->coupon="78TYK";
9. }
10. }
11.
12. ?>
In donu classes me hamne CouponGenerator interface ko implement
kiya then ek member variable coupon initialize krwaya aur phir addCoupon() method
me usme kuch value add krdi aur phir coupon member variable ko return krdiya.
Finally client class ke liye ye code place kijiye is
filename ke sath
CouponGeneratorClient.php
CouponGeneratorClient.php
1. <?php
2.
3. include_once("MercedesCouponGenerator.php");
4. include_once("BMWCouponGenerator.php");
5. class CouponGeneratorClient{
6.
7. private $car;
8. public function __construct($carName){
9. if($carName=="mercedes"){
10. $this->car=new MercedesCouponGenerator();
11. }else{
12. $this->car=new BMWCouponGenerator();
13. }
14. }
15.
16. public function getCoupon(){
17. $coupon="Coupon for this Car is {$this->car->addCoupon()}";
18. return $coupon;
19. }
20. }
21.
22. ?>
Ab jab client class ka object create hoga ke to iske constructor
me car ka name pass hoga, Constructor car name ke hisab se Car Coupon Generator class ka object create
krde ga aur usey member variable $car me store krde ga.
Ab hamen coupon chaiye us car ka jiska object create hua to
simple hamne ek method banaya hay getcoupon() ka aur usme ham Car coupon
generator class ka addCoupon method call kr rhe hain aur uska result $coupon variable
me store krke usey return kr rhe hain.
Aur in last ham index.php me jahan per ham car ka name get
kr rhe hain aur phir ham car ke hisab se coupon show krwa rhe hain. Is file me
ye code place kijiye
1. <?php
2.
3. include_once("CouponGeneratorClient.php");
4. $coupon=new CouponGeneratorClient("bmw");
5. echo $coupon->getCoupon();
6.
7. ?>
Yahan per simply hamne client class ka object create kia aur
us ke constructor me car name pass krdiya aur phir uske coupon method ke result
ko echo krdiya.
Ap soch rhe hoge ke ye kam to simply if else condition se
bhi hoskta tha, Lekin hamne phir strategy pattern ko q use kia?
To yad rkhiye ye ek bohat hi simple se example hay lekin
isme bhi ham dekh skte hain index.php me kitna clean code hay koi if else
condition nh, Agar code complex ho tab apko iski asli need samajh ae gi.
I hope apko ye Strategy Pattern samajh aya hoga, Yad
rkhiye programming concept ke sath sath practice ka bhi name hay to isliye is
pattern ki practice krne mat bhooliye ga, Apna editor khole aur shuru hojaen !
And Remember Sharing is Caring !
Good and Precise!
ReplyDeleteThanks Bro.
Delete