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 ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.