BT Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
package creational;
class A {
public void method1() {
}
public void method2() {
}
public void method99() {
}
public void method100() {
}
}
public class factoryAdapter {
private A factory;
public String adaptMethod(String me) {
factory = new A();
if (me.equals("method1")) {
factory.method1();
System.out.println("method1");
return "method1";
}
if (me.equals("method2")) {
factory.method2();
System.out.println("method2");
return "method2";
}
if (me.equals("method99")) {
factory.method99();
System.out.println("method99");
return "method99";
}
if (me.equals("method100")) {
factory.method100();
System.out.println("method100");
return "method100";
}
return "nothing";
}
public static void main(String[] args) {
factoryAdapter fa = new factoryAdapter();
fa.adaptMethod("method1");
fa.adaptMethod("method2");
fa.adaptMethod("method99");
fa.adaptMethod("method100");
}
}
Hi Andi,
- Orpos December 01, 2012If you can change the class A then you can create an interface, that class A implements, with all methods of class A. Then you can use the proxy design pattern in order to log entries and exists of any method.
But, if you can't change class A, then you can use adapter pattern.