Yesterday I have attended interview in one Leading IT Service company. Technical interview was good, no issues, then I have moved to another set of round about Management, Design and Process. I have answered everything except the below question.
Question asked by interviewer:
Let say you are developing a class, which I am going to consume in my class by extending that, what are the key points you keep in mind? Ex, Class A, which has a method called "method A" returns a Collection, let say "list". What are the precautions you will take?
My Answer: The following points I will consider, such as:
But the interviewer wasn't convinced by my points. He was expecting a different answer from me but I am not able to get his thought process, what he was excepting.
So please provide your suggestions.
The short form of the design thinking process can be articulated in five steps or phases: empathize, define, ideate, prototype and test.
Good design is a concept defined by industrial designer Dieter Rams's principles: It makes a product useful and understandable, is innovative, aesthetic, unobtrusive, honest, long-lasting, thorough to the last detail, environmentally friendly, and involves as little design as possible.
The Three Phases of Design Thinking: Immersion, Ideation and Prototyping. The Design Thinking approach have changed the way thousands of companies think (and do) innovation.
I would want you holding to design principles of Single Reaponsibility, Open/Close, and Dependency Injection. Keep it stateless, simple, and testable. Make sure it can be extended without needing to change.
But then, I wasn't interviewing you.
A few more points which haven't been mentioned yet would be:
public
, protected
, package private
and private
methods. Ensure that you don't expose any more than you actually want to. Removing features is hard. If something is missing from your well designed API, you can add it later. But you expose a slew of useless public methods, you really can't upgrade your API without deprecating methods since you never know who else is using it.If you are returning a collection, the first thing you should think about is should I protect myself from the caller changing my internal state e.g.
List list = myObject.getList();
list.retainAll(list2);
Now I have all the elements in common between list1
and list2
The problem is that myObject may not expect you to destroy the contents of the list it returned.
Two common ways to fix this are to take a defensive copy or to wrap the collection with a Collections.unmodifiableXxxx()
For extra paranoia, you might do both.
The way I prefer to get around this is to avoid returning the collection at all. You can return a count and a method to get the n-th value or for a Map return the keys and provide a getter, or you can allow a visitor to each element. This way you don't expose your collection or need a copy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With