Time-Base Event QA
Time-Base Event QA
Data Structures
1. Queues
Why: Queues are useful for handling events in the order they occur, such as job scheduling
or breadth-first search.
When: Use when events need to be processed in a First-In-First-Out (FIFO) manner.
Java Example:
import java.util.Queue;
import java.util.LinkedList;
2. Stacks
Why: Stacks are useful for depth-first search, parsing expressions, or implementing
undo/redo functionality.
When: Use when events need to be processed in a Last-In-First-Out (LIFO) manner.
Java Example:
import java.util.Stack;
4. Trees
Why: Trees are useful for efficient sorting, searching, and storing hierarchical data.
When: Use when events need to be sorted or searched efficiently, such as in a calendar or
scheduling system.
Java Example:
import java.util.TreeSet;
5. Interval Trees
Why: Interval trees are useful for efficiently checking collisions between time intervals.
When: Use when multiple events have overlapping time intervals and need to be queried
efficiently.
Java Example:
class Interval {
int start, end;
Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
class IntervalTree {
private Interval[] intervals;
Design Patterns
1. Observer Pattern
Why: The observer pattern is useful for notifying multiple objects about changes to another
object.
When: Use when multiple objects need to react to changes in a single object, such as in
event-driven systems.
Java Example:
interface Observer {
void update(String message);
}
class Subject {
private List<Observer> observers = new ArrayList<>();
2. Command Pattern
Why: The command pattern is useful for encapsulating requests as objects, allowing for
parameterization and queuing.
When: Use when requests need to be handled asynchronously or queued for later
execution.
Java Example:
interface Command {
void execute();
}
class Invoker {
private Command command;
3. Factory Pattern
Why: The factory pattern is useful for creating objects without specifying the exact class of
object that will be created.
When: Use when different types of events need to be created based on specific conditions.
Java Example:
abstract class Event {
abstract void handle();
}
class EventFactory {
public static Event createEvent(String type) {
if (type.equals("concrete")) {
return new ConcreteEvent();
}
return null;
}
}
4. Singleton Pattern
Why: The singleton pattern is useful for ensuring a class has only one instance and
providing a global point of access to it.
When: Use when a single instance of an event handler or manager is required across the
application.
Java Example:
class Singleton {
private static Singleton instance;
private Singleton() {}