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:
“That is the way of youth and life in general: that we do not understand the strategy until after the campaign is over.”
—Johann Wolfgang Von Goethe (17491832)
“The confusion is not my invention. We cannot listen to a conversation for five minutes without being aware of the confusion. It is all around us and our only chance now is to let it in. The only chance of renovation is to open our eyes and see the mess. It is not a mess you can make sense of.”
—Samuel Beckett (19061989)
“On a flat road runs the well-trained runner,
He is lean and sinewy with muscular legs,
He is thinly clothed, he leans forward as he runs,
With lightly closed fists and arms partially raised.”
—Walt Whitman (18191892)
“We have been here over forty years, a longer period than the children of Israel wandered through the wilderness, coming to this Capitol pleading for this recognition of the principle that the Government derives its just powers from the consent of the governed. Mr. Chairman, we ask that you report our resolution favorably if you can but unfavorably if you must; that you report one way or the other, so that the Senate may have the chance to consider it.”
—Anna Howard Shaw (18471919)