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 Cakepound.made() // āĻāĻāĻāĻĒā§āĻāĻ You made Pound Cakebiscuit.made() // āĻāĻāĻāĻĒā§āĻāĻ You made Biscuit CakeÂ
Â
āĻĻā§āĻāĻ¤ā§ āĻ āĻ¨ā§āĻ āĻŦā§ āĻšāĻ˛ā§āĻ āĻā§āĻĄāĻāĻŋ āĻ¸āĻšāĻāĨ¤ āĻ¸āĻŦāĻžāĻ° āĻāĻĒāĻ°ā§ āĻāĻŽāĻ°āĻžÂ CakeFactory āĻ¨āĻžāĻŽā§ āĻāĻāĻāĻž āĻā§āĻ˛āĻžāĻ¸ āĻ¤ā§āĻ°āĻŋ āĻāĻ°ā§āĻāĻŋ, āĻ¯āĻž āĻāĻāĻāĻŋ āĻā§āĻ¨ā§āĻ°āĻŋāĻ āĻā§āĻŽā§āĻĒāĻ˛ā§āĻ āĻšāĻŋāĻ¸ā§āĻŦā§ āĻāĻžāĻ āĻāĻ°āĻŦā§ āĻ āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ āĻ¸āĻžāĻŦāĻā§āĻ˛āĻžāĻ¸ āĻĻāĻŋā§ā§ āĻ āĻŦāĻā§āĻā§āĻ āĻ¤ā§āĻ°āĻŋ āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯āĨ¤ āĻŽāĻžāĻ¨ā§, ChocolateCake, PoundCake āĻāĻ°Â BiscuitCake āĻā§āĻ˛āĻžāĻ¸āĻā§āĻ˛ā§āĻā§ āĻĻāĻŋā§ā§ āĻ āĻŦāĻā§āĻā§āĻ āĻ¤ā§āĻ°āĻŋ āĻāĻ°ā§āĨ¤Â CakeFactory āĻā§āĻ˛āĻžāĻ¸ā§āĻ°Â constructor āĻ āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāĻ¨ āĻāĻā§Â createCake āĻ¨āĻžāĻŽā§ āĻ¯āĻž āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ° āĻšāĻŋāĻ¸ā§āĻŦā§ flavor āĻ¨ā§ā§, āĻ¯āĻĻāĻŋ flavor chocolate āĻšā§ āĻ¤āĻžāĻšāĻ˛ā§Â ChocolateClass āĻāĻ° āĻāĻāĻāĻŋ āĻ āĻŦāĻā§āĻā§āĻ āĻ¤ā§āĻ°āĻŋ āĻšāĻŦā§, āĻ āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ flavor āĻšāĻ˛ā§ āĻ āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ āĻā§āĻ˛āĻžāĻ¸ā§āĻ° āĻ āĻŦāĻā§āĻā§āĻ āĻ¤ā§āĻ°āĻŋ āĻšāĻŦā§āĨ¤
āĻāĻāĻžāĻŦā§āĻ āĻāĻŽāĻ°āĻžÂ Factory āĻā§āĻŽā§āĻĒāĻ˛ā§āĻ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ āĻāĻāĻ āĻ°āĻāĻŽā§āĻ° āĻāĻ˛āĻžāĻĻāĻž āĻāĻ˛āĻĻāĻž āĻ āĻŦāĻā§āĻā§āĻ āĻ¤ā§āĻ°āĻŋ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŋāĨ¤
- Person āĻ¨āĻžāĻŽā§āĻ° āĻāĻāĻāĻŋ āĻā§āĻ˛āĻžāĻ¸ āĻ¤ā§āĻ°āĻŋ āĻāĻ°āĻž āĻšā§ā§āĻā§ āĻ¯āĻžāĻ° name āĻ age āĻ¨āĻžāĻŽā§āĻ° ā§¨ āĻāĻŋ āĻāĻā§āĻ°āĻŋāĻŦāĻŋāĻāĻ āĻāĻā§ āĻ¯āĻž āĻāĻ¨ā§āĻ¸āĻā§āĻ°āĻžāĻā§āĻāĻ°ā§āĻ° āĻŽāĻ§ā§āĻ¯ā§ āĻāĻ¨āĻŋāĻļāĻŋā§āĻžāĻ˛āĻžāĻāĻ āĻāĻ°āĻž āĻšā§ā§āĻā§ āĻāĻŦāĻ āĻāĻ° printName āĻ¨āĻžāĻŽā§āĻ° āĻāĻāĻāĻŋ āĻŽā§āĻĨāĻĄ āĻ°ā§ā§āĻā§āĨ¤
- 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 āĻāĻŋāĻā§āĻžāĻ°ā§āĻĄ āĻāĻ° āĻāĻžāĻ āĻāĻŋ?
- āĻŽā§āĻĨāĻĄ āĻ āĻāĻ¨ā§āĻ¸āĻā§āĻ°āĻžāĻā§āĻāĻāĻ° āĻāĻ˛āĻžāĻĻāĻž āĻā§āĻāĻžāĻŦā§?