Mixin के वैकल्पिक Approaches
In this page:
Object Composition
Object composition एक नई subclass बनाने के बजाय independent objects को सीधे combine करता है — यह अक्सर सबसे simple option है जब added behavior को class inheritance में participate करने की बिल्कुल ज़रूरत न हो।
उदाहरण: Object Composition
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
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
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
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
// 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));
- एक object को
{ ...obj, log }से spread करना और इसेobjectके रूप में type करना, इसलिएservice.logTypeScript को नहीं पता चलता जब तक return type infer या annotate न किया जाए। - Interface intersection types उपयोग करना और उनसे runtime behavior add होने की उम्मीद करना, जब वे सिर्फ types बदलते हैं।
- 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: