You are here

Strategy Pattern

15 January, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

JFrame is not responsible for arranging the GUI components that it contains. Instead it delegates such a task to its layout manager, LayoutManager. There are many concrete layout managers: FlowLayout, BorderLayout, GridLayout, etc. that arrange the internal components diferently. There are said to be diferent strategies for layout. The interaction between JFrame and its layout manager is said to follow the Strategy Pattern.

The strategy pattern is powerful and important design pattern. It is based on that principle of delegation that we have been applying to model many of the problems so far. It is a mean to delineate the invariant behavior of the context (e.g. JFrame) and the variant behaviors of a union of algorithms to perform certain common abstract task (e.g. LayoutManager). We shall see more applications of the strategy design pattern in future lessons.

At this point the only the buttons in the Frame1 example do not perform any task besides "blinking" when they are clicked upon. The example in the next lecture will show how to associate an action to the click event.

Click here to download code samples.