Vivasoft-logo

 7.4 Factory Pattern āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻž:

 

āĻ†āĻŽāĻ°āĻžÂ Factory Pattern āĻ¸āĻŽā§āĻĒāĻ°ā§āĻ•ā§‡ āĻœā§‡āĻ¨ā§‡āĻ›āĻŋāĨ¤ āĻĻā§‡āĻ–ā§‡āĻ›āĻŋ āĻ•ā§€āĻ­āĻžāĻŦā§‡Â Factory Function āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻ¤ā§‡ āĻšā§ŸāĨ¤ āĻ†āĻœ āĻ†āĻŽāĻ°āĻž āĻĻā§‡āĻ–āĻŦ āĻ•ā§€āĻ­āĻžāĻŦā§‡Â Factory Pattern āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻž āĻ¯āĻžā§ŸāĨ¤

Factory Pattern āĻ¸āĻŽā§āĻĒāĻ°ā§āĻ•ā§‡ āĻ†āĻŽāĻ°āĻž āĻœāĻžāĻ¨āĻŋ āĻ¯ā§‡, āĻāĻŸāĻŋ āĻĻāĻŋā§Ÿā§‡ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻž āĻ¯āĻžā§Ÿ āĻĄāĻŋāĻ°ā§‡āĻ•ā§āĻŸāĻ˛āĻŋ new āĻ•āĻŋāĻ“ā§ŸāĻžāĻ°ā§āĻĄ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ¨āĻž āĻ•āĻ°ā§‡āĨ¤ āĻ¤āĻžāĻ° āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤ā§‡ āĻāĻ‡āĻŸāĻž āĻāĻ•āĻŸāĻž āĻ•āĻŽāĻ¨ āĻ‡āĻ¨ā§āĻŸāĻžāĻ°āĻĢā§‡āĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°ā§‡ āĻ¯āĻž āĻ¸āĻžāĻŦāĻ•ā§āĻ˛āĻžāĻ¸āĻ—ā§āĻ˛ā§‹ āĻĻāĻŋā§Ÿā§‡ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°ā§‡āĨ¤ āĻ§āĻ°āĻž āĻ¯āĻžāĻ•, āĻāĻ•āĻŸāĻžÂ cake āĻāĻ° āĻĢā§āĻ¯āĻžāĻ•ā§āĻŸāĻ°āĻŋāĻ¤ā§‡ āĻšāĻ°ā§‡āĻ• āĻ°āĻ•āĻŽā§‡āĻ°Â cake āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻž āĻšā§ŸāĨ¤ āĻ¯ā§‡āĻŽāĻ¨āĻƒ āĻšāĻ•āĻ˛ā§‡āĻŸ, āĻĒāĻžāĻ‰āĻ¨ā§āĻĄ āĻ†āĻ° āĻŦāĻŋāĻ¸ā§āĻ•ā§āĻŸÂ cakeāĨ¤ āĻāĻ•ā§āĻˇā§‡āĻ¤ā§āĻ°ā§‡ āĻ†āĻŽāĻ°āĻž āĻ•ā§€āĻ­āĻžāĻŦā§‡ āĻĢā§āĻ¯āĻžāĻ•ā§āĻŸāĻ°āĻŋ āĻĒā§āĻ¯āĻžāĻŸāĻžāĻ°ā§āĻ¨ āĻĢāĻ˛ā§‹ āĻ•āĻ°ā§‡ āĻ•āĻžāĻœ āĻ•āĻ°āĻŦ? āĻ¨āĻŋāĻšā§‡āĻ° āĻ‰āĻĻāĻžāĻšāĻ°āĻŖāĻŸāĻŋ āĻ–ā§‡ā§ŸāĻžāĻ˛ āĻ•āĻ°āĻŋāĨ¤

 
 
 
class CakeFactory {
constructor(){
this.createCake = function(flavor){
let cake;
if(flavor === “chocolate”){
cake = new ChocolateCake();
}
else if(flavor === “biscuit”){
cake = new BiscuitCake();
}
else if(flavor === “pound”){
cake = new PoundCake();
}
return cake;
}
}
}
 
class ChocolateCake {
constructor(){
this.cakeFlavor = “Chocolate Cake”;
this.made = function (){
console.log(“You made “+this.cakeFlavor)
}
}
}
 
class BiscuitCake {
constructor(){
this.cakeFlavor = “Biscuit Cake”;
this.made = function (){
console.log(“You made “+this.cakeFlavor)
}
}
}
 
class PoundCake {
constructor(){
this.cakeFlavor = “Pound Cake”;
this.made = function (){
console.log(“You made “+this.cakeFlavor)
}
}
}
 
