← Back to TypeScript Course | Chapter 22: Mixins | Lesson 4 of 4

Mixin के वैकल्पिक Approaches

Mixins को application की ज़रूरतों के अनुसार कई तरीकों से implement किया जा सकता है। Class factories के अलावा, आप object composition, interfaces, या helper functions उपयोग करके behavior combine कर सकते हैं।

Object Composition

Object composition एक नई subclass बनाने के बजाय independent objects को सीधे combine करता है — यह अक्सर सबसे simple option है जब added behavior को class inheritance में participate करने की बिल्कुल ज़रूरत न हो।

उदाहरण: Object Composition

typescript
function withLogger(obj: object) {
  return { ...obj, log: (msg: string) => console.log(msg) };
}
const service = withLogger({ name: "Api" });
service.log("Composed via object spread, no class involved");

Interface Composition

Interfaces actual runtime object के बारे में कुछ भी बदले बिना सिर्फ type level पर एक combined capability describe कर सकते हैं — उपयोगी जब एक implementation पहले से मौजूद हो और सिर्फ इसका type shape describe करना हो।

उदाहरण: Interface Composition

typescript
interface Named { name: string; }
interface Timestamped { createdAt: Date; }
type AuditedRecord = Named & Timestamped;
const record: AuditedRecord = { name: "Order1", createdAt: new Date() };
console.log(record);

Mixin Class Factories

Class factories (mixin-function pattern) सही tool हैं जब mixed-in behavior को असली inheritance में participate करना हो और class instances के साथ naturally काम करना हो, खासकर कई अलग-अलग classes के across।

उदाहरण: Mixin Class Factories

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function Loggable<TBase extends Constructor>(Base: TBase) {
  return class extends Base { log() { console.log("logging"); } };
}
class Service {}
class LoggableService extends Loggable(Service) {}
new LoggableService().log();

Functional Behavior Composition

छोटे functions बिना किसी classes involved के भी behavior compose कर सकते हैं — एक अच्छा alternative जब भी data और behavior plain objects और standalone functions के रूप में ज़्यादा naturally model होते हों।

उदाहरण: Functional Behavior Composition

typescript
function createCounter() {
  let count = 0;
  return {
    increment: () => ++count,
    get: () => count,
  };
}
const counter = createCounter();
counter.increment();
console.log(counter.get());

Choosing an Approach

Class mixins तब चुनें जब आपको specifically reusable class behavior inheritance में tied चाहिए; object या functional composition को prefer करें जब behavior inheritance पर depend न करे और simpler data-oriented code बेहतर fit हो।

उदाहरण: Choosing an Approach

typescript
// Need real inheritance + class instances -> mixin class factory
// Otherwise -> plain object/functional composition (simpler)
function withId<T extends object>(obj: T, id: number) {
  return { ...obj, id };
}
console.log(withId({ name: "Item" }, 1));
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 को { ...obj, log } से spread करना और इसे object के रूप में type करना, इसलिए service.log TypeScript को नहीं पता चलता जब तक return type infer या annotate न किया जाए।
  2. Interface intersection types उपयोग करना और उनसे runtime behavior add होने की उम्मीद करना, जब वे सिर्फ types बदलते हैं।
  3. Class mixins के लिए पहुंचना जब एक simple object spread या एक function काफी होता।
चैप्टर सारांश
  • Mixin pattern कई sources से behavior को एक class में combine करता है।
  • Mixins एक base class पर apply किए जाते हैं, और constraints restrict करती हैं कि उन्हें किन classes पर apply किया जा सकता है।
  • Mixins के alternative approaches मौजूद हैं, जो अलग trade-offs प्रदान करते हैं।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.