Objectives In this chapter, we will introduce design patterns. The main focus
of this chapter is designing reusable, and flexible components using abstract
classes, interfaces, and design patterns. We will discuss several important
design patterns for abstraction: Template Method, Strategy, Factory, and
Iterator. A case study will also be presented.
Contents 5.1 Design Patterns
5.2 Designing Generic Components
5.2.1 Factorization
5.2.2 Generalization
5.2.3 Abstract Coupling
5.3 Design Case Study_ Sorting Animation
5.3.1 The Initial Implementation
5.3.2 Separating the Algorithms
5.3.3 Separating the Display Strategies
This example demonstrates factorization by inheritance. A generic animation
applet class AnimationApplet is defined by extracting the common
elements of animation applets, the methods start(), stop(), and run().
Then, the digital clock applet in Example 3.10 is re-implemented using
the AnimationApplet class. Hence, there are two Java programs to
download.
Example 5.2: Double Buffered Generic Animation Applet
This example illustrates how to resolve the complications involved in factorization
by inheritance. It also addresses issues concerning flexibility, i.e.,
adaptability, in the generic animation applet. A double-buffered generic
animation applet class DBAnimationApplet is defined using factorization
by inheritance. Then, the bouncing ball applet is re-implemented using
the DBAnimationApplet class. Again, there are two example Java programs
to download.
The Plotter applet is a generic funtion plotter applet for plotting arbitrary
single variable functions on a two-dimensional canvas. This example shows
several concrete programs in action.
This Plotter applet demonstrates the Template Method design pattern. This
is a generic funtion plotter applet for plotting arbitrary single variable
functions on a two-dimensional canvas. This example implements two concrete
functions, Sine and cosine.