We will have an interface which the client uses to consume the implementation via an abstraction. The implementation of the interface registers itself with the abstraction.
public interface IActionPerformer { public void performAction(); }Abstraction
public class ActionController { private static Set<IActionPerformer> performers=new HashSet<IActionPerformer>(); public static boolean registerPerformer(IActionPerformer performer){ return performers.add(performer); } public static void perform(){ Iterator it=performers.iterator(); ((IActionPerformer)it.next()).performAction(); } }Implementation
public class ActionPerformerImpl implements IActionPerformer { static{ ActionController.registerPerformer(new ActionPerformerImpl()); } public void performAction() { System.out.println("this impl can be independent of client"); } }the client
public class ActionClient { public static void main(String...strings ){ try { Class.forName("dp.bridge.ActionPerformerImpl"); } catch (ClassNotFoundException e) {} ActionController.perform(); } }
No comments:
Post a Comment