Design patterns in java interview questions11/16/2023 ![]() ![]() Name the design pattern that uses inheritance and relies on a subclass to handle the desired object instantiation. Name the design pattern in which a class delegates the responsibility of object instantiation to another object via composition. We need to do subclassing just to instantiate a particular class. What is the liability of using factory method for object creation?Ī. This way the code deals only with the interface and can work with any concrete class that supports this interface. Factory method makes the code more flexible and reusable by eliminating the instantiation ofĪpplication-specific classes. What benefits you achieve with factory method?Ī. Abstract factory HAS_A (not always) factory method. Which of the following relation can hold between abstract factory and factory method?Ī. This would mean that we can change the implementation from one factory to another. Abstract factory pattern is preferred in situations where the names of actual implementing classes are not needed to be known at the client side. When will you use Abstract factory pattern?Ī. Using abstract factory pattern, an instance of a class is created that has only factory methods. Factory pattern is said to be there if there is method in a class that is used to create class instances. What is the difference between factory design pattern and abstract factory design pattern?Ī. Create 2 implementations for it and write a factory method for it. Give an example of factory method that creates and return an object based on a String parameter. factory pattern can be used where we need to create an object of any one of sub-classes depending on the data provided ![]() a class does not know which class of objects it must create The factory pattern is preferred in the following cases: 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.Ī. ![]() It is used to isolate the creation of objects from their usage. Object construction is referred as a factory. Define a factory with respect to software development.Ī. Creational patterns use inheritance to decide the object to be instantiated. Which object oriented method is used by the creational patterns to instantiate 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. 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. Using new operator to create objects is a valid approach but it’s like hard coding the object type. Why should we use creational design patterns to create objects?Ī. A class instance can be created using new operator. Singletion pattern, factory method pattern, abstract factory pattern are examples of creational design pattern. Usually an abstract super class contains the details of the classes that are instantiated and the client class is unaware of such details. Creational patterns are used to define and describe how objects are created at class instantiation time. ![]() Lazy Initialization is a method used for implementing singleton pattern, that creates instances in the Global Access Method.Java Patterns Interview Questions and Answers Creational Design PatternsĪ. Initializating the class "public static final" Field. In how many ways we can write Singleton Class in Java? Pseudo Code used for achieving singleton is: What Pseudo Code is used for achieving Singleton? It can be avoided by throwing an exception from the constructor. Singleton Class needs to be private, which helps in preventing the creation of an instance of Singleton from the outside but can access all the private methods and fields, by opening the threat of any other instance. How can we prevent from creating another instance of Singleton using reflection? ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |