O'Reilly logo

Design Patterns Java™ Workbook by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Function Decorators

You can combine DECORATOR with the idea of treating functions as objects to allow for runtime composition of new functions. In Chapter 4, Facade, you refactored an application that shows the flight path of a nonexploding aerial shell. The original code calculated the path in the paintComponent() method of a JPanel subclass:

 public void paintComponent(Graphics g) { super.paintComponent(g); // paint the background int nPoint = 101; double w = getWidth(); double h = getHeight(); int[] x = new int[nPoint]; int[] y = new int[nPoint]; for (int i = 0; i < nPoint; i++) { // t goes 0 to 1 double t = ((double) i) / (nPoint - 1); // x goes 0 to w x[i] = (int) (t * w); // y is h at t = 0 and t = 1, and y is 0 at t = .5 y[i] = (int) (4 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required