Skip to main content

Refactoring Task

Overview

This exercise focuses on a structure in which common pre- and post-processing logic is repeatedly implemented across multiple file upload classes.
You are expected to identify the design problems in terms of maintainability, reusability, and extensibility, and propose a refactoring plan.

Initial Code

The following implementation handles file uploads for images, videos, and audio, each in a separate class.
All classes perform the same sequence of steps—"Start validation", "Upload", and "Print completion"—in a repeated manner.
Consider how this duplication might affect future maintenance and what alternatives could be introduced.

class ImageUploader {
upload(file: string) {
console.log("検証中...");
console.log(`画像ファイルをアップロード中: ${file}`);
console.log("完了しました");
}
}

class VideoUploader {
upload(file: string) {
console.log("検証中...");
console.log(`動画ファイルをアップロード中: ${file}`);
console.log("完了しました");
}
}

class AudioUploader {
upload(file: string) {
console.log("検証中...");
console.log(`音声ファイルをアップロード中: ${file}`);
console.log("完了しました");
}
}

Question 1: What design issues can you identify in this code?

List specific problems using the following points as guidance:

  • Common logic is duplicated across all classes
  • Adding or changing shared behavior requires edits in multiple locations
  • Risk of inconsistency or oversight when making updates
  • Violation of the Open/Closed Principle (OCP)

Question 2: How can the structure be improved for better maintainability and flexibility?

Discuss improvement strategies with consideration for the following:

  • What techniques can be used to consolidate common logic?
  • How can the core upload behavior be isolated as the only customizable part?
  • Which design patterns are applicable, and what are their benefits and trade-offs?

Example: Candidate Design Patterns

Pattern NamePurpose and Effect
Template MethodConsolidates shared logic in a base class, allowing customization of core steps
StrategyEncapsulates variation in behavior as interchangeable strategy objects
DecoratorDynamically adds behavior to the upload process without modifying the core logic
VisitorExternalizes operations for different file types to achieve a highly extensible structure

Optional Extensions

  • If new file types (e.g., JSON, Markdown) need to be added, how can the structure minimize the impact of such changes?
  • If a virus scanning step must be added before uploads, which pattern best accommodates this without breaking existing logic?

Suggested Output Format (for review or team discussion)

  • At least three identified structural issues
  • Refactoring strategy and rationale for chosen pattern(s)
  • Conceptual diagram or textual description of the improved design (e.g., inheritance or delegation model)