Have you flustered in a recent technical interview or are you preparing for an up-coming interview? Many high-tech companies are screening candidates using textbook questions before bringing them in for further interviews. This article contains typical design pattern questions that interviewers will ask. Pay attention.
The singleton pattern permits only one copy of the particular class to be instantiated. Thus, there should only be at most one instantiated object at any given point in time. This fundamental question is not difficult to solve. Simply declare the constructor as private and provide a visible method to instantiate the object. The separate method will then check for the existence of the object before creating one.
Interviewers may touch up this question by adding concurrency conditions. In Java, one can easily include concurrency by adding the word "synchronized* as part of the method signature. However, this does not work for static methods, which is typically used in the Singleton Pattern. In this case, simply have a private static field that is instantiated statically. Then in the method, you can return this singleton object concurrently. But the problem becomes even harder if the interviewer requires that the object be instantiated lazily; otherwise known as Lazy Instantiation, another design pattern.
As a follow up, you can synchronize on the field declaration of the singleton object in the code block where the check is and then return.
The Observer Pattern is a typical push and subscribe design. Here, we have a set of subscribers that want to be notified of some events or messages that is published by the publishers. This design is often used for asynchronous designs such as mail systems. It can also be used in user interface designs.
This pattern is very useful for encapsulating detailed and complex implementations while abstracting a common interface. It is widely used.
For example, a sorting class my consist of many types of sorting algorithms, which includes quicksort, bubble sort, and others. We can abstract an interface that a user can call, such as ".sort()", which in the implementation, we can dynamically swap the algorithms that perform the sort. For example, using bucket sort for numeric input, and quicksort for all other sorts.
You should be able to ace the interview.
1LZHYh I loved your blog post.Really looking forward to read more. Great.
Very neat blog post.Much thanks again.
Major thankies for the blog.Much thanks again. Want more.
This is one awesome blog article.
http://www.maccosmeticwholesaler.com/check my reference mac cosmetics http://maccosmeticswebsite.webs.com/