Strategy and Open/closed Principle
According to the strategy pattern, the behaviors of a class should not be inherited, instead they should be encapsulated using interfaces. As an example, consider a car class. Two possible functionalities for car are brake and accelerate.
Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks: accelerate and brake behaviors must be declared in each new Car model. The work of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.
The strategy pattern uses aggregation instead of inheritance. In the strategy pattern, behaviors are defined as separate interfaces and specific classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at run-time as well as at design-time. For instance, a car object’s brake behavior can be changed from BrakeWithABS
to Brake
by changing the brakeBehavior
member to:
This gives greater flexibility in design and is in harmony with the Open/closed principle (OCP) that states that classes should be open for extension but closed for modification.
Read more about this topic: Strategy Pattern
Famous quotes containing the words strategy, open, closed and/or principle:
“... the generation of the 20s was truly secular in that it still knew its theology and its varieties of religious experience. We are post-secular, inventing new faiths, without any sense of organizing truths. The truths we accept are so multiple that honesty becomes little more than a strategy by which you manage your tendencies toward duplicity.”
—Ann Douglas (b. 1942)
“The role of the writer is not simply to arrange Being according to his own lights; he must also serve as a medium to Being and remain open to its often unfathomable dictates. This is the only way the work can transcend its creator and radiate its meaning further than the author himself can see or perceive.”
—Václav Havel (b. 1936)
“With two sons born eighteen months apart, I operated mainly on automatic pilot through the ceaseless activity of their early childhood. I remember opening the refrigerator late one night and finding a roll of aluminum foil next to a pair of small red tennies. Certain that I was responsible for the refrigerated shoes, I quickly closed the door and ran upstairs to make sure I had put the babies in their cribs instead of the linen closet.”
—Mary Kay Blakely (20th century)
“I sincerely believe ... that banking establishments are more dangerous than standing armies, and that the principle of spending money to be paid by posterity, under the name of funding, is but swindling futurity on a large scale.”
—Thomas Jefferson (17431826)