Command - Functors and the Command Pattern Design Patterns...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Design Patterns In Java Bob Tarr Functors and the Command Pattern
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java Functors And The Command Pattern 2 Functors Functors l Often when designing general-purpose software, we make use of callback functions l A callback function is a function that is made known (registered) to the system to be called at a later time when certain events occur l In C and C++ we can use pointers to functions as a callback mechanism, but this is not available in Java l In Java we must use an object that serves the role of a pointer to a function. (We could also use this technique in C++.) l A functor is a class with usually only one method whose instances serve the role of a pointer to a function. Functor objects can be created, passed as parameters and manipulated wherever function pointers are needed. l Coplien coined the word functor for this type of class
Background image of page 2
Bob Tarr Design Patterns In Java Functors And The Command Pattern 3 Functor Example 1 Functor Example 1 l Consider a Utilities class with a class method that compares two Numbers. We would like to be able to specify the method of comparison at run-time, so we add a Comparator argument to the class method as follows: public class Utilities { public static int compareNumbers(Number a, Number b, Comparator c) { return c.compare(a, b); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java Functors And The Command Pattern 4 Functor Example 1 (Continued) Functor Example 1 (Continued) l The Comparator object is a functor, since it acts like a pointer to the compare() function. To support different types of comparators, we'll use interface inheritance via a Java interface. l The Comparator interface is: public interface Comparator { public int compare(Number a, Number b); } l Many implementations of functors in Java involve the use of Java interfaces in this fashion
Background image of page 4
Bob Tarr Design Patterns In Java Functors And The Command Pattern 5 Functor Example 1 (Continued) Functor Example 1 (Continued) l Here is an Integer comparator: // Integer comparator public class IntComparator implements Comparator { public int compare(Number a, Number b) { int x = a.intValue(); int y = b.intValue(); if (x < y) return -1; else if (x > y) return 1; else return 0; } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java Functors And The Command Pattern 6 Functor Example 1 (Continued) Functor Example 1 (Continued) l And here is a String comparator: // String comparator public class StringComparator implements Comparator { public int compare(Number a, Number b) { String x = a.toString(); String y = b.toString(); if (x.compareTo(y) < 0) return -1; else if (x.compareTo(y) > 0) return 1; else return 0; } }
Background image of page 6
Bob Tarr Design Patterns In Java Functors And The Command Pattern 7 Functor Example 1 (Continued) Functor Example 1 (Continued) l And here is a test program that demonstrates the use of the different comparators: public class TestUtilities { public static void main(String args[]) { // Create an integer Comparator. Comparator c1 = new IntComparator();
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

Command - Functors and the Command Pattern Design Patterns...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online