Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would suggest a <a href="http://en.wikipedia.org/wiki/Proxy_pattern" rel="nofollow noreferrer">Proxy</a>. Here's the <a href="http://java.sun.com/j2se/1.4.2/docs/guide/reflection/proxy.html" rel="nofollow noreferrer">Java example</a>.</p> <pre><code>public interface Bike { public String getWheels(); public int getSize(); } public class MountainBike implements Bike { @Override public int getSize() { return 24; } @Override public String getWheels() { return "Treaded"; } @Override public String toString() { String newLine = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); sb.append("Type: MOUNTAIN").append(newLine); sb.append("Wheels: ").append(getWheels()).append(newLine); sb.append("Size: ").append(getSize()).append(newLine); return sb.toString(); } } public class CruiserBike implements Bike { @Override public int getSize() { return 26; } @Override public String getWheels() { return "Smooth"; } @Override public String toString() { String newLine = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); sb.append("Type: CRUISER").append(newLine); sb.append("Wheels: ").append(getWheels()).append(newLine); sb.append("Size: ").append(getSize()).append(newLine); return sb.toString(); } } public class BikeProxy implements InvocationHandler { private Object obj; public static Object newInstance(Object obj) { return java.lang.reflect.Proxy.newProxyInstance(obj.getClass() .getClassLoader(), obj.getClass().getInterfaces(), new BikeProxy(obj)); } public static &lt;T&gt; T newInstance(String className) { try { return (T) newInstance(Class.forName(className)); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } public static &lt;T&gt; T newInstance(Class&lt;T&gt; bikeClass) { try { return (T) java.lang.reflect.Proxy.newProxyInstance(Bike.class.getClassLoader(), new Class[]{Bike.class}, new BikeProxy(bikeClass.newInstance())); } catch (Exception e) { throw new RuntimeException(e); } } private BikeProxy(Object obj) { this.obj = obj; } public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { Object result; try { result = m.invoke(obj, args); } catch (InvocationTargetException e) { throw e.getTargetException(); } catch (Exception e) { throw new RuntimeException(e); } return result; } } public class ProxyTester { public static void main(String[] args) { Bike mountainBike = BikeProxy.newInstance(MountainBike.class); System.out.println(mountainBike); Bike mountainBike2 = BikeProxy.newInstance(MountainBike.class.getName()); System.out.println(mountainBike2); Bike cruiserBike = BikeProxy.newInstance(CruiserBike.class); System.out.println(cruiserBike); Bike cruiserBike2 = BikeProxy.newInstance(CruiserBike.class.getName()); System.out.println(cruiserBike2); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload