oop - Advantages of using strategy pattern in php -
i can't seem head around advantages strategy pattern offer. see example below.
//implementation without strategy pattern class registry { public function func1(){ echo 'called function 1'; } public function func2(){ echo 'called function 2'; } } $client = new registry(); $client->func1(); $client->func2(); //implementation strategy pattern interface registry { public function printmsg(); } class func1 implements registry { public function printmsg(){ echo 'called function 1'; } } class func2 implements registry { public function printmsg(){ echo 'called function 2'; } } class context { public function printmsg(registry $class){ $class->printmsg(); } } $client = new context(); $client->printmsg(new func1()); $client->printmsg(new func2()); in above 2 example advantages strategy pattern offer , how better first approach? why should use strategy pattern?
the above example code might contain errors please ignore code.
the intent of strategy pattern to:
define family of algorithms, encapsulate each one, , make them interchangeable. strategy lets algorithm vary independently clients use it. [gof:349]
to understand means, have (emphasis mine)
consider should variable in design. approach opposite of focusing on cause of redesign. instead of considering might force change design, consider want able change without redesign. focus here on encapsulating concept varies, theme of many design patterns. [gof:29]
in other words, strategies related pieces of code can plug client (another object) @ runtime change behavior. 1 reason this, prevent having touch client each time new behavior added (cf. open closed principle (ocp) , protected variation). in addition, when got sufficiently complex algorithms, putting them own classes, helps adhering single responsibility principle (srp).
i find example in question ill-suited grasp usefulness of strategy pattern. registry should not have printmsg() method , cannot make sense of example such. easier example example give in can include code php class? (the part talk strategy begins halfway down answer).
but anyway, in first code, registry implements methods func1 , func2. since assume these related algorithms, let's pretend persisttodatabase() , persisttocsv() have wrap our mind around. let's imagine that, @ end of application request, call 1 of these methods shutdown handler (the client).
but method? well, depends on configured , flag stored in registry itself. in client end like
switch ($registry->persiststrategy) { case 'database': $registry->persisttodatabase(); case 'csv': $registry->persisttocsv(); default: // not persist database } but switch statements bad (cf. cleancode:37ff). imagine customer requests add persisttoxml() method. not have change registry class add method, have change client accommodate new feature. that's 2 classes have change, when ocp tell our classes should closed modification.
one way improve add generic persist() method on registry , move switch/case client needs call
$registry->persist(); that's better still leaves switch/case , still forces modify registry each time add new way persist it.
now imagine product framework used many developers , come own persist algorithms. how can add them? they'd have extend class they'd have replace occurrences in framework yours used. or write them class, they'd have patch class each time provided new version of it. that's can of worms.
strategy rescue. since persist algorithms stuff varies, encapsulate them. since know how define family of algorithms, i'll skip part , show resulting client:
class registry { public function persist() { $this->persistable->persist($this->data); } public function setpersistable(persistable $persistable) { $this->persistable = $persistable } // other code … nice, refactored conditional polymorphism. , other developers can set whatever persistable desired strategy:
$registry->setpersistable(new persisttocloudstorage); and that's it. no more switch/case. no more registry hacking. create new class , set it. strategy lets algorithm vary independently clients use it.
also see
- how strategy pattern work?
- https://softwareengineering.stackexchange.com/questions/187378/context-class-in-strategy-pattern
- http://sourcemaking.com/design_patterns/strategy more explanation.
- https://www.youtube.com/watch?v=-ncgrd9-c6o
end notes:
[gof] gamma, e., helm, r., johnson, r., vlissides, j., design patterns: elements of reusable objectoriented software, reading, mass.: addisonwesley, 1995.
[cleancode] martin, robert c. clean code: handbook of agile software craftsmanship. upper saddle river, nj: prentice hall, 2009. print.
Comments
Post a Comment