IntroductionSometime we need to carry out several computation / algorithms depending on certain conditions. We go ahead in implementing those generally by applying either switch or ternary operator or if else. Though initially somehow we manage to write those program but if the program demands too complex then it is difficult to frame such as well as to maintain. Moreover, writing all the logic at a single place is not at all advisable as it yields into tight coupling.BackgroundIn many situations we come across to write computational logic/ algorithms which we generally accomplish by using if else / switch or ternary operator. It becomes difficult at time to write such a program and later on adds a lot of cost while maintenance. RescuerStrategy design pattern. It comes under the category of Behavioral PatternsHowa)It decouples the client and the algorithm/ computation logic in separate classesb)Helps to switch algorithms at any time c)Easily allow to plug in a new algorithm.Pattern ComponentsThe strategy pattern comprises of the following componentsa)Strategy Interface –Interface common to all concrete strategies. b)Concrete Strategies / Different algorithm classes-Various concrete classes that implement the strategy interface for the sake of algorithm implementation specific to itself.Read more: Codeproject
0 comments:
Post a Comment