group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
270 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
30 Jan 2019

Strategy Design Pattern

Strategy Design Pattern -


1.Here what we do is instead of having a method which may or may not be actually part of sub-classes


2.What we do is ,  we create a interface and  have one or two default implementation of the interface and have that as a property in our class , if we don't set its null and the respective methods don't work else it works .


3.Having it this way we can set such behavior to a particular sub-class 


4.Let's take an example suppose there's a duck class which has three sub classes

city duck and wild duck and plastic duck .


5.We want to add flying behavior ,but plastic duck can't fly 


6.So we create a interface fly and implement it in classes simplefly and can'tfly. 


7.And put this fly interface as a property in duck base class  with setter and a function that invokes the fly method of this property as a variable ...which is also called composition .


8.So if we have wild or city duck we set the simplefly property 


9.While if its plastic duck we set can't fly property  .


The client/user/We can also provide his only implementation of the interface and set the property of duck class.


There are many more advantages of using this pattern .Explore..