Refactoring Task
Overviewâ
This exercise addresses a structure where the management of inquiry ticket states is handled by a single method using conditional branches.
You are to identify the design issues from the perspectives of extensibility, maintainability, and testability, and propose a viable refactoring plan.
Initial Codeâ
The following implementation defines a handle()
method that transitions a support ticket through the following states: open
â in_progress
â resolved
â closed
.
All state-dependent behaviors and transitions are hardcoded using if
statements within the same method, making it difficult to accommodate future changes or new states.
- TypeScript
- PHP
- Python
class SupportTicket {
private status: string = "open"; // ç¶æ
: open, in_progress, resolved, closed
handle() {
if (this.status === "open") {
console.log("ăă±ăăăćŠçäžă«èšćźăăŸă");
this.status = "in_progress";
} else if (this.status === "in_progress") {
console.log("ăă±ăăăè§Łæ±șæžăżă«èšćźăăŸă");
this.status = "resolved";
} else if (this.status === "resolved") {
console.log("ăă±ăăăăŻăăŒășăăŸă");
this.status = "closed";
} else if (this.status === "closed") {
console.log("ăăźăă±ăăăŻăă§ă«ç”äșăăŠăăŸă");
}
}
}
<?php
class SupportTicket {
private string $status = "open"; // ç¶æ
: open, in_progress, resolved, closed
public function handle(): void {
if ($this->status === "open") {
echo "ăă±ăăăćŠçäžă«èšćźăăŸă\n";
$this->status = "in_progress";
} elseif ($this->status === "in_progress") {
echo "ăă±ăăăè§Łæ±șæžăżă«èšćźăăŸă\n";
$this->status = "resolved";
} elseif ($this->status === "resolved") {
echo "ăă±ăăăăŻăăŒășăăŸă\n";
$this->status = "closed";
} elseif ($this->status === "closed") {
echo "ăăźăă±ăăăŻăă§ă«ç”äșăăŠăăŸă\n";
}
}
}
class SupportTicket:
def __init__(self):
self.status = "open" # ç¶æ
: open, in_progress, resolved, closed
def handle(self):
if self.status == "open":
print("ăă±ăăăćŠçäžă«èšćźăăŸă")
self.status = "in_progress"
elif self.status == "in_progress":
print("ăă±ăăăè§Łæ±șæžăżă«èšćźăăŸă")
self.status = "resolved"
elif self.status == "resolved":
print("ăă±ăăăăŻăăŒășăăŸă")
self.status = "closed"
elif self.status == "closed":
print("ăăźăă±ăăăŻăă§ă«ç”äșăăŠăăŸă")
Question 1: What design problems exist in this code?â
List and explain specific issues based on the following perspectives:
- All state-specific logic is centralized in a single method
- Difficult to add new states or transition conditions
- Violates SRP (Single Responsibility Principle) and OCP (Open/Closed Principle)
- Hard to test or reuse individual transitions
Question 2: How can this structure be made more maintainable and flexible?â
Formulate an improvement plan based on the following points:
- How can logic for each state be separated and encapsulated?
- What would the class design look like if each state were modeled as its own object?
- What design patterns would be effective, and why?
Example: Design Pattern Candidatesâ
Pattern Name | Purpose and Effects |
---|---|
State | Encapsulates state-specific behavior in separate classes and handles transitions |
Strategy | Provides flexible selection of behavior (when transition control is external) |
Command | Treats each operation as an object, enabling tracking of state changes and undo |
Observer | Notifies external components when state changes, enabling reactive state updates |
Optional Extensionsâ
- If new states such as "on_hold" or "escalated" are introduced, how would the structure accommodate them?
- If state transitions depend on user permissions or action types, how would you incorporate such conditions into the design?
Suggested Output Format (for review or study groups)â
- List of at least three structural problems
- Refactoring strategy and rationale behind chosen patterns
- Design sketch or textual flow of the improved structure (e.g., class relationships or method delegation)