const cakeFactory = new CakeFactory();
const chocolate = cakeFactory.createCake(“chocolate”);
const pound = cakeFactory.createCake(“pound”);
const biscuit = cakeFactory.createCake(“biscuit”)
chocolate.made() // āĻ†āĻ‰āĻŸāĻĒā§āĻŸāĻƒ You made Chocolate Cake
pound.made() // āĻ†āĻ‰āĻŸāĻĒā§āĻŸāĻƒ You made Pound Cake
biscuit.made() // āĻ†āĻ‰āĻŸāĻĒā§āĻŸāĻƒ You made Biscuit Cake
 

 

āĻĻā§‡āĻ–āĻ¤ā§‡ āĻ…āĻ¨ā§‡āĻ• āĻŦā§œ āĻšāĻ˛ā§‡āĻ“ āĻ•ā§‹āĻĄāĻŸāĻŋ āĻ¸āĻšāĻœāĨ¤ āĻ¸āĻŦāĻžāĻ° āĻ‰āĻĒāĻ°ā§‡ āĻ†āĻŽāĻ°āĻžÂ CakeFactory āĻ¨āĻžāĻŽā§‡ āĻāĻ•āĻŸāĻž āĻ•ā§āĻ˛āĻžāĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°ā§‡āĻ›āĻŋ, āĻ¯āĻž āĻāĻ•āĻŸāĻŋ āĻœā§‡āĻ¨ā§‡āĻ°āĻŋāĻ• āĻŸā§‡āĻŽā§āĻĒāĻ˛ā§‡āĻŸ āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ āĻ•āĻžāĻœ āĻ•āĻ°āĻŦā§‡ āĻ…āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ āĻ¸āĻžāĻŦāĻ•ā§āĻ˛āĻžāĻ¸ āĻĻāĻŋā§Ÿā§‡ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻžāĻ° āĻœāĻ¨ā§āĻ¯āĨ¤ āĻŽāĻžāĻ¨ā§‡, ChocolateCake, PoundCake āĻ†āĻ°Â BiscuitCake āĻ•ā§āĻ˛āĻžāĻ¸āĻ—ā§āĻ˛ā§‹āĻ•ā§‡ āĻĻāĻŋā§Ÿā§‡ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°ā§‡āĨ¤Â CakeFactory āĻ•ā§āĻ˛āĻžāĻ¸ā§‡āĻ°Â constructor āĻ āĻāĻ•āĻŸāĻŋ āĻĢāĻžāĻ‚āĻļāĻ¨ āĻ†āĻ›ā§‡Â createCake āĻ¨āĻžāĻŽā§‡ āĻ¯āĻž āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻŸāĻžāĻ° āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ flavor āĻ¨ā§‡ā§Ÿ, āĻ¯āĻĻāĻŋ flavor chocolate āĻšā§Ÿ āĻ¤āĻžāĻšāĻ˛ā§‡Â ChocolateClass āĻāĻ° āĻāĻ•āĻŸāĻŋ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻšāĻŦā§‡, āĻ…āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ flavor āĻšāĻ˛ā§‡ āĻ…āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ āĻ•ā§āĻ˛āĻžāĻ¸ā§‡āĻ° āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻšāĻŦā§‡āĨ¤

āĻāĻ­āĻžāĻŦā§‡āĻ‡ āĻ†āĻŽāĻ°āĻžÂ Factory āĻŸā§‡āĻŽā§āĻĒāĻ˛ā§‡āĻŸ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻāĻ•āĻ‡ āĻ°āĻ•āĻŽā§‡āĻ° āĻ†āĻ˛āĻžāĻĻāĻž āĻ†āĻ˛āĻĻāĻž āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°āĻŋāĨ¤

  • Person āĻ¨āĻžāĻŽā§‡āĻ° āĻāĻ•āĻŸāĻŋ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻž āĻšā§Ÿā§‡āĻ›ā§‡ āĻ¯āĻžāĻ° name āĻ“ age āĻ¨āĻžāĻŽā§‡āĻ° ā§¨ āĻŸāĻŋ āĻāĻŸā§āĻ°āĻŋāĻŦāĻŋāĻ‰āĻŸ āĻ†āĻ›ā§‡ āĻ¯āĻž āĻ•āĻ¨ā§āĻ¸āĻŸā§āĻ°āĻžāĻ•ā§āĻŸāĻ°ā§‡āĻ° āĻŽāĻ§ā§āĻ¯ā§‡ āĻ‡āĻ¨āĻŋāĻļāĻŋā§ŸāĻžāĻ˛āĻžāĻ‡āĻœ āĻ•āĻ°āĻž āĻšā§Ÿā§‡āĻ›ā§‡ āĻāĻŦāĻ‚ āĻāĻ° printName āĻ¨āĻžāĻŽā§‡āĻ° āĻāĻ•āĻŸāĻŋ āĻŽā§‡āĻĨāĻĄ āĻ°ā§Ÿā§‡āĻ›ā§‡āĨ¤
