BT Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Andi,

If 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.

- Orpos December 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
	}
}

- Sandesh Sharma January 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution will change the calling program . is that ok ?

- Anonymous December 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If class A is not final, create a class B which extends class A. Override all methods in Class A in B. Log entry, call super.method, log exit. And from factory class, create and return object of class B.

- Basil Kurian July 13, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More