|
Note: This pattern is taken from the The ServerSide.com.
|
|
import java.io.*; public interface InterfaceAlgorithm { public void executeAlgorithm(); } // End of interface // Beginning of class class Algorithm1 implements InterfaceAlgorithm { public void executeAlgorithm() { prt(); } public void prt() { System.out.println(" Class is Algorithm1."); } }// End of class // Beginning of class class Algorithm2 implements InterfaceAlgorithm { public void executeAlgorithm() { prt(); } public void prt() { System.out.println(" Class is Algorithm2."); } } // End of class // Beginning of class class Context { public Context(InterfaceAlgorithm algo){ _algo = algo; } public void contextInterface() { _algo.executeAlgorithm(); } private InterfaceAlgorithm _algo; } //End of class public class Driver { public static void main(String[] args) { Algorithm1 a1 = new Algorithm1(); Algorithm2 a2 = new Algorithm2(); Context context = new Context(a1); context.contextInterface(); } } |