Java Patterns Interview Questions
Creational Design Patterns
Q1. Define creational design patterns?
A. Creational patterns are used to define and describe how objects are created at class instantiation time. Usually an abstract super class contains the details of the classes that are instantiated and the client class is unaware of such details. Singletion pattern, factory method pattern, abstract factory pattern are examples of creational design pattern.
Q2. A class instance can be created using new operator. Why should we use creational design patterns to create objects?
A. Using new operator to create objects is a valid approach but it’s like hard coding the object type. If we are 100% sure that our object will be of the same class all the time, then we use new operator to create an object. In scenarios where the nature of the object can change according to the nature of the program, we use creational design patterns which offer flexible approach for creating class instances.
Q3. Which object oriented method is used by the creational patterns to instantiate object?
A. Creational patterns use inheritance to decide the object to be instantiated.
Q4. Define a factory with respect to software development.
A. Object construction is referred as a factory. It is used to isolate the creation of objects from their usage. If you are using factory methods for objects creation, then the new derived types will cause no code change in your classes that are using factories.
Q5. When will you use a Factory Pattern?
A. The factory pattern is preferred in the following cases:
- a class does not know which class of objects it must create
- factory pattern can be used where we need to create an object of any one of sub-classes depending on the data provided
Q6. Give an example of factory method that creates and return an object based on a String parameter. You have following interface. Create 2 implementations for it and write a factory method for it.
Code
span style="color: #006699;">factory.patternA. SMSMessage and MMSMessage are implementing IMessage and MessageFactory class provides a factory method getMessageSender(String arg) method.Copyright GeekInterview.comCode
span style="color: #006699;">factory.pattern"Sending SMS contnet: %s to %s"factory.pattern"Sending MMS contnet: %s to %s"factory.pattern"SMS""MMS"Copyright GeekInterview.comQ7. Given below is a class that provides a static factory method. Write a class to use this factory method.
Code
span style="color: #006699;">factory.pattern"SMS""MMS"// IMessage interface - it can have many implementations factory.patternA. Based on the command line argument, factroy method will return an implementation of IMessage.Copyright GeekInterview.comCode
span style="color: #006699;">factory.pattern"1234", "Hello World"); } } }Copyright GeekInterview.comQ8. Name the creational design pattern that provides abstraction one level higher than the factory pattern.
A. Abstract factory pattern provides abstraction one level higher than factory pattern. It encapsulates a group of individual factories.Q9. Write example of abstract factory pattern using following interfaces.
A. WInFactory and OSXFactory are the implementations of GUIFactory. TestApplicationRunner provides createOsSpecificFactory method that returns GUIFactory.Code
Copyright GeekInterview.comCode
span style="color: #0000ff;">"I'm a WinButton""I'm an OSXButton""0)) { return new WinFactory(); } else { return new OSXFactory(); } } } Copyright GeekInterview.comQ10. What is the difference between factory design pattern and abstract factory design pattern?
A. Both are creational design patterns. Factory pattern is said to be there if there is method in a class that is used to create class instances. Any class can have factory methods. Using abstract factory pattern, an instance of a class is created that has only factory methods.Q11. When will you use Abstract factory pattern?
A. Abstract factory pattern is preferred in situations where the names of actual implementing classes are not needed to be known at the client side. This would mean that we can change the implementation from one factory to another.Q12. Which of the following relation can hold between abstract factory and factory method?
IS-A
HAS_A
A. Abstract factory HAS_A (not always) factory method.Q13. What benefits you achieve with factory method?
A. Factory method makes the code more flexible and reusable by eliminating the instantiation of application-specific classes. This way the code deals only with the interface and can work with any concrete class that supports this interface.Q14. What is the liability of using factory method for object creation?
A. We need to do subclassing just to instantiate a particular class.Q15. Name the design pattern in which a class delegates the responsibility of object instantiation to another object via composition.
A. Abstract factory patternQ16. Name the design pattern that uses inheritance and relies on a subclass to handle the desired object instantiation.
A. Factory method pattern Note: From Q to Q are base on the following code:Code
Copyright GeekInterview.comQ17. What type of methods are defined in MazeFactory class?
A. MazeFactory class contains a collection of factory methods.Q18. Name the AbstractFactory in the given code.
A. MazeFactory is acting as both an AbstractFactory and a ConcreteFactory.Q19. Which design pattern you will use if your system being developed must use just one of a set of families of products?
A. Abstract factory pattern can be used if system being developed must use just one of a set of families of products.Q20. What is singletion design pattern?
A. Singleton design pattern allows us to use just one instance of a particular class. It insures that one and only once instance of a particular class is used when requested.Q21. Give an example scenario where you will use singletion pattern.
A. Assume what we are writing an application that needs to log events in a log file. Logging is done with timestamp. We do not want to have more than one instances of Logger otherwise the log file will be created with every instance. In this case, we use Singleton pattern and instantiate the logger when the first request comes or when the server is started.Q22. Write singleton example?
A. The following class shows how to write a singleton class:Code
Copyright GeekInterview.comQ23. Can you name some design patterns that make use of singletion pattern?
A. The Abstract Factory, Builder, and Prototype patterns may use singletion pattern. Also facade objects are often singletons.Q24. How to you prevent instantiation of a singletion class from other classes?
A. Singleton class can have a private constructor with zero argument to prevent instantiation from other classes. For example:Code
Copyright GeekInterview.comQ25. What are the drawbacks of using singletion design pattern?
A. The following drawbacks of Singleton pattern must be considered before applying it to any solution: - it introduces global state into an application and thus it makes unit testing far more difficult -access to the singleton in a multi-threaded context must be serializedQ26. Name the creational design pattern that is used to build complex objects step by step.
A. Builder pattern builds complex objects from simple ones. It separates the construction of complex objects from their representation. The purpose of builder pattern is to abstract steps of object construction so that different implementations of these steps can construct different representations of objects.Q27. What is the role of director in builder pattern implementation?
A. The sequence of object creation is managed by director class. It receives a Concrete Builder (implementation of abstract interface for creating objects) as a parameter and executes the necessary operations on it.Q28. Write an example to show how builder pattern can be applied.
A. The following example uses builder pattern to construct a Pizza object using PizzaBuilder.Code
span style="color: #0000ff;">"Dough:"" Topping:"+topping+" Sauce:""cross").setTopping("ham+pineapple").setSauce("mild""Hawaiian Pizza: "+hawaiianPizza); } }Copyright GeekInterview.comQ29. When cloning an object is preferred over creating a new one?
A. If cost of creating a new object is larger than the cost of cloning the object, then it is better to clone the object.Q30. What is prototype pattern?
A. Prototype simply means making a clone and it is used to cloning an object to avoid its creation. Java provides an interface called Cloneable whose method clone() is used to clone an object.Java Patterns User Questions Java Patterns User DiscussionComments
It looks like you are using an AD Blocker!
Please Turn OFF your ad blocker
- OR -
LOGIN to continue using GeekInterview website.