Java Design Patterns Flashcards

1
Q

What are design patterns?

A

Design patterns are architectural approaches used to solve commonly occurring problems. They are more of a template to solve a problem in software design.

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

What are the three types of design patterns?

A

Creational

Behavioural

Structural

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

Give examples of Creational design pattern.

A

Builder
Factories
Prototype
Singleton

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

Give examples of Behavioral design pattern.

A
Chain of responsibility
Command
Interpreter
Iterator
Mediator
Memento
Null object
Observer
State
Strategy
Template method
Visitor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give examples of Structural design pattern.

A
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the SOLID design principles.

A

Single Responsibility Principle: a single class should have only single responsibility instead of handling different tasks.

Open Closed Principle: a class should be open for extension but closed for modification. Inheritance and implementation of Interfaces should be used.

Liskov Substitution Principle: one should be able to substitute a subclass object for its baseclass object. This can be achieved by not implementing stricter rules on subclass than those implemented by parent class.

Interface Segregation Principle: clients should not be forced to depend upon interfaces they do not use. If an interface contains various method declaration that are not cohesive, client might be forced to provide implementations of the methods not needed for carrying out a particular task.

Dependency Inversion Principle: high level modules (containing complex logic) should not depend directly on low level modules; both should depend on abstractions (Interface or abstract class). And abstractions should not depend on details, details should depend on abstractions.

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

What does the acronym SOLID stand for in design patterns?

A
Single Responsibility Principle
Open Closed Principle
Liskov Substitution Principle
Interface Segragation Principle
Dependency Inversion Principle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a telescoping constructor?

A

If a class has multiple properties and we are using multiple constructors to set those properties, we will end up with different combinations of constructors, eventually having similar logic.

Instead of writing separate constructors to set the properties, we can have one constructor call a more specific constructor in the hierarchy having more parameters and provide default values for the extra parameters.

public Person(String firstName, String lastName){
 this(firstName, lastName, 0);
}
public Person(String firstName, String lastName, int age){
 this.firstName=firstName;
 this.lastName=lastName;
 this.age=age;
}

This is called telescoping constuctor and is considered an anti-pattern as having multiple constructors with same type of parameters can be confusing.

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

Explain Builder pattern.

A

It is a creational design pattern that helps in creating instances of complex objects (having multiple parameters and telescoping constructors) in easiest way.

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

What is fluent interface?

A

It is an object-oriented API whose design uses method chaining, improving readability.

StringBuilder sb = new StringBuilder();
sb.append("hello").append("world").append("!");

Although this improves legibility, it is difficult to debug as breakpoints cannot be set inside the chain.

To make a method use fluent interface, we can make it return β€œthis” or some other other object, on which another method can be called.

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

What is a polluted/fat interface?

A

An interface with more member functions than are logically necessary is called polluted/fat interface.

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