/** * @author mh, inspired by Mark Lutton, Brookline, NH * @version 1.0 * @date 04.04.2002, updated 13.01.2003 * @package informatik2.hello * @class Hello5 */ // programming to an interface, not an implementation, // -> ADT basic principle -> Design Patterns: // interfaces serve as contract, // implementations may be exchanged, if required; // different implementations may even be used concurrently; public class Hello5 { public static void main (String[] argv) { Mouth mouth = new SimpleMouth("Hello, World!"); mouth.say(); Mouth mund = new CountingMouth("Hallo, Welt!"); mund.say(); mouth.say(); mund.say(); mouth.say(); // assignment of different implementations System.out.println("-----------------"); mouth = mund; mouth.say(); mund.say(); } // main } // Hello5 //--------------------- cut into separate files ----------------------------- interface Mouth { public void say(); } // Mouth //--------------------- cut into separate files ----------------------------- class SimpleMouth implements Mouth { public SimpleMouth (String thought) { what = thought; } // SimpleMouth public void say() { System.out.println(what); } // say private String what; } // SimpleMouth //--------------------- cut into separate files ----------------------------- class CountingMouth implements Mouth { public CountingMouth (String thought) { what = thought; count = 0; } // CountingMouth public void say() { count++; System.out.println("(" + count + ") " + what); } // say private String what; private int count; } // CountingMouth