0% found this document useful (0 votes)
4 views4 pages

Design Patterns QA

The document describes four design patterns: Singleton, Factory, Builder, and Proxy. Each pattern is explained with its definition, use cases, and a Java implementation example. These patterns help in managing object creation, ensuring single instances, and controlling access to objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Design Patterns QA

The document describes four design patterns: Singleton, Factory, Builder, and Proxy. Each pattern is explained with its definition, use cases, and a Java implementation example. These patterns help in managing object creation, ensuring single instances, and controlling access to objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. What is the Singleton Pattern?

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it.

Use Cases:

- Configuration management

- Logging

- Thread pools

Java Implementation:

```java

public class Singleton {

private static Singleton instance;

private Singleton() {}

public static synchronized Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

return instance;

```

2. What is the Factory Pattern?

The Factory Pattern defines an interface for creating an object but lets subclasses alter the type of objects

that will be created.

Use Cases:

- When the creation process is complex

- To centralize object creation

Java Example:
```java

interface Shape {

void draw();

class Circle implements Shape {

public void draw() { System.out.println("Circle"); }

class ShapeFactory {

public Shape getShape(String type) {

if (type.equals("circle")) return new Circle();

return null;

```

3. What is the Builder Pattern?

The Builder Pattern separates the construction of a complex object from its representation so that the same

construction process can create different representations.

Use Cases:

- When object creation involves many steps or parameters

Java Example:

```java

class User {

private String name;

private int age;

public static class Builder {

private String name;

private int age;


public Builder setName(String name) {

this.name = name;

return this;

public Builder setAge(int age) {

this.age = age;

return this;

public User build() {

User user = new User();

user.name = this.name;

user.age = this.age;

return user;

```

4. What is the Proxy Pattern?

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

Use Cases:

- Lazy loading

- Access control

- Logging or performance monitoring

Java Example:

```java

interface Image {

void display();

}
class RealImage implements Image {

public void display() { System.out.println("Displaying real image"); }

class ProxyImage implements Image {

private RealImage realImage;

public void display() {

if (realImage == null) realImage = new RealImage();

realImage.display();

```

You might also like