Mixin Pattern की जानकारी
type Constructor<T = {}> = new (...args: any[]) => T;
function MixinName<TBase extends Constructor>(Base: TBase) {
return class extends Base {
// added members
};
}
What Is a Mixin?
एक mixin multiple inheritance की ज़रूरत के बिना एक class में reusable behavior जोड़ता है — एक mixin function एक constructor को input के रूप में लेता है और एक नया constructor return करता है जिसमें original के सभी members plus added वाले होते हैं।
उदाहरण: What Is a Mixin?
type Constructor<T = {}> = new (...args: any[]) => T;
function Timestamped<TBase extends Constructor>(Base: TBase) {
return class extends Base {
timestamp = Date.now();
};
}
class Point {}
class TimestampedPoint extends Timestamped(Point) {}
console.log(typeof new TimestampedPoint().timestamp);
Mixin with Methods
एक mixin किसी भी class में एक या ज़्यादा methods जोड़ सकता है जिस पर यह apply किया जाए; mixin function से return हुई class दोनों original class के members और mixin द्वारा contributed नया behavior carry करती है।
उदाहरण: Mixin with Methods
type Constructor<T = {}> = new (...args: any[]) => T;
function Serializable<TBase extends Constructor>(Base: TBase) {
return class extends Base {
serialize() { return JSON.stringify(this); }
};
}
class Point { x = 1; y = 2; }
class SerializablePoint extends Serializable(Point) {}
console.log(new SerializablePoint().serialize());
Combining Mixins
कई mixins को एक के बाद एक chain किया जा सकता है, हर mixin function chain में पिछले mixin द्वारा पहले से return किए गए constructor को receive करते हुए, capabilities को एक-दूसरे पर layer करते हुए।
उदाहरण: Combining Mixins
type Constructor<T = {}> = new (...args: any[]) => T;
function Named<TBase extends Constructor>(Base: TBase) {
return class extends Base { name = "unnamed"; };
}
function Aged<TBase extends Constructor>(Base: TBase) {
return class extends Base { age = 0; };
}
class Base {}
class Person extends Aged(Named(Base)) {}
const p = new Person();
console.log(p.name, p.age);
Mixin State
Mixins methods के साथ-साथ instance fields भी जोड़ सकते हैं — वे fields returned class से बनाए हर individual instance से belong करते हैं, इसलिए हर instance को उस state की अपनी independent copy मिलती है।
उदाहरण: Mixin State
type Constructor<T = {}> = new (...args: any[]) => T;
function Counter<TBase extends Constructor>(Base: TBase) {
return class extends Base {
count = 0;
increment() { this.count++; }
};
}
class Base {}
class Widget extends Counter(Base) {}
const w1 = new Widget();
const w2 = new Widget();
w1.increment();
console.log(w1.count, w2.count);
Mixin Benefits
Mixins तब फ़ायदा देते हैं जब कई otherwise-unrelated classes को exactly same behavior चाहिए हो, एक गहरी, rigid inheritance hierarchy में सब कुछ force करने के बजाय छोटी, composable capabilities को encourage करते हुए।
उदाहरण: Mixin Benefits
type Constructor<T = {}> = new (...args: any[]) => T;
function Loggable<TBase extends Constructor>(Base: TBase) {
return class extends Base {
log(msg: string) { console.log(`[LOG] ${msg}`); }
};
}
class Service {}
class ApiService extends Loggable(Service) {}
new ApiService().log("Request received");
- एक mixin लिखना जो
Constructor<T>उपयोग नहीं करता, इसलिएclass extends Basefail होता है क्योंकिBaseको constructable नहीं जाना जाता। - mixin function के खिलाफ
instanceofchecks की उम्मीद करना, क्योंकि यह एक class नहीं है। - एक mixin को एक ऐसी class के साथ उपयोग करना जिसका constructor
(...args: any[])से अलग arguments लेता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: