Tutorial#12 The Strategy Pattern
In this tutorial, you will do a practical exercise about the Strategy pattern.
The Strategy pattern is one of the most fundamental design pattern. It embodies two object-oriented design principles—encapsulate the concept that varies and program to an interface, not an implementation.
The Design Patterns authors define the Strategy pattern as:
Define a family of algorithms, encapsulate each one, and make them interchangeable. [The] Strategy [pattern] lets the algorithm vary independently from clients that use it.
The following suggests a good article for you to learn the Strategy pattern. It first illustrates how Swing uses the Strategy pattern to draw borders around its components. Then it discusses how Swing benefits by using the Strategy pattern, and finally it explains how you can implement the Strategy pattern in your own software.
Discussion: Suppose JComponent.paintBorder() method use switch-case to handle drawing borders around its components, instead of using the Strategy pattern. What you need to do if you tried to implement a new border type? What are the difficulties that you will be faced with?
* * *
No comments yet
Sorry, the comment form is closed at this time.