class Person{
constructor(name, age){
this.name = name;
this.age = age;
}
printName(){
console.log(name);
}
}
//creating object of the Person class
const new_person = new Person(‘Alex’, 20)
 
  • Rectangle āĻ¨āĻžāĻŽā§‡āĻ° āĻāĻ•āĻŸāĻŋ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ°āĻž āĻšā§Ÿā§‡āĻ›ā§‡ āĻ¯āĻžāĻ° height āĻ“ width āĻ¨āĻžāĻŽā§‡āĻ° ā§¨ āĻŸāĻŋ āĻāĻŸā§āĻ°āĻŋāĻŦāĻŋāĻ‰āĻŸ āĻ†āĻ›ā§‡ āĻ¯āĻž āĻ•āĻ¨ā§āĻ¸āĻŸā§āĻ°āĻžāĻ•ā§āĻŸāĻ°ā§‡āĻ° āĻŽāĻ§ā§āĻ¯ā§‡ āĻ‡āĻ¨āĻŋāĻļāĻŋā§ŸāĻžāĻ˛āĻžāĻ‡āĻœ āĻ•āĻ°āĻž āĻšā§Ÿā§‡āĻ›ā§‡ āĻāĻŦāĻ‚ āĻāĻ° getHeight āĻ“ getWidth āĻ¨āĻžāĻŽā§‡āĻ° āĻĻā§āĻŸāĻŋ āĻŽā§‡āĻĨāĻĄ āĻ°ā§Ÿā§‡āĻ›ā§‡.
    class Rectangle {
    constructor(height, width) {
    this.height = height;
    this.width = width;
    }
    getHeight(){
    return this.height;
    }
    getWidth(){
    return this.width;
    }
    }
    const rectangle20x30 = new Rectangle(20,30);
  •  Animal āĻ¨āĻžāĻŽā§‡āĻ° āĻāĻ•āĻŸāĻŋ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ¤ā§ˆāĻ°āĻŋ āĻ•āĻ° āĻ¯āĻžāĻ° āĻŽāĻ§ā§āĻ¯ā§‡ name, color, canFly(boolean), canswimm(boolean) āĻ¨āĻžāĻŽā§‡āĻ° āĻāĻŸā§āĻ°āĻŋāĻŦāĻŋāĻ‰āĻŸ āĻāĻŦāĻ‚ speak, eat, run āĻ¨āĻžāĻŽā§‡āĻ° ā§Š āĻŸāĻŋ āĻŽā§‡āĻĨāĻĄ āĻĨāĻžāĻ•āĻŦā§‡āĨ¤
  • const myCar = {
    make: ‘Ford’,
    model: ‘Mustang’,
    year: 1969,
    start: function(){
    console.log(‘Engine started’);
    },
    stop: function(){
    console.log(‘Engine stoped’);
    }
    };
    āĻ‰āĻĒāĻ°ā§‹āĻ•ā§āĻ¤ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻŸāĻŋāĻ•ā§‡ āĻ•ā§āĻ˛āĻžāĻ¸ā§‡ āĻ°ā§‚āĻĒāĻžāĻ¨ā§āĻ¤āĻ°āĻŋāĻ¤ āĻ•āĻ° āĻāĻŦāĻ‚ āĻ…āĻŦāĻœā§‡āĻ•ā§āĻŸ āĻ•ā§āĻ°āĻŋā§Ÿā§‡āĻŸ āĻ•āĻ°āĨ¤
  • āĻ•ā§āĻ˛āĻžāĻ¸ āĻ•ā§‡āĻ¨ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻž āĻšā§Ÿ āĻ…āĻŦā§āĻœā§‡āĻ•ā§āĻŸ āĻ¤ā§ˆāĻ°āĻŋāĻ° āĻœāĻ¨ā§āĻ¯ā§‡?
  • āĻ•ā§āĻ˛āĻžāĻ¸ā§‡āĻ° āĻŽāĻ§ā§āĻ¯ā§‡ this āĻ•āĻŋāĻ“ā§ŸāĻžāĻ°ā§āĻĄ āĻāĻ° āĻ•āĻžāĻœ āĻ•āĻŋ?
  • āĻŽā§‡āĻĨāĻĄ āĻ“ āĻ•āĻ¨ā§āĻ¸āĻŸā§āĻ°āĻžāĻ•ā§āĻŸāĻŸāĻ° āĻ†āĻ˛āĻžāĻĻāĻž āĻ•ā§€āĻ­āĻžāĻŦā§‡?