Design Patterns based Android interview questions and answers
1.Creational Design Pattern
Factory Pattern
Abstract Factory Pattern
Singleton Pattern
Prototype Pattern
Builder Pattern.
What is singleton pattern?
Singleton is a most commonly used design pattern and is used when we want one and only one instance of a class per JVM. Classes developed to perform business logic is good example of Singleton class.
Name one singleton class in Java?
Runtime
How we can create a class singleton?
To make any class singleton, we need to consider or implement following-
- Private or protected constructor- We need to make the constructor of a class private so that no one from outer world can call a “new”.
- Create a private static instance of the class.
- Create a public static method which will return the instance created.
Explain early and lazy loading of singleton class?
We can have an early and lazy loading of a singleton class. In case of early loading, an object of singleton class is instantiated while loading of a class where as in case of lazy loading object is crated on first request. While using lazy loading we need to consider synchronization as ignoring it might end up with having multiple objects in multi threaded applications.
Explain double check locking in singleton class?
Synchronizing complete method in case of lazy loading of a singleton classes can have a performance issues in multi threading applications so double check locking is a way to synchronize a block and check if the instance is null twice. Refer below example
Can we break singleton?
Answer- Yes, one can create multiple objects using
- Serialization/deserialization if singleton class is marked as serializable. So we should not mark singleton class as serializable and in case class is serializable via inheritance, override readObject() method and throw Non serializable exception.
- Cloning- if singleton class is marked as cloneable. So we should not mark singleton class as Cloneable and if class is cloneable (by parent class), override clone method and throw CloneNotSupported exception.
- Using reflection as reflection can access private fields. So we can apply check in constructor and throw exception if instance is not created from constructor.
How to avoid Singleton class instance creation through Reflection ?
To avoid instance creation through reflection, throw exception from constructor.
private Singleton() {
if( Singleton.singleton != null ) {
throw new InstantiationError( “Creating of this object through reflection is not allowed.” );
}
}
How to avoid multiple singleton creation during deserialization ?
Avoid multiple singletons during deserialization using ReadResolve().
protected Object readResolve() {return instance;}
How to avoid cloning of the Singleton instance ?
To prevent cloning implement Cloneable interface and throw the cloneNotSupportedException from clone() method.
Why singleton is considered an Anti-pattern ?
– Singletons aren’t easy to handle with unit tests. You can’t control the instantiation and by their very nature may retain state across invocations.
For that reason the principle of dependency injection is popular.
Factory Pattern
Abstract Factory Pattern
Singleton Pattern
Prototype Pattern
Builder Pattern.
What is singleton pattern?
Singleton is a most commonly used design pattern and is used when we want one and only one instance of a class per JVM. Classes developed to perform business logic is good example of Singleton class.
Name one singleton class in Java?
Runtime
How we can create a class singleton?
To make any class singleton, we need to consider or implement following-
- Private or protected constructor- We need to make the constructor of a class private so that no one from outer world can call a “new”.
- Create a private static instance of the class.
- Create a public static method which will return the instance created.
Explain early and lazy loading of singleton class?
We can have an early and lazy loading of a singleton class. In case of early loading, an object of singleton class is instantiated while loading of a class where as in case of lazy loading object is crated on first request. While using lazy loading we need to consider synchronization as ignoring it might end up with having multiple objects in multi threaded applications.
Explain double check locking in singleton class?
Synchronizing complete method in case of lazy loading of a singleton classes can have a performance issues in multi threading applications so double check locking is a way to synchronize a block and check if the instance is null twice. Refer below example
Can we break singleton?
Answer- Yes, one can create multiple objects using
- Serialization/deserialization if singleton class is marked as serializable. So we should not mark singleton class as serializable and in case class is serializable via inheritance, override readObject() method and throw Non serializable exception.
- Cloning- if singleton class is marked as cloneable. So we should not mark singleton class as Cloneable and if class is cloneable (by parent class), override clone method and throw CloneNotSupported exception.
- Using reflection as reflection can access private fields. So we can apply check in constructor and throw exception if instance is not created from constructor.
How to avoid Singleton class instance creation through Reflection ?
To avoid instance creation through reflection, throw exception from constructor.
private Singleton() {
if( Singleton.singleton != null ) {
throw new InstantiationError( “Creating of this object through reflection is not allowed.” );
}
}
How to avoid multiple singleton creation during deserialization ?
Avoid multiple singletons during deserialization using ReadResolve().
protected Object readResolve() {return instance;}
How to avoid cloning of the Singleton instance ?
To prevent cloning implement Cloneable interface and throw the cloneNotSupportedException from clone() method.
Why singleton is considered an Anti-pattern ?
– Singletons aren’t easy to handle with unit tests. You can’t control the instantiation and by their very nature may retain state across invocations.
For that reason the principle of dependency injection is popular.
2. Structural Design Pattern Android Design Pattern
Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Facade Pattern
Flyweight Pattern
Proxy Pattern
Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Facade Pattern
Flyweight Pattern
Proxy Pattern
3. Behavioral Design Pattern
Chain Of Responsibility Pattern
Command Pattern
Interpreter Pattern
Iterator Pattern
Mediator Pattern
Memento Pattern
Observer Pattern
State Pattern
Strategy Pattern
Template Pattern
Visitor Pattern
Chain Of Responsibility Pattern
Command Pattern
Interpreter Pattern
Iterator Pattern
Mediator Pattern
Memento Pattern
Observer Pattern
State Pattern
Strategy Pattern
Template Pattern
Visitor Pattern
Android Design Pattern
MVC
MVP
MVVM
MVC
MVP
MVVM
1) Difference between MVC & MVP & MVVM?
MVC is the Model-View-Controller architecture where model refers to the data model classes. The view refers to the xml files and the controller handles the business logic. The issue with this architecture is unit testing. The model can be easily tested since it is not tied to anything. The controller is tightly coupled with the android apis making it difficult to unit test. Modularity & flexibility is a problem since the view and the controller are tightly coupled. If we change the view, the controller logic should also be changed. Maintenance is also an issues.
MVP architecture: Model-View-Presenter architecture. The View includes the xml and the activity/fragment classes. So the activity would ideally implement a view interface making it easier for unit testing (since this will work without a view).
Sample Implementation
MVVM: Model-View-ViewModel Architecture. The Model comprises data, tools for data processing, business logic. The View Model is responsible for wrapping the model data and preparing the data for the view. IT also provides a hook to pass events from the view to the model.
Sample Implementation
2) What is MVVM?
MVVM stands for Model-View-ViewModel. Android applications built using MVVM are more robust, fast, scalable and very less prone to memory leaks. In MVVM ViewModel is the key between View and Model.
The important key point about MVVM is that View has reference to the ViewModel but not in reverse and that makes the application’s UI-Components related memory leak-proof. Each View may have a dedicated ViewModel or may be shared by multiple in case of fragments to enable sharing data cross fragments of an activity. MVVM is a more suitable candidate for automated testing
3) Why Google is using MVVM over MVP?
I would advise MVVM, it is the pattern of the future. And it is the only architecture that can give you full testing without dependencies. Once you get your head around the data-binding concept, it is super easy. In MVVM we are using Android Architecture Components. There LiveData and ViewModel are the main components which will help in resolving the view dependency.
LiveData is an observable data holder class which is Lifecycle state of the Activity or Fragment. And ViewModel is like the state representation of UI which stores and manages UI-related data in a lifecycle conscious way. It will also save data when screen orientation changed.
MVP is tight coupling, so there are many things that are difficult to test
What is the role of Presenter in MVP?
A) The Presenter is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.
What is the advantage of MVVM over MVP?
A) In MVP, Presenter is responsible for view data updates as well as data operations where as in MVVM, ViewModel does not hold any reference to View. It is the View's responsibility to pick the changes from ViewModel. This helps in writing more maintainable test cases since ViewModel does not depend upon View.
Aaaass
Comments
Post a Comment