Continuing with the series on Design patterns, I will take up Behavioral
Patterns from today. I have written about Structural
Patterns
and Creational
Patterns
in my previous posts. You can get all the links for them
here
and
here.
Behavioral Patterns
Behavioral patterns are concerned with algorithms and communication
between them. An algorithm that may have been spilt into complex classes
could be hard to maintain. Behavioral patterns helps in identifying the
basis for this split and deals with communication between these classes.
Strategy Pattern
The strategy pattern deals with removing algorithms from its host and
placing them separately. The client program is given a context to select
the desired algorithm in a simple way.
As you can see in the above example, we have 2 strategies to sort a
list. There is a context class which is exposed to the client where it
can select a particular strategy. We can use the strategy pattern when
we have different classes only differing in behavior or when there are
different algorithms and we have to give the selection to the client.
In my next post I will write about the State Pattern.
Comments