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?
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you explain how the Factory pattern supports the open/closed principle?
What are some real-world scenarios where the Factory pattern is especially useful?
Can you show how to add a new user type to this example?
Awesome!
Completion rate improved to 7.14
Factory Pattern
Svep för att visa menyn
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?
Tack för dina kommentarer!