Design Patterns in Dart: Prototype Pattern

Prototype Pattern – one of the creational design patterns that focuses on object cloning instead of building from scratch.


What is the Prototype Pattern?

The Prototype Pattern is about creating new objects by copying existing ones (prototypes).

Instead of calling a constructor, we make a clone.
This is especially useful when object creation is costly (e.g., large data structures, network-heavy objects).


When to Use It?

  • When object creation is expensive or complex.
  • When you want to avoid repeating initialization logic.
  • When you need different variations of an object.

Example in Dart: Document Cloning

Let’s imagine we have a Document object with lots of properties.
Instead of reconstructing it every time, we’ll clone it.


Step 1: Define Prototype Interface

abstract class Prototype<T> {
  T clone();
}

Step 2: Concrete Class that Implements Prototype

class Document implements Prototype<Document> {
  String title;
  String content;
  List<String> tags;

  Document(this.title, this.content, this.tags);

  @override
  Document clone() {
    // Deep copy to avoid modifying the original list
    return Document(title, content, List.from(tags));
  }

  @override
  String toString() {
    return "Document(title: $title, content: $content, tags: $tags)";
  }
}

Step 3: Client Code

void main() {
  // Original document
  Document original = Document("Design Patterns", "Prototype Pattern", ["dart", "oop", "patterns"]);
  print("Original: $original");

  // Cloned document
  Document copy = original.clone();
  copy.tags.add("clone");
  copy.title = "Cloned Document";

  print("Copy: $copy");
  print("Original after clone modification: $original");
}

Example Output

Original: Document(title: Design Patterns, content: Prototype Pattern, tags: [dart, oop, patterns])
Copy: Document(title: Cloned Document, content: Prototype Pattern, tags: [dart, oop, patterns, clone])
Original after clone modification: Document(title: Design Patterns, content: Prototype Pattern, tags: [dart, oop, patterns])

Notice how the original document was not modified when we changed the copy.


Benefits of Prototype Pattern

  • Reduces the cost of creating objects.
  • Simplifies object creation logic.
  • Allows creation of objects without knowing their exact classes.

Drawbacks

  • Requires deep cloning logic, which can be complex if the object graph is large.
  • Might introduce hidden dependencies if cloning isn’t implemented properly.

Flutter Use Cases

  • Widget Templates: Clone base widget configurations.
  • Theme Objects: Create variations of app themes by cloning prototypes.
  • Game Development: Spawn multiple enemies or objects from a base prototype.

Summary Table

AttributePrototype Pattern
PurposeClone objects instead of creating anew
Common Use CasesDocuments, configurations, game objects
Easy in Dart?Yes, with clone() method
Useful in Flutter?For themes, widget configs, object copies

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Lên đầu trang