Design Pattern Questions Flashcards

1
Q

What are Design Patterns?

A

Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Gang of Four (GOF)?

A

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. These authors are collectively known as Gang of Four (GOF).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name types of Design Patterns?

A

Design patterns can be classified in three categories: Creational, Structural and Behavioral patterns.

Creational Patterns - These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.

Structural Patterns - These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.

Behavioral Patterns - These design patterns are specifically concerned with communication between objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Factory pattern?

A

Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Abstract Factory pattern?

A

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Singleton pattern?

A

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you create Singleton class in java?

A

It is two step process. First, make the constructor private so that new operator cannot be used to instantiate the class. Return an object of the object if not null otherwise create the object and return the same via a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the difference between a static class and a singleton class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Name some of the design patterns which are used in JDK library.

A

Following are some of the design patterns which are used in JDK library.

Decorator patttern is used by Wrapper classes.

Singleton pattern is used by Runtime, Calendar classes.

Factory pattern is used by Wrapper class like Integer.valueOf.

Observer pattern is used by event handling frameworks like swing, awt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the benefit of Factory pattern?

A

Factory pattern encapsulates the implementation details and underlying implementation can be changed without any impact on caller api.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Builder pattern?

A

Builder pattern builds a complex object using simple objects and using a step by step approach. This builder is independent of other objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Prototype pattern?

A

Prototype pattern refers to creating duplicate object while keeping performance in mind. This pattern involves implementing a prototype interface which tells to create a clone of the current object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When Prototype pattern is to be used?

A

This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Adapter pattern?

A

Adapter pattern works as a bridge between two incompatible interfaces. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an example of Adapter pattern.

A

A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Decorator pattern?

A

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

17
Q

What is Facade pattern?

A

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.

This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

18
Q

What is Flyweight pattern?

A

Flyweight pattern is primarily used to reduce the number of objects created and to decrease memory footprint and increase performance. 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.

Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.