You can create an interface (or use an existing one) and create anonymous implementations (
Java 7 and before) or lambda's (Java 8) for them. For example, in Java 7:
Java 8 makes it a bit easier, also because there are already a lot of matching existing interfaces. For instance, using java.util.function.Consumer: