← Back to TypeScript Course | Chapter 16: Advanced Patterns | Lesson 2 of 7

Factory Pattern की जानकारी

Factory Pattern object creation को एक dedicated function या class में move करता है। यह उपयोगी है जब construction details expose किए बिना exact object type input के आधार पर चुनी जानी चाहिए।
Syntax
typescript
function factory(kind: "a" | "b"): Product {
  switch (kind) {
    case "a": return new ProductA();
    case "b": return new ProductB();
  }
}

Factory Function

एक factory function इस pattern का सबसे simple रूप है — एक typed function जो कुछ input लेती है और एक पूरी तरह से constructed object return करती है, object के construction details को एक single call के पीछे छिपाते हुए।

उदाहरण: Factory Function

typescript
interface User {
  name: string;
}
function createUser(name: string): User {
  return { name };
}
console.log(createUser("Ravi"));

Interface के साथ Factory

एक interface के against typed एक factory गारंटी देती है कि यह produce करने वाली हर object उस interface के shape को satisfy करती है, भले ही factory internally scenes के पीछे कई अलग concrete implementations बनाए।

उदाहरण: Factory with Interface

typescript
interface Shape {
  area(): number;
}
function createSquare(side: number): Shape {
  return { area: () => side * side };
}
console.log(createSquare(4).area());

Factory Class

एक factory class related creation logic को methods में group करती है, जो उपयोगी है जब एक object construct करने के लिए कई steps चाहिए या factory खुद calls के बीच रखे shared internal state पर depend करता है।

उदाहरण: Factory Class

typescript
class WidgetFactory {
  private count = 0;
  create(name: string) {
    this.count++;
    return { id: this.count, name };
  }
}
const factory = new WidgetFactory();
console.log(factory.create("Button"));

Configuration-Based Creation

Configuration-based creation का मतलब है factory एक passed-in option (जैसे एक type: circle | square field) के आधार पर यह चुनता है कि कौन सा concrete type construct करना है, उस branching logic को बिखरने के बजाय एक जगह centralize करते हुए।

उदाहरण: Configuration-Based Creation

typescript
interface Shape { area(): number; }
function createShape(type: "circle" | "square", size: number): Shape {
  if (type === "circle") return { area: () => Math.PI * size * size };
  return { area: () => size * size };
}
console.log(createShape("circle", 2).area());

Factory कब उपयोग करें

Factory pattern तब अपनाएं जब object creation में meaningful decision logic या setup steps शामिल हों — किसी भी असली construction complexity न रखने वाले plain object के लिए, एक factory value जोड़े बिना सिर्फ indirection जोड़ता है।

उदाहरण: When to Use Factory

typescript
interface Shape { area(): number; }
function createShape(type: "circle" | "square", size: number): Shape {
  return type === "circle"
    ? { area: () => Math.PI * size * size }
    : { area: () => size * size };
}
console.log(createShape("square", 3).area());
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक object return करना जो interface को satisfy नहीं करता, जैसे area() missing हो।
  2. एक factory function पर new उपयोग करना, जब factories normally call होती हैं।
  3. Factory से any return करना, इसलिए callers specific type खो देते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.