Java Design Patterns Flashcards
What are design patterns?
Design patterns are architectural approaches used to solve commonly occurring problems. They are more of a template to solve a problem in software design.
What are the three types of design patterns?
Creational
Behavioural
Structural
Give examples of Creational design pattern.
Builder
Factories
Prototype
Singleton
Give examples of Behavioral design pattern.
Chain of responsibility Command Interpreter Iterator Mediator Memento Null object Observer State Strategy Template method Visitor
Give examples of Structural design pattern.
Adapter Bridge Composite Decorator Facade Flyweight Proxy
Explain the SOLID design principles.
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.
What does the acronym SOLID stand for in design patterns?
Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Interface Segragation Principle Dependency Inversion Principle
What is a telescoping constructor?
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.
Explain Builder pattern.
It is a creational design pattern that helps in creating instances of complex objects (having multiple parameters and telescoping constructors) in easiest way.
What is fluent interface?
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.
What is a polluted/fat interface?
An interface with more member functions than are logically necessary is called polluted/fat interface.