Seminar 1 Flashcards

1
Q

What is the template method pattern?

A

Template Method is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure

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

When is template method pattern useful?

A

It is useful when different processes share common logic but need custom
implementations in certain parts

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

With the example of drinks, explain how template method can be useful.

A

You can create a method called prepareDrink() in an abstract class with 3 other methods. One method is abstract while the other two have controlled workflows. By using this template, one can make coffe, tea, chocolate milk etc, without having to rewrite code. Any future modifications to the process (e.g., changing how water is boiled) can be made in one place (the base class), making the code easier to maintain.

abstract class DrinkTemplate { // Template method
public final void prepareDrink() {
boilWater(); // Controlled workflow
pourInCup(); // Controlled workflow
addCondiments(); // Abstract method
print(“Drink is ready”);
}
}

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

What is the chain of responsibility pattern?

A

The Chain of Responsibility (CoR) pattern is a behavioral design pattern that allows multiple objects to handle a request sequentially without the sender knowing which object will process it.

📌 Key Concepts
✅ Decouples sender and receiver – The sender doesn’t know which handler will process the request.
✅ Creates a chain of handlers – Requests move along a chain until handled.
✅ Each handler decides – It can process the request or pass it to the next handler.
✅ Avoids hardcoded conditional logic – Eliminates if-else or switch-case blocks.

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

When is the chain of responsibility useful?

A

The Chain of Responsibility (CoR) pattern is useful when:

✅ 1. Multiple Handlers Might Process a Request
If you don’t know in advance which handler should process a request, CoR allows you to pass the request along a chain until the right handler is found.
📌 Example: A customer service system where a request starts at Level 1 support and escalates to Level 2 or Level 3 if unresolved.

✅ 2. Avoiding Complex if-else or switch-case Statements
If your code has long if-else chains or a huge switch-case block, the CoR pattern can simplify it by delegating responsibility to handlers dynamically.
📌 Example: A logging system where messages are passed to DEBUG, INFO, WARNING, and ERROR loggers, and each logger decides whether to handle or pass the log.

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

With the example of drinks, explain how chain of responsibility can be useful.

A

The chain of responsibility can be used in the scenario by using handlers to verify/handle requests. The following code shows how it works:

// Abstract handler
abstract class DrinkHandler {
protected DrinkHandler nextHandler;

// Set the next handler in the chain
public void setNextHandler(DrinkHandler nextHandler) {
    this.nextHandler = nextHandler;
}

// Abstract method to process the request
public abstract void handleRequest(String drink); }

// Handles coffee orders
class CoffeeHandler extends DrinkHandler {
@Override
public void handleRequest(String drink) {
if (drink.equalsIgnoreCase(“coffee”)) {
System.out.println(“Coffee order processed.”);
} else if (nextHandler != null) {
nextHandler.handleRequest(drink); // Pass to next handler
} else {
System.out.println(“Sorry, we don’t serve that drink.”);
}
}
}

// Handles tea orders
class TeaHandler extends DrinkHandler {
@Override
public void handleRequest(String drink) {
if (drink.equalsIgnoreCase(“tea”)) {
System.out.println(“Tea order processed.”);
} else if (nextHandler != null) {
nextHandler.handleRequest(drink);
} else {
System.out.println(“Sorry, we don’t serve that drink.”);
}
}
}

// Handles hot chocolate orders
class HotChocolateHandler extends DrinkHandler {
@Override
public void handleRequest(String drink) {
if (drink.equalsIgnoreCase(“hot chocolate”)) {
System.out.println(“Hot chocolate order processed.”);
} else if (nextHandler != null) {
nextHandler.handleRequest(drink);
} else {
System.out.println(“Sorry, we don’t serve that drink.”);
}
}
}

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

What is command pattern?

A

The Command Pattern is a behavioral design pattern that turns a request or simple operation into an object, which can then be passed around, stored, and executed at a later time.

It decouples the sender of a request from the object that actually performs the action. This allows for flexibility in command execution, including undo/redo functionality, queuing of requests, and support for multiple receivers.

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

What classes does a command pattern require?

A
  1. Command interface - Defines a execute() method that all ConcreteCommands will implement
  2. Command
  3. Invoker
  4. Reciever
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When is command pattern useful?

A

Use the Command Pattern when:
* You need to decouple the invoker (user interface or application logic) from the receiver (business logic). (The sender doesn’t need to know anything about the receiver, just that it can invoke a command)
* You want to support undo/redo functionality - The Command pattern allows you to store commands and execute them as needed.
* You need to queue operations and execute them later (like in task scheduling systems).
* Logging and auditing - You can use the Command pattern to log or audit each action performed in an application. Each action is encapsulated in a command object, making it easy to track what was executed.
Example: A financial transaction system, where each command (like withdrawal, deposit) is logged and stored for future auditing.
* Extensibility - New commands can easily be added without changing existing code

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

What are the drawbacks of command pattern?

A

Increased number of classes:
* Every action will become a new class (a command).
* Complexity in simple scenarios:
* For simple scenarios, implementing Command can introduce
unnecessary complexity.
* Performance overhead:
* Every command is typically an object, which can lead to memory
usage.

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

What is Singelton pattern?

A

The Singleton Pattern is a creational design pattern that ensures a class has only one instance and provides a global point of access to that instance. It’s often used when exactly one object is needed to coordinate actions across the system, such as a configuration manager, logging utility, or database connection pool.

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

What are the drawback of Singleton?

A

Global state:
* Global access to the instance can make it hard to track and control the
state of your application.
Testing challenges:
* Can make unit testing difficult because their state is often shared
across tests.
Concurrency issues:
* In applications using multi-threading, one needs to make sure the
Singleton instance is thread-safe

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

When should Singleton be used?

A
  • When you need to limit the number of instances of a class to
    one.
  • When a class needs to control access to shared resources.
  • When an object must be accessed globally throughout the
    application
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What should one always think about when using Singleton?

A

To make it thread safe

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

What are creational design patterns?

A

Creational patterns are focused on object creation mechanisms, trying to create objects in a manner suitable to the situation. These patterns deal with the process of object creation, which can be a complex process when done manually. They abstract the instantiation process and make it more flexible and reusable.

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

What are structural design patterns?

A

Structural patterns are concerned with how objects are composed to form larger structures. These patterns help ensure that the relationships between objects are flexible and efficient while providing better organization and reducing complexities in the structure of a system.

17
Q

What are Behavioral Design Patterns?

A

Behavioral patterns are concerned with how objects interact and communicate with each other. These patterns focus on improving or streamlining the flow of control and data in a system, making it easier to manage and extend.

18
Q

What are Concurrency Design Patterns?

A

Concurrency patterns are focused on multi-threading or parallel processing problems. They aim to manage concurrent access to shared resources or handle asynchronous tasks in a safe and efficient manner. This pattern can be considered to be behavioral or creational