Factory pattern used to create object by providing static factory methods. There are many advantage of providing factory methods e.g. caching immutable objects, easy to introduce new objects etc.
Posted Date:- 2021-09-07 06:12:34
This design pattern question is asked on Java interview not just to check familiarity with Composite pattern but also, whether candidate has real life experience or not.
Composite pattern is also a core Java design pattern, which allows you to treat both whole and part object to treat in similar way.
Client code, which deals with Composite or individual object doesn't differentiate on them, it is possible because Composite class also implement same interface as there individual part.
One of the good example of Composite pattern from JDK is JPanel class, which is both Component and Container.
When paint() method is called on JPanel, it internally called paint() method of individual components and let them draw themselves.
Posted Date:- 2021-09-07 06:11:33
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.
Posted Date:- 2021-09-07 06:09:23
Creational patterns use inheritance to decide the object to be instantiated.
Posted Date:- 2021-09-07 06:08:47
1. Using new operator to create objects is a valid approach but it's like hard coding the object type.
2. 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.
3. 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.
Posted Date:- 2021-09-07 06:08:11
<> 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.
Posted Date:- 2021-09-07 06:07:20
Creational patterns use inheritance to decide the object to be instantiated.
Posted Date:- 2021-09-07 06:06:23
<> 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.
Posted Date:- 2021-09-07 06:05:42
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.
Posted Date:- 2021-09-07 06:04:52
1. Decorator pattern enhances capability of individual object.
2. Java IO uses decorator pattern extensively and classical example is Buffered classes like BufferedReader and BufferedWriter which enhances Reader and Writer objects to perform Buffer level reading and writing for improved performance.
Posted Date:- 2021-09-07 06:03:49
Following are the entities of this type of design pattern:
Filter - Filter which will performs certain task prior or after execution of request by request handler.
Filter Chain - Filter Chain carries multiple filters and help to execute them in defined order on target.
Target - Target object is the request handler.
Filter Manager - Filter Manager manages the filters and Filter Chain.
Client - Client is the object who sends request to the Target object.
Posted Date:- 2021-09-07 06:01:25
The intercepting filter design pattern is used when we want to do some pre-processing / postprocessing with request or response of the application:
<> Filters are defined and applied on the request before passing the request to actual target application.
<> Filters can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers.
Posted Date:- 2021-09-07 05:59:22
The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler.
This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers.
Following are the entities of this type of design pattern:
1. Front Controller - Single handler for all kinds of requests coming to the application eitherwebbased/desktopbased.
2. Dispatcher - Front Controller may use a dispatcher object which can dispatch the request to corresponding specific handler.
3. View - Views are the object for which the requests are made.
Posted Date:- 2021-09-07 05:58:28
Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services.
Following are the participants in Data Access Object Pattern:
1. Data Access Object Interface - This interface defines the standard operations to be performed on a model objects.
2. Data Access Object concrete class - This class implements above interface. This class is responsible to get data from a data source which can be database / xml or any other storage mechanism.
3. Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.
Posted Date:- 2021-09-07 05:56:10
Business Delegate Pattern is used to decouple presentation tier and business tier:
It is basically use to reduce communication or remote lookup functionality to business tier code in presentation tier code.
In business tier we have following entities:
<> Client - Presentation tier code may be JSP, servlet or UI java code.
<> Business Delegate - A single entry point class for client entities to provide access to Business Service methods.
<> LookUp Service - Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object.
<> Business Service - Business Service interface. Concrete classes implement this business service to provide actual business implementation logic.
Posted Date:- 2021-09-07 05:55:16
1. In Visitor pattern, we use a visitor class which changes the executing algorithm of an element class.
2. By this way, execution algorithm of element can vary as and when visitor varies.
3. This pattern comes under behavior pattern category.
4. As per the pattern, element object has to accept the visitor object so that visitor object handles the operation on the element object.
Posted Date:- 2021-09-07 05:51:29
1. In Strategy pattern, a class behavior or its algorithm can be changed at run time.
2. This type of design pattern comes under behavior pattern.
3. In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object.
4. The strategy object changes the executing algorithm of the context object.
Posted Date:- 2021-09-07 05:50:33
In Null Object pattern, a null object replaces check of NULL object instance.
Instead of putting if check for a null value, Null Object reflects a do nothing relationship.
Such Null object can also be used to provide default behaviour in case data is not available.
In Null Object pattern, we create an abstract class specifying various operations to be done, concrete classes extending this class and a null object class providing do nothing implemention of this class and will be used seemlessly where we need to check null value.
Posted Date:- 2021-09-07 05:49:31
1. Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.
2. Observer pattern falls under behavioral pattern category.
Posted Date:- 2021-09-07 05:48:57
1. Memento pattern uses three actor classes.
2. Memento contains state of an object to be restored.
3. Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento.
Posted Date:- 2021-09-07 05:48:16
1. Mediator pattern is used to reduce communication complexity between multiple objects or classes.
2. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling.
3. Mediator pattern falls under behavioral pattern category.
Posted Date:- 2021-09-07 05:42:53
Following are the entities of this type of design pattern:
1. Service - Actual Service which will process the request. Reference of such service is to be looked upon in JNDI server.
2. Context / Initial Context - JNDI Context carries the reference to service used for lookup purpose.
3. Service Locator - Service Locator is a single point of contact to get services by JNDI lookup caching the services.
4. Cache - Cache to store references of services to reuse them.
5. Client - Client is the object that invokes the services via ServiceLocator.
Posted Date:- 2021-09-07 05:41:55
This pattern is used in SQL parsing, symbol processing engine etc.
Posted Date:- 2021-09-07 05:40:56
1. Flyweight pattern is primarily used to reduce the number of objects created and to decrease memory footprint and increase performance.
2. This type of design pattern comes under structural pattern as this pattern provides ways to decrease object count thus improving the object structure of application.
3. Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.
Posted Date:- 2021-09-07 05:40:27
1. Composite pattern is used where we need to treat a group of objects in similar way as a single object.
2. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.
3. This type of design pattern comes under structural pattern as this pattern creates a tree structure of group of objects.
4. This pattern creates a class that contains group of its own objects.
5. This class provides ways to modify its group of same objects.
Posted Date:- 2021-09-07 05:39:24
1. This pattern is used when creation of object directly is costly.
2. For example, an object is to be created after a costly database operation.
3. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.
Posted Date:- 2021-09-07 05:37:53
1. Prototype pattern refers to creating duplicate object while keeping performance in mind.
2. This pattern involves implementing a prototype interface which tells to create a clone of the current object.
Posted Date:- 2021-09-07 05:36:47
ollowing are some of the design patterns which are used in JDK library:
1. Decorator patttern is used by Wrapper classes.
2. Singleton pattern is used by Runtime, Calendar classes.
3. Factory pattern is used by Wrapper class like Integer.valueOf.
4. Observer pattern is used by event handling frameworks like swing, awt.
Posted Date:- 2021-09-07 05:35:27
Following are the differences between a static class and a singleton class:
> > A static class can not be a top level class and can not implement interfaces where a singleton class can.
> > All members of a static class are static but for a Singleton class it is not a requirement.
> > A static class get initialized when it is loaded so it can not be lazily loaded where a singleton class can be lazily loaded.
> > A static class object is stored in stack whereas singlton class object is stored in heap memory space.
Posted Date:- 2021-09-07 05:34:23
It is two step process:
1. First, make the constructor private so that new operator cannot be used to instantiate the class.
2. Return an object of the object if not null otherwise create the object and return the same via a method.
Posted Date:- 2021-09-07 05:33:28
Some of the most common Behavioral Patterns are:
a) Iterator -This design pattern is used to access elements of a collection sequentially hiding the internal implementation.
b) Chain of Responsibilities â€“ This design pattern is used where a chain of objects is used to handle the responsibilities. In this design pattern, a request to process the client call is only passed to the next object when the first object fails to process the same.
c) Observer â€“ It is used where a one-to-many relationship exists among the objects and modifying a single object can impact the dependent objects.
d) Mediator -This design pattern encapsulates the communication complexity takes place between different objects and acts as an intermediary between the objects for communication.
Posted Date:- 2021-09-07 05:32:27
This is achieved after applying the Observer Pattern and adding them dynamically without requiring any changes to the Subject Class. Also, Observers remain unaffected when the state chance logic of the subject changes.
Posted Date:- 2021-09-07 05:31:57
When the presentation tier or client application accesses the business tier directly, the business service API is exposed to the client application or presentation tier. So if the business API changes, the corresponding changes will have to be made in the presentation tier. Also, in such an implementation, there is a performance issue as well, since the business service is invoked each time by the client application. Thirdly, this kind of implementation causes a tight coupling between the presentation tier and the business tier. The business delegate pattern solves all these problems. The Business Delegate class is present on the client side and it hides the implementation details of the business tier by providing an abstraction. So the client application only interacts with the delegate class which in turn routes the clientâ€™s request to the appropriate business method.
Posted Date:- 2021-09-07 05:31:32
In the case of Proxy Pattern, the response to the request is guaranteed, provided the communication between the client and the server locations is working.
In the case of Chain of Responsibility, the response is not guaranteed. It means that the request may end up reaching the end of the chain and still might not be processed.
Posted Date:- 2021-09-07 05:31:08
The Facade Pattern helps in hiding the complexities of the application beneath the layer of an interface. This interface helps in reducing the dependency of the client classes on the subsystem classes, thus resulting in a manageable and user-friendly subsystem.
Posted Date:- 2021-09-07 05:30:45
Design patterns can be classified in three categories:
<> Creational patterns
<> Structural patterns
<> Behavioral patterns
Posted Date:- 2021-09-07 05:30:12
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.
Posted Date:- 2021-09-07 04:59:10
Of course, using the wrong design pattern can have a very negative effect on your code. So, it takes good judgement to use the correct design pattern. The term â€œanti-patternâ€ is used to describe a poor programming practice that leads to ineffective code.
Posted Date:- 2021-09-07 04:57:34
Design patterns are useful because they provide a pre-formulated solution to problems based on the experience of other programmers. This can save you a lot of time.
Design patterns also help in communication of a programming solution, and provide a common technical vocabulary between programmers.
Posted Date:- 2021-09-07 04:56:40
Are design patterns the same thing as frameworks? The answer to that is no. Design patterns are more like general guidelines on how to solve specific programming problems, but they do not specify the detailed code thatâ€™s necessary to solve those problems.
Posted Date:- 2021-09-07 04:56:05
Although design patterns were probably used by programmers for many years before, design patterns were formally introduced in computer science in a book called â€œDesign Patterns: Elements of Reusable Object-Oriented Softwareâ€. This book was published in 1994 and is still considered to be the bible of design patterns. There are four people who wrote the book, and they are commonly referred to as the Gang of Four, or GoF (even Go4) for short â€“ many times GoF is even used to refer to the book itself.
Posted Date:- 2021-09-07 04:52:46
Instagram is a photo-sharing application that provides some custom filters to enhance your photo quality. Your application should have photo upload functionality, tagging photos for search, and some basic filters. If you could add share or social network that could be great.
Posted Date:- 2021-09-07 04:31:49
Pastebin allows you to paste text or code and then share a link to that code anywhere you want. Itâ€™s not an online code editor but you can use this to store any kind of text.
Posted Date:- 2021-09-07 04:31:11
A limit order book is used in stock exchanges to match a buy order with a sell order based on price and time priority. How would you go about that? Which data structure you will use? Remember, the speed of matching is key and also the reliability.
Posted Date:- 2021-09-07 04:30:45
A classical system design question from old age which is still popular. Make sure you know how to transition from one state to another like RED to GREEN and from GREEN to ORANGE to RED etc.
Posted Date:- 2021-09-07 04:30:13
The Composite pattern is also a core Java design pattern, which allows you to treat both whole and part object to treat similarly. Client code, which deals with a Composite or individual object, doesn't differentiate between them, it is possible because the Composite class also implements the same interface as their individual part.
One of the good examples of the Composite pattern from JDK is the JPanel class, which is both Component and Container. When the paint() method is called on JPanel, it is internally called the paint() method of individual components and let them draw themselves.
Posted Date:- 2021-09-07 04:18:11
This is an interesting Java design pattern interview question as both Strategy and State pattern has the same structure. If you look at the UML class diagram for both patterns, they look exactly the same, but their intent is totally different.
Posted Date:- 2021-09-07 04:16:50
It is a creational design pattern which is used in cases when a large number of instances of a class are required and these instances are almost identical but may have slightly different properties. This pattern is useful in cases wherever there is high time and cost overhead associated with the object creation. This pattern mainly works by creating clone of the existing object instead of creating a new instance every time so that performance is optimized.
Posted Date:- 2021-09-07 04:15:03
Builder design pattern is more suitable for creating complex object as it is an extension of Factory pattern and is created for solving the issues associated with Factory and Abstract Factory design patterns.
Posted Date:- 2021-09-07 04:14:33
Adapter object has a different input than the real subject whereas Proxy object has the same input as the real subject.
Proxy object is such that it should be placed as it is in place of the real subject.
Posted Date:- 2021-09-07 04:10:27