Factory Pattern
The Factory pattern is a creational design pattern that provides a way to create objects without specifying the exact class or type of object that will be created. This pattern is particularly helpful when you want to delegate the responsibility of instantiating objects to a separate component, often called a "factory." You should consider using the Factory pattern when your code needs to create objects that share a common interface but may have different internal implementations, or when the process of creating an object is complex or involves conditional logic.
1234567891011121314151617181920212223242526272829303132333435363738394041class AdminUser { constructor(name) { this.name = name; this.role = "admin"; } } class GuestUser { constructor(name) { this.name = name; this.role = "guest"; } } class RegularUser { constructor(name) { this.name = name; this.role = "regular"; } } class UserFactory { static createUser(type, name) { switch (type) { case "admin": return new AdminUser(name); case "guest": return new GuestUser(name); default: return new RegularUser(name); } } } const admin = UserFactory.createUser("admin", "Alice"); const guest = UserFactory.createUser("guest", "Bob"); const regular = UserFactory.createUser("regular", "Charlie"); console.log(admin.role); // "admin" console.log(guest.role); // "guest" console.log(regular.role); // "regular"
Using the Factory pattern improves code flexibility by centralizing the logic for object creation. This means you can introduce new types of objects or modify the creation process without changing the code that depends on these objects. Instead of scattering instantiation logic throughout your codebase, the Factory pattern lets you encapsulate it in a single location. This approach also supports the open/closed principle, making your code easier to extend and maintain.
1. The Factory pattern is useful when:
2. Which of the following is a benefit of the Factory pattern?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 7.14
Factory Pattern
Stryg for at vise menuen
The Factory pattern is a creational design pattern that provides a way to create objects without specifying the exact class or type of object that will be created. This pattern is particularly helpful when you want to delegate the responsibility of instantiating objects to a separate component, often called a "factory." You should consider using the Factory pattern when your code needs to create objects that share a common interface but may have different internal implementations, or when the process of creating an object is complex or involves conditional logic.
1234567891011121314151617181920212223242526272829303132333435363738394041class AdminUser { constructor(name) { this.name = name; this.role = "admin"; } } class GuestUser { constructor(name) { this.name = name; this.role = "guest"; } } class RegularUser { constructor(name) { this.name = name; this.role = "regular"; } } class UserFactory { static createUser(type, name) { switch (type) { case "admin": return new AdminUser(name); case "guest": return new GuestUser(name); default: return new RegularUser(name); } } } const admin = UserFactory.createUser("admin", "Alice"); const guest = UserFactory.createUser("guest", "Bob"); const regular = UserFactory.createUser("regular", "Charlie"); console.log(admin.role); // "admin" console.log(guest.role); // "guest" console.log(regular.role); // "regular"
Using the Factory pattern improves code flexibility by centralizing the logic for object creation. This means you can introduce new types of objects or modify the creation process without changing the code that depends on these objects. Instead of scattering instantiation logic throughout your codebase, the Factory pattern lets you encapsulate it in a single location. This approach also supports the open/closed principle, making your code easier to extend and maintain.
1. The Factory pattern is useful when:
2. Which of the following is a benefit of the Factory pattern?
Tak for dine kommentarer!